Stretchblt function and bitblt function usage

Source: Internet
Author: User

Both stretchblt and bitblt are used in the dual-buffer view to display an image.

I. stretchblt

The function copies a bitmap from the source rectangle to the target rectangle. If necessary, the image is stretched or compressed in the mode set by the target device. That is, the bitmap in the memory can be copied to the screen and scaled according to the size of the Screen Drawing area to adapt to the response screen (or image control)


 

Bool stretchblt (int x, int y, int nwidth, int nheight, CDC * psrcdc, int xsrc, int ysrc, int nscwidth, int nscheight, DWORD dwrop ); X: coordinate of the upper left corner of the X axis of the logical unit;(After obtaining the DC of the image control, this value is generally set to 0) Y: coordinate of the upper left corner of the Y axis of the logical unit;(After obtaining the DC of the image control, this value is generally set to 0)Nwidth: the width of the device rectangle; (That is, the width of the image control, obtained through int rcwidth = RC. Right-RC. Left;, RC saves the coordinate information of the rectangular area of the image control)Nheight: the height of the device's rectangle; (That is, the height of the image control, obtained through int rcheight = RC. Bottom-RC. Top)Psrcdc: Context of the source device; (That is, we define the memory DC, and then take the address)Xsrc: source rectangle Coordinates in the upper left corner of the X axis;(This value is 0 because all images are copied)Ysrc: source rectangle y Coordinates in the upper left corner of the axis;(This value is 0 because all images are copied)Specifies the X-coordinate (in logical units) of the upper-left corner of the source rectangle. nscwidth: width of the source rectangle; (If we define a structThe bitmap type m_bmp is used to save the bitmap information.M_bmp .bmwidth get the width)Nscheight: height of the source rectangle (If we define a structThe bitmap type m_bmp is used to save the bitmap information.M_bmp .bmheight get width)Dwdrop: Specify the grating operation to be performed. (There are many options. We generally choose srccopy: copy the source rectangle area directly to the target rectangle area .)
Ii. bitblt Functions

This function performs bit_block conversion on pixels in the specified source device environment area to transmit the data to the target device environment.

 

CDC: bitblt

Bool bitblt (int X, IntY, Int Nwidth, Int Nheight, CDC *Psrcdc, Int Xsrc, Int Ysrc, DWORDDwrop );

The bitblt function and the preceding stretchblt function have almost the same parameters, but the second and third parameters are missing:

Nscwidth: the width of the source rectangle; And, nscheight: the height of the source rectangle.

The first four parameters are also related to the target rectangle (that is, our image control area). If it is an image control, the start coordinate is (0, 0 ), the ending coordinate is the width and height of the rectangle area;

For the source rectangular area (the bitmap in the memory DC), you only need to give the copy start point,

The bitblt function does not stretch or compress the original bitmap.Nwidth,The nheight value determines how many parts should be displayed.




The following code provides the specific usage of the two functions:

Void cenvirmonibeta1view: showbitmap (CDC * PDC, cstring totalname, const rect & rc)
{
// The image display function LoadImage loads an image based on the full path totalname of the bitmap (or image ).
Hbitmap 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 is the created bitmap object.
}
M_bitmap.attach (m_hbitmap); // associate the bitmap object m_bitmap with the bitmap we load (the loaded bitmap can only provide a bitmap handle)

// Define and create a memory device Environment
CDC m_dcmemory;
If (!M_dcmemory. Createcompatibledc (PDC) // create a compatible DC
Return;


// Define a bitmap structure and save the image information in the bitmap structure.
Bitmap bitmap;
M_bitmap.getbitmap (&Bitmap);

Cbitmap * pbmpold = NULL;
M_dcmemory. SelectObject (& m_bitmap); // select the bitmap to the temporary memory device environment.

// Obtain the width and height of the image area (image control ).
Int rcwidth = RC. Right-RC. Left;
Int rcheight = RC. Bottom-RC. Top;

// Call the bitblt function for Image Display
PDC-> bitblt (0, 0, rcwidth, rcheight, & dcbmp, 0, srccopy );
// If you want the image and the rectangle to be automatically adapted, you can use the following function
// PDC-> stretchblt (0, 0, rcwidth, rcheight, & dcbmp, 0, bitmap. bmwidth, bitmap. bmheight, srccopy );

// The following is some Aftercare Work
Dcbmp. SelectObject (pbmpold); // restore the bitmap of the temporary DC
Deleteobject (& m_bitmap); // deletes a bitmap.
Dcbmp. deletedc (); // Delete the background DC
// Invalidate (); // always call ondraw to display the image


}

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.