HBITMAP到JPEG / PNG,在C ++中没有CImage [英] HBITMAP to JPEG /PNG without CImage in C++

查看:525
本文介绍了HBITMAP到JPEG / PNG,在C ++中没有CImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HBITMAP,我想保存为JPEG / PNG流或字节数组。问题是我使用mingw作为我的编译器,所以我不能使用CImage ..这将使我的生活更轻松。

I've got a HBITMAP that I want to save into a JPEG/PNG stream or array of bytes. The problem is that I'm using mingw as my compiler so I can't use CImage.. which would have made my life easier.

我可以得到像素位图没有任何问题,但我不知道如何以JPEG / PNG格式访问他们。

I can get the pixels from the bitmap without any problems, but I have no idea how to get access to them in JPEG/PNG-format.

我从哪里开始?

推荐答案

如果您访问DirectX库,您可以使用IStream将您的图像转换为JPEG
http://msdn.microsoft.com/en-us/library/windows/desktop /aa380034(v=vs.85).aspx

If you have access DirectX library you may use IStream to convert your image to JPEG http://msdn.microsoft.com/en-us/library/windows/desktop/aa380034(v=vs.85).aspx

或者如果您有GDI +这样的工作可能会奏效

or if you have GDI+ something like this might work

Gdiplus::Bitmap bmp(hbmpImage,(HPALETTE)0);
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bmp.Save(L"D:\image.png",&pngClsid,NULL);

GetEncoderLCLsid看起来像这样
int GetEncoderClsid(const WCHAR * format,CLSID * pClsid)
{
UINT num = 0; //图像编码器编号
UINT size = 0; //以像素为单位的图像编码器数组大小

GetEncoderLCLsid looks like this int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = 0; // number of image encoders UINT size = 0; // size of the image encoder array in bytes

   ImageCodecInfo* pImageCodecInfo = NULL;

   GetImageEncodersSize(&num, &size);
   if(size == 0)
      return -1;  // Failure

   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL)
      return -1;  // Failure

   GetImageEncoders(num, size, pImageCodecInfo);

   for(UINT j = 0; j < num; ++j)
   {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }    
   }

   free(pImageCodecInfo);
   return -1;  // Failure
}

不要忘记初始化GDI +

don't forget to initialize GDI+

 GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

如果没有访问权限,你可以使用libjpeg,但是你需要把所有的依赖包从GnuWin32网站。在这个页面中的代码应该工作得更快,只是忘记了提升

if don't have access to either you may use libjpeg but you need to put all the dependencies packages from the GnuWin32 site. Much faster the code in this page should work, just forget about the boost

libjpeg没有消息就消失

这篇关于HBITMAP到JPEG / PNG,在C ++中没有CImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆