StretchBlt giving monochrome bitmap as output

461 views Asked by At

I m writing code for scaling down the image in VC++. When i save destination bitmap it gives monochrome image. Code:

int iNewWidth = 400;
int iNewHeight = 500;
CImage image;
HRESULT rs=image.Load(_T("E:\\input.jpg"));
int a=10;
CDC destDC;
int res=destDC.CreateCompatibleDC(NULL);
HDC hdcDest=HDC(destDC);
HBITMAP hDestBitmap=CreateCompatibleBitmap(hdcDest, iNewWidth, iNewHeight);
HBITMAP hOldBitmap=(HBITMAP)SelectObject(hdcDest,hDestBitmap);
SetStretchBltMode(hdcDest,BLACKONWHITE);
BOOL bl=image.StretchBlt(hdcDest,0, 0, iNewWidth, iNewHeight, 0, 0, image.GetWidth(), image.GetHeight(), SRCERASE);
HRESULT res2;
CImage new_image;
new_image.Attach(hDestBitmap);
res2=new_image.Save(_T("E:\\NewImage.jpg"));
HBITMAP hb=new_image.Detach();
ReleaseDC(NULL,hdcDest);

I will be very thankful if someone help me here. Thanks in advance

1

There are 1 answers

0
Mark Ransom On

When you create a DC it initially has a monochrome bitmap selected into it. When you use CreateCompatibleBitmap with that DC, it uses the characteristics of that selected bitmap - i.e. it creates another monochrome bitmap.

Use the DC returned by GetDC(NULL) in your CreateCompatibleBitmap call.