blog

枪手亨利

博客园 首页 新随笔 联系 订阅 管理

#include <Stdio.h>
#include <Objbase.h>
#include <Windows.h>
#include <Gdiplus.h>
#include <GdiPlusEnums.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus")
// Helper functions
int GetCodecClsid(const WCHAR*, CLSID*);

int main()
{
 CLSID              codecClsid;
 EncoderParameters  encoderParameters;
 long               quality;
 Status             stat;
 GdiplusStartupInput gdiplusStartupInput;
 ULONG gdiplusToken;
 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
 {
  // Get an image from the disk.
  Image image(L"F:\\ddd.bmp");
  
  // Get the CLSID of the JPEG codec.
  GetCodecClsid(L"image/jpeg", &codecClsid);
  
  // Before we call Image::Save, we must initialize an
  // EncoderParameters object. The EncoderParameters object
  // has an array of EncoderParameter objects. In this
  // case, there is only one EncoderParameter object in the array.
  // The one EncoderParameter object has an array of values.
  // In this case, there is only one value (of type LONG)
  // in the array. We will set this value to 0, 50, and 100.
  
  encoderParameters.Count = 1;
  encoderParameters.Parameter[0].Guid = EncoderQuality;
  encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  encoderParameters.Parameter[0].NumberOfValues = 1;
  
  // Save the image as a JPEG with quality level 0.
  quality = 0;
  encoderParameters.Parameter[0].Value = &quality;
  stat = image.Save(L"Shapes001.jpg", &codecClsid, &encoderParameters);
  
  if(stat == Ok)
   wprintf(L"%s saved successfully.\n", L"Shapes001.jpg");
  else
   wprintf(L"%d  Attempt to save %s failed.\n", stat, L"Shapes001.jpg");
  
  // Save the image as a JPEG with quality level 50.
  quality = 50;
  encoderParameters.Parameter[0].Value = &quality;
  stat = image.Save(L"Shapes050.jpg", &codecClsid, &encoderParameters);
  
  if(stat == Ok)
   wprintf(L"%s saved successfully.\n", L"Shapes050.jpg");
  else
   wprintf(L"%d  Attempt to save %s failed.\n", stat, L"Shapes050.jpg");
  
  // Save the image as a JPEG with quality level 100.
  quality = 100;
  encoderParameters.Parameter[0].Value = &quality;
  stat = image.Save(L"Shapes100.jpg", &codecClsid, &encoderParameters);
  
  if(stat == Ok)
   wprintf(L"%s saved successfully.\n", L"Shapes100.jpg");
  else
   wprintf(L"%d  Attempt to save %s failed.\n", stat, L"Shapes100.jpg");
 }
 GdiplusShutdown(gdiplusToken);
 return 0;
} // main
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

   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(int j = 0; j < num; ++j)
   {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         return j;  // Success
      }   
   } // for

   return -1;  // Failure

} // GetCodecClsid
//download gdiplus.dll
//http://www.microsoft.com/downloads/release.asp?releaseid=32738
//Platform SDK Redistributable: GDI+ RTM


//下面另外一个例子程序,都是masterz写的

#include <Stdio.h>
#include <Objbase.h>
#include <Windows.h>
#include <Gdiplus.h>
#include <GdiPlusEnums.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus")
int GetCodecClsid(const WCHAR*, CLSID*);
HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{
    ULONG cCharacters;
    DWORD dwError;
    if (NULL == pszA)
    {
        *ppszW = NULL;
        return NOERROR;
    }
    cCharacters =  strlen(pszA)+1;
    *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
    if (NULL == *ppszW)
        return E_OUTOFMEMORY;
    if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,*ppszW, cCharacters))
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszW);
        *ppszW = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }
    return NOERROR;
}

int main(int argc, char* argv[])
{
 int n_ret=0;
 printf("%s can convert jpg, png, gif, tiff to gif file.\n if it works, it is written by masterz, otherwise I don't know who write it\n",argv[0]);
 if(argc!=3)
 {
  printf("usage:%s gif_filename source_image_filename\n",argv[0]);
  return -1;
 }
 GdiplusStartupInput gdiplusStartupInput;
 ULONG gdiplusToken;
 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
 {
  CLSID              codecClsid;
  Status             stat;
  EncoderParameters  encoderParameters;

  GetCodecClsid(L"image/gif", &codecClsid);  
  encoderParameters.Count = 0;  
  LPOLESTR bstr_src_img;
  LPOLESTR bstr_dst_fn;
  AnsiToUnicode(argv[1],&bstr_dst_fn);
  AnsiToUnicode(argv[2],&bstr_src_img);
  Image image(bstr_src_img);
  stat =image.Save(bstr_dst_fn, &codecClsid, &encoderParameters);  
  if(stat == Ok)
  {
   n_ret=0;
   wprintf(L"%s saved successfully.\n",bstr_dst_fn);
  }
  else
  {
   n_ret=-2;
   wprintf(L"%d  Attempt to save %s failed.\n", stat, bstr_dst_fn);
  }
  CoTaskMemFree(bstr_dst_fn);
  CoTaskMemFree(bstr_src_img);
 }
 GdiplusShutdown(gdiplusToken);
 return 0;
} // main
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

   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
//in order to compile this program, you need to download microsoft platform sdk from http://www.microsoft.com/msdownload/platformsdk/sdkupdate/downlevel.htm
//also download gdiplus.dll and put it at the same directory as the executable.
//http://www.microsoft.com/downloads/release.asp?releaseid=32738
//Platform SDK Redistributable: GDI+ RTM

//sample usage: img2gif a.gif src.bmp



二、
编程技巧与维护第5期有算法和源程序!!!
http://www.comprg.com.cn/2002source/05.zip


三、
(1) JPEG文件编码.把bitmap 格式压缩成JPEG格式:
http://www.copathway.com/vchelp/view_article.asp?ft=2&article_id=269
(参考:http://www.copathway.com/vchelp/type.asp?class_id=1&type_id=18)

(2) http://www.vckbase.com/code    非常好的代码
jpeglib.zip可以显示JPEG文件,进行JPEG和BMP之间的相互转换,并可以保存为灰度JPG图像
http://www.vckbase.com/code/listcode.asp?mclsid=7&sclsid=715 ¥¥¥¥¥
    1、有没有方法创建一个半透明的窗口,并将该窗口上发生的所有鼠标事件都传递到桌面或另一个应用窗口处理?

    2、我正在写一个幻灯显示程序,该程序要显示JPEG图像序列。我使用了 2002年三月刊专栏文章中的 CPicture 类来绘制图像(参见:C++ Q&A: Do You Have a License for that GIF? PreSubclassWindow, EOF in MFC, and More)。那个程序运行得很好。但我现在想添加从某一张图像到下一张图像的渐变特性。我在网页中用转换效果可以做到。那么是否有办法从程序代码中实现图像渐变特性?

[代码性质] VC完整应用程序代码
[代码作者] Paul DiLascia
[文件大小] 504K
[更新日期] 2005-11-24 9:36:00
[下载次数] 1087 下载

(3) 可以使用GDI+类库提供的类和函数来处理位图,包括未压缩的以及JPEG,GIF等压缩格式。

具体的讲,主要用到的类是Bitmap及其父类Image。可以使用静态方法Bitmap::FromHBITMAP()从一个HBitmap生成GDI+中的Bitmap对象,然后可以用Image::Save()方法将位图存为文件或者流,保存时可以制定Encoder的类型,包括"image/bmp","image/jpeg","image/gif"等等。

详细地说明、注意事项以及例子代码参见:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/ucodecs_7qg5.asp

posted on 2005-12-19 16:09  henry  阅读(1281)  评论(0)    收藏  举报