跪求一个保存位图的程序MFC程序.请不要用GlobalAlloc/lock/free等函数.

Air_sky 2009-03-04 10:14:51
小弟最近学习MFC显示位图,然后对位图进行处理后,想保存起来.
大体步骤知道,就是先设置文件头,写文件头,写信息头,写位图数据.
我写得程序可以把图像保存,图像无法在ACDSEE中打开.但是在我显示位图的程序中确能显示出来 .
我几进崩溃啦,怎么整也整不好.请高手指教.
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenqinwuhai 2009-03-05
  • 打赏
  • 举报
回复
GDI+
byxdaz 2009-03-05
  • 打赏
  • 举报
回复
//位图句柄转换图像

void WriteBMPFile(HBITMAP hBitMap, LPTSTR filename, HDC hDC)

{

BITMAP bmp;

PBITMAPINFO pbmi;

WORD cClrBits;

HANDLE hf; // file handle

BITMAPFILEHEADER hdr; // bitmap file-header

PBITMAPINFOHEADER pbih; // bitmap info-header

LPBYTE lpBits; // memory pointer

DWORD dwTotal; // total count of bytes

DWORD cb; // incremental count of bytes

BYTE *hp; // byte pointer

DWORD dwTmp;



// create the bitmapinfo header information



if (!GetObject( hBitMap, sizeof(BITMAP), (LPTSTR)&bmp))

{

return;

}



// Convert the color format to a count of bits.

cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);

if (cClrBits == 1)

cClrBits = 1;

else if (cClrBits <= 4)

cClrBits = 4;

else if (cClrBits <= 8)

cClrBits = 8;

else if (cClrBits <= 16)

cClrBits = 16;

else if (cClrBits <= 24)

cClrBits = 24;

else cClrBits = 32;





// Allocate memory for the BITMAPINFO structure.

if (cClrBits != 24)

pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits));

else

pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));



// Initialize the fields in the BITMAPINFO structure.



pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

pbmi->bmiHeader.biWidth = bmp.bmWidth;

pbmi->bmiHeader.biHeight = bmp.bmHeight;

pbmi->bmiHeader.biPlanes = bmp.bmPlanes;

pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;

if (cClrBits < 24)

pbmi->bmiHeader.biClrUsed = (1<<cClrBits);



// If the bitmap is not compressed, set the BI_RGB flag.

pbmi->bmiHeader.biCompression = BI_RGB;



// Compute the number of bytes in the array of color

// indices and store the result in biSizeImage.

pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8 * pbmi->bmiHeader.biHeight * cClrBits;

// Set biClrImportant to 0, indicating that all of the

// device colors are important.

pbmi->bmiHeader.biClrImportant = 0;



// now open file and save the data

pbih = (PBITMAPINFOHEADER) pbmi;

lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);



if (!lpBits)

{

return;

}



// Retrieve the color table (RGBQUAD array) and the bits

if (!GetDIBits(hDC, HBITMAP(hBitMap), 0, (WORD) pbih->biHeight, lpBits, pbmi, DIB_RGB_COLORS))

{

return;

}



// Create the .BMP file.

hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0,

NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,

(HANDLE) NULL);

if (hf == INVALID_HANDLE_VALUE)

{

return;

}

hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"

// Compute the size of the entire file.

hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed

* sizeof(RGBQUAD) + pbih->biSizeImage);

hdr.bfReserved1 = 0;

hdr.bfReserved2 = 0;



// Compute the offset to the array of color indices.

hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +

pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);



// Copy the BITMAPFILEHEADER into the .BMP file.

if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL))

{

return;

}



// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.

if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL)))

{

return;

}



// Copy the array of color indices into the .BMP file.

dwTotal = cb = pbih->biSizeImage;

hp = lpBits;

if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))

{

return;

}



// Close the .BMP file.

if (!CloseHandle(hf))

{

return;

}



// Free memory.

GlobalFree((HGLOBAL)lpBits);

}



实例:

void CTESTView::OnDraw(CDC* pDC)

{

CTESTDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

CRect rect;

GetClientRect(&rect);

CDC ImageDC;

ImageDC.CreateCompatibleDC(pDC);

CBitmap bmp,*oldbmp;

bmp.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());

oldbmp = ImageDC.SelectObject(&bmp);

ImageDC.FillSolidRect(rect, pDC->GetBkColor());

ImageDC.MoveTo(10, 10);

ImageDC.LineTo(300, 300);

WriteBMPFile((HBITMAP)bmp.GetSafeHandle(),"C:\\1.bmp",ImageDC.m_hDC);

pDC->BitBlt(0,0,rect.Width(),rect.Height(),&ImageDC,0,0,SRCCOPY);

ImageDC.SelectObject(oldbmp);

ImageDC.DeleteDC();

}
Air_sky 2009-03-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mxw0922 的回复:]
把邮箱给我给你发个实例源码。里面附带很多图像处理算法,可参考一下。
[/Quote]
wtrunwolf@gmail.com,谢谢关注
mxw0922 2009-03-05
  • 打赏
  • 举报
回复
发了
谭建新 2009-03-04
  • 打赏
  • 举报
回复
cximage 类库
mxw0922 2009-03-04
  • 打赏
  • 举报
回复
把邮箱给我给你发个实例源码。里面附带很多图像处理算法,可参考一下。
uuio1984 2009-03-04
  • 打赏
  • 举报
回复
软件杀手,能给我一个么? uuio1984@126.com 谢谢
代码下载地址: https://pan.quark.cn/s/a4b39357ea24 在Android系统上达成蓝牙与便携式打印机的连接,通常包含了一系列技术要素和操作流程。名为"Android连接蓝牙打印机(cpcl指令)Demo完整版"的项目提供了一个全面的解决方案,它运用了专门的打印机指令集——CPCL(Control Program for ClearType Language),该指令集主要用于操控打印作业,尤其适用于标签和面单的打印。以下是关于此主题的详尽阐述: 1. **蓝牙传输打印**:Android平台内置了`BluetoothAdapter`类别用于管理蓝牙数据交互。必须确认设备已启用蓝牙功能,随后对邻近的蓝牙设备进行探测。`startDiscovery()`函数负责启动搜寻过程,而`getBondedDevices()`能够列出已建立配对的设备。 2. **建立与便携打印机的联系**:当识别出目标打印机后,可借助`BluetoothDevice`类中的`createRfcommSocketToServiceRecord()`函数构建RFCOMM(蓝牙串行端口协议)的通道。一旦连接建立成功,便可通过`connect()`函数与打印机展开信息交流。 3. **CPCL编码**:CPCL是一种与ESC/POS类似的页面描述性语言,它用于调控打印机的输出格式、文字样式、条形码等。比如,`^PJL`用于设定页面布局,`^FO`指定输出位置,`^A`设定字体尺寸,`^BC`生成条形码,`^PG`执行分页操作。在Android程序中,需将CPCL编码转为文本格式,再经由蓝牙通道传输至打印机。 4. **博思达软件开发工具包**:博思达作为一家著名的打印...

19,464

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧