~~我在对话框里有个picture用LoadImage()读入一个BMP,然后显示,问题在这里,因为BMP的尺寸比picture小,而我保存时是将整个pictureDC保存下来了,请问怎么才能按图片原来的大小保存这是我保存BMP的代码(是将整个picture DC保存的)望高手给出代码,谢谢。 CDC memDC;
CRect rect;
m_Dest.GetClientRect(rect);//m_Dest就是picture memDC.CreateCompatibleDC(m_Dest.GetDC());
CBitmap bm;
int Width = rect.Width();
int Height = rect.Height();
bm.CreateCompatibleBitmap(m_Dest.GetDC(), Width, Height);
CBitmap* pOld = memDC.SelectObject(&bm);
memDC.BitBlt(0, 0, Width, Height, m_Dest.GetDC(), 0, 0, SRCCOPY);
memDC.SelectObject(pOld);
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size = btm.bmWidthBytes * btm.bmHeight;
LPSTR lpData = (LPSTR)GlobalAllocPtr(GPTR, size);
BITMAPFILEHEADER bfh;
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount = btm.bmBitsPixel;
bih.biClrImportant = 0;
bih.biClrUsed = 0;
bih.biCompression = 0;
bih.biHeight = btm.bmHeight;
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = size;
bih.biWidth = btm.bmWidth;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
GetDIBits(m_Dest.GetDC()->GetSafeHdc(),bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
bfh.bfReserved1 = bfh.bfReserved2 = 0;
bfh.bfType = ((WORD)('M'<< 8)|'B');
bfh.bfSize = 54 + size;
bfh.bfOffBits = 54;
CFile bf;
if(bf.Open(lpFilePath, CFile::modeCreate | CFile::modeWrite))
{
bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData, size);
bf.Close();
}
GlobalFreePtr(lpData);
CRect rect;
m_Dest.GetClientRect(rect);//m_Dest就是picture memDC.CreateCompatibleDC(m_Dest.GetDC());
CBitmap bm;
int Width = rect.Width();
int Height = rect.Height();
bm.CreateCompatibleBitmap(m_Dest.GetDC(), Width, Height);
CBitmap* pOld = memDC.SelectObject(&bm);
memDC.BitBlt(0, 0, Width, Height, m_Dest.GetDC(), 0, 0, SRCCOPY);
memDC.SelectObject(pOld);
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size = btm.bmWidthBytes * btm.bmHeight;
LPSTR lpData = (LPSTR)GlobalAllocPtr(GPTR, size);
BITMAPFILEHEADER bfh;
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount = btm.bmBitsPixel;
bih.biClrImportant = 0;
bih.biClrUsed = 0;
bih.biCompression = 0;
bih.biHeight = btm.bmHeight;
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = size;
bih.biWidth = btm.bmWidth;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
GetDIBits(m_Dest.GetDC()->GetSafeHdc(),bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
bfh.bfReserved1 = bfh.bfReserved2 = 0;
bfh.bfType = ((WORD)('M'<< 8)|'B');
bfh.bfSize = 54 + size;
bfh.bfOffBits = 54;
CFile bf;
if(bf.Open(lpFilePath, CFile::modeCreate | CFile::modeWrite))
{
bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData, size);
bf.Close();
}
GlobalFreePtr(lpData);
解决方案 »
- MFC中如何在其他源文件中控制各个控件
- 菜鸟求助 MFC自动生成的构造函数,怎么看不明白
- Watch窗口出现:CXX0069: Error: variable needs stack frame是怎么了?
- 我解码得到了RGB32的数据buf,现在我要把它显示出来我怎么做啊,我想用directDraw!
- 有关ComboBox默认值的问题
- 求捉弄人的小代码,屏幕剧烈抖动等等。
- 把一个长度不固定的数组分成5段
- 怎么用MFC开启笔记本摄像头并保存
- 关于对话框的问题?
- 在VC中怎么样将位图进行移动
- 请大虾相助:怎样实现字符串型CString向无符号字符数组型unsigned char[]的转换?
- 刚开始学 COM
CBitmap b;
BITMAP bm;
hbmp = (HBITMAP)LoadImage(NULL, _T(lpFilePath), IMAGE_BITMAP,
0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
b.Attach(hbmp); b.GetBitmap(&bm);
CDC memDC;
CRect rect;
m_Dest.GetClientRect(rect);//m_Dest就是picture这几句话里面的rect使用bm里面的宽度高度代替就行了
2:为内存兼容DC选择合适大小的位图
3:使用Bitblt把屏幕上合适位置的一块区域拷贝到内存DC中
4:把位图替换下来
5:保存位图