关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享VC中使用GDI+在内存转换图片类型教程,希望对大家能有一点小小的帮助。
【 tulaoshi.com - 编程语言 】
VC中使用GDI+在内存转换图片类型
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)文章
详细代码如下:
BOOL MBmpToMImage(CMemFile& cbfBmp, CMemFile& cbfImage, CString strType)
{
 int iBmpSize = cbfBmp.GetLength();
 HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
 if (hMemBmp == NULL) return FALSE;
 IStream* pStmBmp = NULL;
 CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
 if (pStmBmp == NULL) 
 {
  GlobalFree(hMemBmp);
  return FALSE;
 }
 BYTE* pbyBmp = (BYTE *)GlobalLock(hMemBmp);
 cbfBmp.SeekToBegin();
 cbfBmp.Read(pbyBmp, iBmpSize);
 Image* imImage = NULL;
 imImage = Image::FromStream(pStmBmp, FALSE);
 if (imImage == NULL) 
 {
  GlobalUnlock(hMemBmp);
  GlobalFree(hMemBmp);
  return FALSE;
 }
 USES_CONVERSION;
 CLSID clImageClsid;
 GetImageCLSID(A2W("image/"+strType.GetBuffer(0)), &clImageClsid);
 HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0);
 if (hMemImage == NULL)
 {
  pStmBmp-Release();
  GlobalUnlock(hMemBmp);
  GlobalFree(hMemBmp);
  if (imImage != NULL) delete imImage;
  return FALSE;
 } 
 IStream* pStmImage = NULL;
 CreateStreamOnHGlobal(hMemImage, TRUE, &pStmImage);
 if (pStmImage == NULL)
 {
  pStmBmp-Release();
  GlobalUnlock(hMemBmp);
  GlobalFree(hMemBmp);
  GlobalFree(hMemImage);
  if (imImage != NULL) delete imImage
   return FALSE;
 } 
 imImage-Save(pStmImage, &clJpgClsid);
 if (pStmImage == NULL) 
 {
  pStmBmp-Release();
  pStmImageRelease();
  GlobalUnlock(hMemBmp);
  GlobalFree(hMemBmp);
  GlobalFree(hMemImage;
  if (imImage != NULL) delete imImage;
  return FALSE;
 }
 LARGE_INTEGER liBegin = {0};
 pStmImage-Seek(liBegin, STREAM_SEEK_SET, NULL);
 BYTE* pbyImage = (BYTE *)GlobalLock(hMemImage);
 cbfImage.SeekToBegin();
 cbfImage.Write(pbyImage, GlobalSize(hMemImage));
 if (imImage != NULL) delete imImage;
 pStmBmp-Release();
 pStmImage-Release();
 GlobalUnlock(hMemBmp);
 GlobalUnlock(hMemImage);
 GlobalFree(hMemBmp);
 GlobalFree(hMemImage);
 return TRUE;
} 
来源:http://www.tulaoshi.com/n/20160219/1622920.html
看过《VC中使用GDI+在内存转换图片类型》的人还看了以下文章 更多>>