Chinaunix首页 | 论坛 | 博客
  • 博客访问: 145877
  • 博文数量: 124
  • 博客积分: 70
  • 博客等级: 民兵
  • 技术积分: 1745
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 13:49
文章分类

全部博文(124)

文章存档

2011年(55)

2010年(14)

2009年(30)

2008年(25)

我的朋友

分类: WINDOWS

2008-07-07 17:54:09

 static bool GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
 {
  using namespace Gdiplus;
  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 false;  // Failure
  pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  if(pImageCodecInfo == NULL)
   return false;  // 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 true;  // Success
   }   
  }
  free(pImageCodecInfo);
  return false;  // Failure
 }
 enum PIC_TYPE
 {
  BMP_TYPE = 0,
  JPG_TYPE,
  GIF_TYPE,
  PNG_TYPE,
  ILLEGAL_TYPE
 };
 static bool SaveThumbNailPic(const WCHAR* original_file_name, const WCHAR * dest_file_name, unsigned thumbnail_width, unsigned thumbnail_height, const PIC_TYPE& pic_type)
 {
  using namespace Gdiplus;
  Graphics *pGraphic = NULL;
  Image *pImage = NULL;
  bool bResult = false;
  /*
   gif格式的图片如果先取缩略图(GetThumbnailImage),然后再SavePic转换格式,会造成背景颜色随机的问题,很多时候
   背景颜色是黑色的,造成联系人列表上效果非常差
   这里先转换了格式,最后再取缩率图
  */
  try
  {
   // lmzhang 2008-07-06
   // 先将图片画到白色背景上
   
   Image image(original_file_name);
   ULONG x = image.GetWidth();
   ULONG y = image.GetHeight();     
   Bitmap bmp(x,y);
   pGraphic  = Graphics::FromImage(&bmp);
   if(!pGraphic)
   {
    throw 1;
   }
   // Create a Pen object.
   //Pen bluePen(Color(255,255,255,255));
   SolidBrush whiteBrush(Color(255,255,255,255));

   // Create a Rect object that bounds the ellipse.
   Rect ellipseRect(0, 0, x, y);
   pGraphic->FillRectangle(&whiteBrush, ellipseRect);
   // 将图片画到
   pGraphic->DrawImage(&image,
    RectF(0.0f,0.0f, (REAL)x, (REAL)y),
    0.0f,0.0f,(REAL)x, (REAL)y,
    UnitPixel,
    NULL);
   pImage = bmp.GetThumbnailImage(thumbnail_width, thumbnail_height);
   if(pImage)
   {
    bResult = SavePic(*pImage, dest_file_name, pic_type);
   }
   //
   
  /* CString tmpFile = dest_file_name + CString(".tmp");
   bool bResult = SavePic(image, tmpFile, pic_type);
   if (bResult)
   {
    Image image(tmpFile);
    Image *pImage = image.GetThumbnailImage(thumbnail_width, thumbnail_height);
    if(pImage)
    {
     SavePic(*pImage, dest_file_name, pic_type);
     delete pImage;
    }
   }

   ::DeleteFile(tmpFile);*/
  }
  catch(...)
  {
   ATLASSERT(FALSE);   
  }
  if(pGraphic)
  {
   delete pGraphic;
   pGraphic = NULL;
  }
  if(pImage)
  {
   delete pImage;
   pImage = NULL;
  }
  return bResult;
 }
 // lmzhang 2007-12-17
 // 将指定的图片存储成制定宽高的灰度图象,这儿的灰度由矩阵定义,暂时不允许用户调整
 static bool SaveGrayPic(const WCHAR* original_file_name, const WCHAR * dest_file_name, unsigned thumbnail_width, unsigned thumbnail_height, const PIC_TYPE& pic_type)
 {
  using namespace Gdiplus;
  
  Image *pImage = NULL;
  bool bResult = true;
  Graphics *pGraphic = NULL;
  try
  {
   Image image(original_file_name);
   ULONG x = image.GetWidth();
   ULONG y = image.GetHeight();  
   Bitmap bmp(x,y);
   // 经典灰度矩阵
   ColorMatrix cm= {0.3f, 0.3f, 0.3f, 0, 0,
    0.59f,0.59f,0.59f,0, 0,
    0.11f,0.11f,0.11f,0, 0,
    0,    0,    0,    1, 0,
    0,    0,    0,    0, 1};
   ImageAttributes ia;
   ia.SetColorMatrix(&cm);
   pGraphic  = Graphics::FromImage(&bmp);
   if(!pGraphic)
   {
    bResult = false;
    goto exit;
   }
   // 将图片画到
   pGraphic->DrawImage(&image,
    RectF(0.0f,0.0f, (REAL)x, (REAL)y),
    0.0f,0.0f,(REAL)x, (REAL)y,
    UnitPixel,
    &ia);
   if( (thumbnail_width == 0 ) ||
    (thumbnail_height == 0))
   {
    thumbnail_width = x;
    thumbnail_height = y;
   }
   pImage = bmp.GetThumbnailImage(thumbnail_width, thumbnail_height);
   if(pImage)
   {
    bResult = SavePic(*pImage, dest_file_name, pic_type);
   }
  }
  catch(...)
  {
   ATLASSERT(FALSE);
   bResult = false;
  }
exit:
  if(pGraphic)
  {
   delete pGraphic;
   pGraphic = NULL;
  }
  if(pImage)
  {
   delete pImage;
   pImage = NULL;
  }
  return bResult;
 }
 // end
 static bool SavePic(Gdiplus::Image& image, const WCHAR* filename, const PIC_TYPE& pic_type)
 {
  using namespace Gdiplus;
  CLSID encoderClsid;
  const WCHAR *encoder_format = 0;
  switch (pic_type)
  {
  case BMP_TYPE:
   {
    encoder_format = L"image/bmp";
    break;
   }
  case JPG_TYPE:
   {
    encoder_format = L"image/jpeg";
    break;
   }
  case GIF_TYPE:
   {
    encoder_format = L"image/gif";
    break;
   }
  case PNG_TYPE:
   {
    encoder_format = L"image/png";
    break;
   }
  case ILLEGAL_TYPE:
   {
    const int ILLEGAL_PIC_TYPE = 0;
    ALISOFT_IM_MESSAGE_ASSERT(_T("ILLEGAL_PIC_TYPE"), ILLEGAL_PIC_TYPE);
    return false;
   }
  }
  if (!GetEncoderClsid(encoder_format, &encoderClsid))
   return false;
  EncoderParameters params;
  params.Count = 1;
  EncoderParameter ¶m = params.Parameter[0];
  param.NumberOfValues = 1;
  param.Guid = EncoderQuality;
  param.Type = EncoderParameterValueTypeLong;
  long quality = 100;
  param.Value = &quality;
  
  return (image.Save(filename, &encoderClsid, ¶ms) == Ok);
 }
 
阅读(951) | 评论(0) | 转发(0) |
0

上一篇:.Net 日记

下一篇:side-by-side

给主人留下些什么吧!~~