CDC memDC;
CBitmap oBitmap;
memDC.CreateCompatibleDC(NULL);
m_hDC = memDC.GetSafeHdc();//全局的,
// create compatible, correctly sized bitmap
oBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight);
// select our bitmap into the device context
CBitmap * oldbm = memDC.SelectObject(&oBitmap);
// turn area white - stock black bitmap is selected
memDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS);
// draw bitmap into memory device context
DrawBitmap();//画图m_hDC是全局;画图是对的./* BITMAP bmp;
oBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
file.Open("c:\\barcode.bmp",CFile::modeCreate|CFile::modeReadWrite);
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh, sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih, sizeof(BITMAPINFOHEADER));//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
int i=GetDIBits(memDC.m_hDC, (HBITMAP)oBitmap.m_hObject, 0, m_nPixelHeight, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.Write(p, bmp.bmWidthBytes * bmp.bmHeight);//写入位图数据
delete [] p;
file.Close();
*/
// put bitmap on clipboard
::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, oBitmap.m_hObject);
::CloseClipboard();
// deselect object out of device context
memDC.SelectObject(oldbm);
// make sure bitmap not deleted with CBitmap object oBitmap.Detach();
return; 这样不能保存在memDC上画的图. 但是剪切板中有图,贴图到微软画笔中正常的.
而中间保存BMP的代码可以用于下面抓屏的
void Screen(CString str)
{
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
FILE *fp = fopen(str, "w+b");
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据
delete [] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
}
CBitmap oBitmap;
memDC.CreateCompatibleDC(NULL);
m_hDC = memDC.GetSafeHdc();//全局的,
// create compatible, correctly sized bitmap
oBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight);
// select our bitmap into the device context
CBitmap * oldbm = memDC.SelectObject(&oBitmap);
// turn area white - stock black bitmap is selected
memDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS);
// draw bitmap into memory device context
DrawBitmap();//画图m_hDC是全局;画图是对的./* BITMAP bmp;
oBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
file.Open("c:\\barcode.bmp",CFile::modeCreate|CFile::modeReadWrite);
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh, sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih, sizeof(BITMAPINFOHEADER));//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
int i=GetDIBits(memDC.m_hDC, (HBITMAP)oBitmap.m_hObject, 0, m_nPixelHeight, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.Write(p, bmp.bmWidthBytes * bmp.bmHeight);//写入位图数据
delete [] p;
file.Close();
*/
// put bitmap on clipboard
::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, oBitmap.m_hObject);
::CloseClipboard();
// deselect object out of device context
memDC.SelectObject(oldbm);
// make sure bitmap not deleted with CBitmap object oBitmap.Detach();
return; 这样不能保存在memDC上画的图. 但是剪切板中有图,贴图到微软画笔中正常的.
而中间保存BMP的代码可以用于下面抓屏的
void Screen(CString str)
{
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
FILE *fp = fopen(str, "w+b");
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据
delete [] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
}
解决方案 »
- GetDlgItem(IDC_EDIT1)->SetWindowText("1");
- 新手opengl编程,MFC窗口屏幕闪烁问题。
- 套接字库加载问题?
- 用MFC 实现HTTP POST,GET ,COOKIE功能
- 在VC中用ADO操作数据库,执行recordset.open时间超长,在pl/sql里执行同一语句很快,什么原因?
- 请问如何去除txt文件里的所有非中文字符(包括英文和符号)
- 怎么能让工具栏多行显示
- vc如何生成dll能够让所有的语言都能使用
- //我用VB6。0,但有个关于菜单的问题在VB论坛中迟迟没有解决,懂SDK的朋友可能感觉会很容易///
- 怎样从鼠标位置得到其下的窗口句柄?
- 浮点数比较的问题
- 求教,如何隐藏掉控件本身的滚动条,换上我定制的滚动条
bitmap 要和具体设备关联才可以
http://www.codeproject.com/bitmap/drawing2bitmap.asp