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);