Solution to the image distortion problem of StretchBlt in VC ++, vcstretchblt

Source: Internet
Author: User

Solution to the image distortion problem of StretchBlt in VC ++, vcstretchblt

Using StretchBlt in VC may encounter some problems related to dot matrix size scaling. When extending a lattice chart, StretchBlt must copy the column or column. If the magnification is not an integer multiple of the source image, the resulting image may be distorted.
If the target rectangle is smaller than the source rectangle, StretchBlt must merge the two rows (or columns) or multiple rows (or columns) into one (or column) When narrowing down the image ). There are four ways to complete this operation. It selects one of the methods based on the device content stretch mode attributes. You can use the SetStretchBltMode function to modify this attribute.
SetStretchBltMode (hdc, iMode );
The following values can be used for iMode:
BLACKONWHITE or STRETCH_ANDSCANS: if two or more elements need to be merged into one, StretchBlt performs a logical AND operation on the element. The result is that the color is white only when all the original elements are white. The actual meaning is that the black element controls the white element. This is suitable for white back scenes with black color.
Whiteonblack or STRETCH_ORSCANS: if two OR more elements need to be merged into one, StretchBlt performs the logical OR operation. The result is that the color is black only when all the original elements are black, that is, the color is determined by the white elements. This applies to a monochrome lattice map with a black background.
COLORONCOLOR or STRETCH_DELETESCANS: The StretchBlt simply removes the graph element rows or columns without any logical combinations. This is usually the best way to process a color dot matrix.
HALFTONE or STRETCH_HALFTONE: Windows calculates the average color of the Target Based on the combined source color. This will be used in conjunction with the semi-tuned color palette. Chapter 1 will show this program.
Using StretchBlt to narrow down an image may sometimes cause color distortion. The solution is as follows:

1. First set the target DC (that is, HDC hDestDC): SetStretchBltMode (hDestDC, HALFTONE); 2. Call: SetBrushOrgEx (hDestDC, 0, 0, NULL ); 3. Call the StretchBlt of CImage

For example:

img.Detach();img.Load(m_testinfo.m_StdPicutre);::SetStretchBltMode(*pDc, HALFTONE);::SetBrushOrgEx(*pDc, 0, 0, NULL);img.StretchBlt(*pDc, 20, 132, 140, 198, SRCCOPY);

Perfect solution!

 

Or solve the problem as follows:

     hbit = (HBITMAP)LoadImage( NULL,cBmpPath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);    if(hbit != NULL) {     Bitmap.Attach(hbit);     DCCompatible.CreateCompatibleDC(GetDC());     DCCompatible.SelectObject(&Bitmap);     Bitmap.GetObject(sizeof(bm),&bm);}

 

2. OnPaint ():

    pDC->SetStretchBltMode(HALFTONE);    pDC->StretchBlt(MAP_LEFT,MAP_TOP,MAP_WIDTH,MAP_HEIGHT,&DCCompatible,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

 

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.