MFC technology accumulation--the things that are based on MFC dialog box Class 4

Source: Internet
Author: User

3.3.4 with compatible DC Loading DIB Bitmap

Create a DC that is compatible with the device environment by temporarily importing the bitmap to a compatible DC and then drawing the bitmap into the device environment using the CDC::BITBLT or Cdc::stretchblt function.

The sample code is as follows:

voidCfdlg::onloadbitmap () {//Todo:add your control notification handler code hereHbitmap hbitmap;    BITMAP Bitmapinfo;    CBitmap CBitmap;    RECT RC; //Gets the window pointer for the specified ID numbercwnd* Mywnd =GetDlgItem (Idc_bitmaparea); //defines a device environment (DC) associated with a specified windowcclientdc DC (Mywnd); //defines a DC-to-compatible memory DCCDC MEMDC; //gets the size of the specified windowMywnd->getclientrect (&RC); //Import Bitmap HandleHbitmap = (HBITMAP):: LoadImage (NULL,"d:\\wanghui1.bmp", Image_bitmap,0,0, Lr_loadfromfile); //Attaching a bitmap handle to a bitmap class objectCBitmap.    Attach (HBITMAP); //get bitmap information, including bitmap width, high parameterCBitmap. Getbitmap (&bitmapinfo); //creates a memory DC that is compatible with the specified window DCMEMDC. CreateCompatibleDC (&DC); //to load a bitmap into a memory DCCBitmap *oldbitmap = MEMDC. SelectObject (&CBitmap); //import a bitmap from a memory DC into the actual DC in the specified window//DC. StretchBlt (0, 0, (Rc.right-rc.left), (rc.bottom-rc.top), &MEMDC, 0, 0, bitmapinfo.bmwidth, Bitmapinfo.bmheight, srccopy);dc. BITBLT (0,0, (Rc.right-rc.left), (rc.bottom-rc.top), &MEMDC,0,0, srccopy); //to remove a bitmap from the memory DCMEMDC.    SelectObject (OLDBITMAP); //Deletes a bitmap handle, and the bitmap class object calls its destructor after the program ends to free memoryCBitmap. DeleteObject ();}

For the above example code, several functions are explained in detail:

HANDLE LoadImage (

hinstance hinst      , //Handle of the instance containing the image

LPCTSTR LpszName ,       //Name or identifier of image

UINT Utype ,             //Type of image

int cxdesired ,          //Desired width

int cydesired ,          //Desired height

UINT fuload //Load Flags

);

This function can import an icon, a cursor, and a bitmap.

Hinst: Point to an instance of an application that can be obtained through the AfxGetInstanceHandle () function,

LpszName: Refers to the file name or resource identifier of the image, and when Fuload=lr_loadfromfile, LpszName points to the file name, hinst the value null.

Utype: Image type, values are:

Value meaning

Image_bitmap Importing bitmaps

Image_cursor Import Cursor

Image_icon Import icon

Cxdesired, cydesired: The width and height of the cursor or icon, in pixels.

Fuload: When fuload = Lr_loadfromfile , indicates that the image is loaded from a file pointed to by Lpszname, and if Lr_loadfromfile is unspecified, lpszname points to the resource name.

3.3.5 Use SetDIBitsToDevice to load the bitmap

first explain the SetDIBitsToDevice:  

int SetDIBitsToDevice (

HDC hdc, //Handle to device context

int xdest, //X-coordinate of Upper-left corner of dest. Rect .

int ydest, //Y-coordinate of Upper-left corner of dest. Rect .

DWORD dwwidth, //Source Rectangle width

DWORD dwheight, //source Rectangle height

int XSRC, //X-coordinate of Lower-left corner of source rect.

int YSRC, //Y-coordinate of Lower-left corner of source rect.

UINT Ustartscan, //First scan line in array

UINT Cscanlines, //number of scan lines

CONST VOID *lpvbits, //Address of array with DIB bits

CONST bitmapinfo *lpbmi, //address of structure with bitmap info.

UINT fucoloruse //RGB or palette indexes

);

Example code:

voidCfdlg::onloadbitmap () {
unsignedChar* imagebuffer=NULL; unsignedChar* ImageData =NULL; Bitmapfileheader M_bitmapfileheader; Bitmapinfoheader*Infoheader; Rgbquad*Palette; Bitmapinfo*BMI;; FILE*ImageFile; unsignedintBytessize=0; if(null== (Imagefile=fopen ("d:\\wanghui1.bmp","RB")) {printf ("can not open the image file!"); return ; } Else { //Read Bitmap file headerFread (&m_bitmapfileheader,sizeof(Bitmapfileheader),1, ImageFile); //opening up memory for bitmap information headers and palettesImagebuffer =NewUnsignedChar[m_bitmapfileheader.bfoffbits-sizeof(Bitmapfileheader)]; //reading header and palette data blocksFread (imagebuffer,m_bitmapfileheader.bfoffbits-sizeof(Bitmapfileheader),1, ImageFile); //force Imagebuffer to the Bitmapinfo type, and then point the information header and palette pointers to the corresponding address locationBMI = (bitmapinfo*) Imagebuffer; Infoheader= & (bmi->Bmiheader); Palette= bmi->bmicolors; //calculating the size of a graph data byteBytessize = (Infoheader->biwidth * infoheader->biheight * (infoheader->bibitcount/8)); //Open Bitmap data memory blockImageData =NewUnsignedChar[Bytessize]; //reading bitmap data blocksFread (Imagedata,bytessize,1, ImageFile); //gets the specified windowcwnd* Mywnd =GetDlgItem (Idc_bitmaparea); //creates the DC associated with the specified windowcclientdc DC (Mywnd); RECT RC; //gets the size of the specified window areaMywnd->getclientrect (&RC); //draws a bitmap to the DC on the specified windowSetDIBitsToDevice (DC.M_HDC,0,0, Infoheader->biwidth, Infoheader->biheight,0,0,0, infoheader->Biheight, ImageData, BMI, dib_rgb_colors); } //releasing dynamically opened memorydelete[] Imagebuffer; Delete[] ImageData;}

MFC technology accumulation--the things that are based on MFC dialog box Class 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.