Original address: StretchBlt function and BitBlt function usage
Both StretchBlt and BitBlt are used in a double-buffered view to display an image
First, StretchBlt
The function copies a bitmap from the source rectangle to the target rectangle and, if necessary, stretches or compresses the image according to the mode set by the target device. This means that the in-memory bitmap is copied to the screen, and can be scaled according to the size of the screen paint area, adapting to the responsive screen (or image control)
BOOL StretchBlt (intx,inty,intnwidth,intnheight, CDC*PSRCDC,intXsrc,intYsrc,intNsrcwidth,intnsrcheight, DWORD dwrop); x: The upper-left coordinate of the logical unit x-axis; (after getting the image control's DC, the value is typically set to0y: The upper-left coordinate of the logical unit y-axis; (after getting the image control's DC, the value is typically set to0) Nwidth: The width of the device rectangle, which is the width of the image control, via int rcwidth= Rc.right-Rc.left; To obtain, RC for saving the image control rectangle area coordinate information) Nheight: The height of the device rectangle, (that is, the height of the image control, through the int rcheight= Rc.bottom-rc.top; to obtain) PSRCDC: The source device context, (that is, the memory DC we defined, and then the address) XSRC: the upper-left coordinate of the x-axis of the source rectangle, which is usually the entire picture copy, so this value is0YSRC: The upper-left coordinate of the y-axis of the source rectangle, usually the entire picture copy, so this value is0)nsrcwidth: the source rectangle width; (if we define a struct bitmap type m_bmp save bitmap information, you can use M_bmp.bmwidth to get the width) nsrcheight: Source Rectangle Height ( If we define a struct bitmap type m_bmp Save the bitmap information, you can use M_bmp.bmheight to get the width) Dwrop: Specifies the raster operation to be performed. (This has a lot of options, we generally choose srccopy: Copy the source rectangle directly to the target rectangle area.) )
Second, BitBlt function
This function converts pixels in the specified source device environment area to a bit-block (bit_block) transition to the target device environment.
bool BitBlt ( int X, int Y, int nwidth, int nheight, CDC * PSRCDC, int XSRC, int Ysrc, DWORD Dwrop); The BitBlt function and the arguments above the StretchBlt function are basically the same, except for the second and last argument: nsrcwidth: Source rectangle width; and, nsrcheight: source Rectangle Height The first four parameters are also related to the target rectangle (that is, our image control area), and if it is an image control, the starting coordinates are also ( 0 , 0
The BitBlt function does not stretch or compress the original bitmap, and only determines how many parts are displayed based on the Nwidth,nheight value we give.
The following code shows the specific usage of the two functions:
voidCenvirmonibeta1view::showbitmap (CDC *PDC, CString totalname,ConstRECT &RC) { //Show Picture function LoadImage load a picture based on the full path name of the bitmap (or picture) TotalnameHbitmap M_hbitmap; M_hbitmap=(HBITMAP) LoadImage (NULL, Totalname, Image_bitmap, 0, 0, Lr_loadfromfile| lr_defaultsize|lr_createdibsection); //Previously, a CBitmap class object M_bitmap was defined in the class . if(m_bitmap.m_hobject) {M_bitmap. Detach (); //M_bitmap The bitmap object that was created} m_bitmap. Attach (M_HBITMAP); //the Diagram object M_bitmap associated with the bitmap we loaded//define and create a memory device environmentCDC m_dcmemory; if(!m_dcmemory.createcompatibledc (PDC))//Creating a compatible DC return; //define a bitmap structure to save the picture information in the bitmap structure bodyBITMAP BITMAP; M_bitmap. Getbitmap (&bitmap); CBitmap*pbmpold =NULL; M_dcmemory.selectobject (&M_BITMAP);//to select a bitmap into a temporary memory device environment//Gets the width and height of the screen paint area (Image control) intRcwidth = Rc.right-Rc.left; intRcheight = Rc.bottom-Rc.top; //Image Display Call function BitBltPdc->bitblt (0,0, Rcwidth,rcheight,&dcbmp,0,0, srccopy); //If you want the picture and rectangle area to fit automatically, you can use the following functionPdc->stretchblt (0,0, Rcwidth,rcheight,&dcbmp,0,0, bitmap.bmwidth,bitmap.bmheight,srccopy); //Here are some of the aftermath workDcbmp.selectobject (Pbmpold);//Restore a bitmap for a temporary DCDeleteObject (&M_BITMAP);//Delete BitmapDcbmp.deletedc ();//Delete a background DC//Invalidate (); //always call OnDraw display picture}
The use of the "Go" StretchBlt function and the BitBlt function