请问如何用CFile创建一个bmp图像,并用write方法将数据写入到图像内呢? 请问如何用CFile创建一个bmp图像,并用write方法将数据写入到图像内呢?不知道如何用CFile创建图像文件。请指教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CFile只是创建个空壳子 bmp文件的文件头 像素数据全都自己写至于怎么创建文件 自己参考MSDN的例子 或者网上的例子 // 囧,刚才贴完一段代码,现在又要贴一次CClientDC srcDC(this); CPaintDC dc(this); CDC memDC; CRect rect; GetClientRect(rect); memDC.CreateCompatibleDC(&srcDC); CBitmap bm; int Width = rect.Width(); int Height = rect.Height(); bm.CreateCompatibleBitmap(&srcDC, Width, Height); CBitmap* pOld = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, Width, Height, &srcDC, 0, 0, SRCCOPY); memDC.SelectObject(pOld); BITMAP btm; bm.GetBitmap(&btm); DWORD size = btm.bmWidthBytes * btm.bmHeight; LPSTR lpData =(LPSTR)GlobalAlloc(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(dc,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; CFileDialog dlg(false,_T("BMP"),_T("*.bmp"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.*|*.*|")); if (dlg.DoModal()==IDOK) { CFile bf; CString ss=dlg.GetPathName(); if(bf.Open(ss, CFile::modeCreate | CFile::modeWrite)) { bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER)); bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER)); bf.WriteHuge(lpData, size); bf.Close(); } GlobalFree(lpData); } 时间控件内容的获取 vc++使用ADO访问数据库出错 谁来帮帮我啊,我都发了4帖啦! 请教一个关于循环的问题 ★★★★CString怎么样转换成char*★★★★ windows自带有几个视频压缩的代码,就是capDlgVideoCompression跳出来的视频压缩程序啊 请问怎样进入指定目录 在DLL中,使用了C++ 的类\overload 函数\默认参数的函数等特性后,那么此dll在VB\Delphi中使用会不会有问题呢? 可执行文件如何删掉自己? vc里怎样屏蔽ctl+alt+del和alt+tab?谢谢!!! 无语+抗议 如何捕获由Flash发到flash activex控件的消息
至于怎么创建文件 自己参考MSDN的例子 或者网上的例子
CClientDC srcDC(this);
CPaintDC dc(this);
CDC memDC;
CRect rect;
GetClientRect(rect);
memDC.CreateCompatibleDC(&srcDC);
CBitmap bm;
int Width = rect.Width();
int Height = rect.Height();
bm.CreateCompatibleBitmap(&srcDC, Width, Height);
CBitmap* pOld = memDC.SelectObject(&bm);
memDC.BitBlt(0, 0, Width, Height, &srcDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOld);
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size = btm.bmWidthBytes * btm.bmHeight;
LPSTR lpData =(LPSTR)GlobalAlloc(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(dc,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;
CFileDialog dlg(false,_T("BMP"),_T("*.bmp"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.*|*.*|"));
if (dlg.DoModal()==IDOK)
{
CFile bf;
CString ss=dlg.GetPathName();
if(bf.Open(ss, CFile::modeCreate | CFile::modeWrite))
{
bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData, size);
bf.Close();
}
GlobalFree(lpData);
}