在MSDN中有如下语句
An application can select a bitmap into memory device contexts only and into only one memory device context at a time. The format of the bitmap must either be monochrome or compatible with the device context; if it is not, SelectObject returns an error
位图必须是单色的或者和设备一致,问题出来了,我怎样才能使之达到一致?比如我用LoadImage或者LoadBitmap加载了一个资源,但这个位图就是跟我的设备的格式不一致,比如一个打印机的设备dc,我并不知道该dc到底是什么格式,我应该怎么办才能符合dc要求?
100分相送解决立刻送分
An application can select a bitmap into memory device contexts only and into only one memory device context at a time. The format of the bitmap must either be monochrome or compatible with the device context; if it is not, SelectObject returns an error
位图必须是单色的或者和设备一致,问题出来了,我怎样才能使之达到一致?比如我用LoadImage或者LoadBitmap加载了一个资源,但这个位图就是跟我的设备的格式不一致,比如一个打印机的设备dc,我并不知道该dc到底是什么格式,我应该怎么办才能符合dc要求?
100分相送解决立刻送分
/*
//打印或者在屏幕上画位图
//pDC 打印机或者屏幕dc指针
iLogPixelX
iLogPixelY
屏幕DC的GetDeviceCaps(LOGPIXELSX)值,其中
iLogPixelX=DC.GetDeviceCaps(LOGPIXELSX);
iLogPixelY=DC.GetDeviceCaps(LOGPIXELSY);
strFileName BMP文件
*/
void DrawBMP(CDC* pDC,int iLogPixelX,int iLogPixelY,const char *strFileName)
{
CDC MemDC; // 内存设备环境指针,在视的整个存在过程都将存在
CBitmap Bitmap,*pOldBmp;
CRect Source, Dest; // 记录源位图尺寸和最终显示尺寸
BITMAP bm;
if(MemDC.GetSafeHdc() == NULL)
{
HBITMAP hbitmap=(HBITMAP)LoadImage(0,strFileName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
Bitmap.Attach(hbitmap);
MemDC.CreateCompatibleDC(pDC);
Bitmap.GetObject(sizeof(bm),&bm);
pOldBmp=MemDC.SelectObject(&Bitmap);
Source.top=0;
Source.left=0;
Source.right= bm.bmWidth;
Source.bottom = bm.bmHeight;
Dest = Source;
}
pDC->DPtoLP(&Dest);
if(pDC->IsPrinting())
{
Dest.left=(int)(Dest.left*((double)pDC->GetDeviceCaps(LOGPIXELSX))/iLogPixelX);
Dest.right=(int)(Dest.right*((double)pDC->GetDeviceCaps(LOGPIXELSX))/iLogPixelX);
Dest.top=(int)(Dest.top*((double)pDC->GetDeviceCaps(LOGPIXELSY))/iLogPixelY);
Dest.bottom=(int)(Dest.bottom*((double)pDC->GetDeviceCaps(LOGPIXELSY))/iLogPixelY);
}
pDC->StretchBlt(Dest.left, Dest.top, Dest.right, Dest.bottom,
&MemDC, Source.left, Source.top, Source.right,Source.bottom, SRCCOPY);
MemDC.SelectObject(pOldBmp);
Bitmap.DeleteObject();
MemDC.DeleteDC();
return;
}