Serialization of the Image class

Source: Internet
Author: User

Using the image class provided by the GDI + graphics device interface We can easily display pictures of file formats such as JPEG, GIF, and so on, but the drawback is that the image class does not provide the ability to serialize data, if you want to move from JPEG, The image data that is read in the GIF format file is saved to the application document that we developed, and we can read the saved picture data from the document and display it correctly, we must implement the serialization of the image class data. If you have a member pointer variable m_pimage to the image class in your document class that is used to display the picture, then add the following code to the serialization function serialize (carchive& AR) of the document class to achieve m_ Serialization of the picture data that the pimage points to:if (AR. IsStoring ())
{
Hglobal M_hmem = GlobalAlloc (gmem_moveable, 0);
IStream *pstm=null;
CreateStreamOnHGlobal (M_hmem, TRUE, &pstm);
CLSID clsid;
Uses_conversion;
Getcodecclsid (a2w ("Image/bmp"), &clsid);
M_pimage->save (Pstm,&clsid,null);
if (pstm==null)
Return
Large_integer libeggining = {0};
Pstm->seek (libeggining, Stream_seek_set, NULL);
DWORD wtemp=globalsize (M_HMEM);
Lpbyte lpdata = (lpbyte) globallock (M_HMEM);
Ar << wtemp;
Ar. Write (lpdata,wtemp);
Pstm->release ();
GlobalUnlock (M_HMEM);
}
Else
{
DWORD wtemp;
Ar >> wtemp;
Hglobal M_hmem = GlobalAlloc (gmem_fixed, wtemp);
if (M_hmem = NULL)
Return
IStream *pstm=null;
CreateStreamOnHGlobal (m_hmem,false,&pstm);
if (pstm==null)
Return
byte* Pmem = (byte*) globallock (M_HMEM);
Ar. Read (pmem,wtemp);
if (m_pimage) {
Delete M_pimage;
M_pimage = NULL;
}
using namespace Gdiplus;
M_pimage = Image::fromstream (pstm, FALSE);
Pstm->release ();
GlobalUnlock (M_HMEM);
Note that GlobalFree (M_HMEM) cannot be added here, otherwise the picture will not show up.
The implementation code for the function Getcodecclsid is as follows:int GetCodecClsid(const WCHAR* format, CLSID* pClsid)
{
  UINT num = 0;     // number of image encoders
  UINT size = 0;     // size of the image encoder array in bytes
  using namespace Gdiplus;
  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;
     return j; // Success
   }  
  } // for
  return -1; // Failure
} // GetCodecClsid
Parameter format is used to illustrate the format of the image data, you can take the following values: "Image/bmp" (bitmap format), "Image/jpeg" (JPEG format), "Image/gif" (gif format) and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.