码迷,mamicode.com
首页 > 其他好文 > 详细

使用GDI+的RotateFlip方法 旋转图像并保存图片

时间:2014-05-07 18:18:32      阅读:1323      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

使用GDI+旋转图像并保存图片的方法,代码如下:

bubuko.com,布布扣
 1 #pragma once
 2 class CGDIImageFile
 3 {
 4 public:
 5     CGDIImageFile(void);
 6     ~CGDIImageFile(void);
 7 
 8     String TypeToFormat(TCHAR cType);
 9     bool GetImageCLSID(const TCHAR* mformat,CLSID *pClsid);
10 
11     void CreateRotateImage(String& sSrcFile, int nArc, String sDesFile = L"");
12     ULONG_PTR gdiplusToken;
13 };
GDIImageFile.h
bubuko.com,布布扣
  1 #include "StdAfx.h"
  2 #include "GDIImageFile.h"
  3 
  4 #include <GdiPlus.h>
  5 using namespace Gdiplus;
  6 #pragma comment(lib,"Gdiplus.lib")
  7 
  8 CGDIImageFile::CGDIImageFile(void)
  9 {
 10     GdiplusStartupInput gdiplus;
 11     GdiplusStartup(&gdiplusToken,&gdiplus,NULL);
 12 }
 13 
 14 
 15 CGDIImageFile::~CGDIImageFile(void)
 16 {
 17     GdiplusShutdown(gdiplusToken);
 18 }
 19 
 20 void CGDIImageFile::CreateRotateImage(String& sSrcFile, int nArc, String sDesFile)
 21 {
 22     Image *srcImg= new Image(sSrcFile.c_str());
 23 
 24     int nWidth = srcImg->GetWidth();
 25     int nHeight = srcImg->GetHeight();
 26         //重点在此
 27     switch(nArc)
 28     {    
 29     case 90:
 30         {
 31             srcImg->RotateFlip(Rotate270FlipXY);
 32             nWidth = srcImg->GetHeight();
 33             nHeight = srcImg->GetWidth();
 34             break;
 35         }
 36     case 180:
 37         {
 38             srcImg->RotateFlip(RotateNoneFlipX);
 39             break;
 40         }
 41     case 270:
 42         {
 43             srcImg->RotateFlip(Rotate90FlipXY);
 44             nWidth = srcImg->GetHeight();
 45             nHeight = srcImg->GetWidth();
 46             break;
 47         }
 48     default:break;
 49     }
 50 
 51     Graphics imgGrahpics(srcImg);
 52     Bitmap bitmap(nWidth, nHeight, &imgGrahpics);//注意高度和宽度
 53     Graphics bmpGraphics(&bitmap);
 54     bmpGraphics.DrawImage(srcImg, 0, 0);
 55     int nPos = sSrcFile.find(L".");
 56     if (nPos == -1)
 57         return;
 58 
 59     String encoder;
 60     String tmpstr = sSrcFile.substr(nPos + 1,nPos + 2);
 61     TCHAR tmpchar = tmpstr[0];
 62 
 63     encoder = TypeToFormat(tmpchar);
 64 
 65     CLSID   imgClsid; 
 66     //GetEncoderClsid()函数是在MSDN中的例子 
 67     if (!GetImageCLSID(encoder.c_str(),&imgClsid))
 68     {
 69         return ;
 70     }
 71     if(sDesFile == L"")
 72         sDesFile = sSrcFile + L"_rotate";
 73     bitmap.Save(sDesFile.c_str(),&imgClsid,NULL);     
 74     delete srcImg;
 75 }
 76 
 77 bool CGDIImageFile::GetImageCLSID(const TCHAR* mformat,CLSID *pClsid)
 78 {
 79     UINT num = 0;
 80     UINT size = 0;
 81 
 82     ImageCodecInfo *pImageCodecInfo = NULL;
 83     GetImageEncodersSize(&num,&size);
 84     if (size == 0)
 85     {
 86         return false;
 87     }
 88     pImageCodecInfo = new ImageCodecInfo[size];
 89     if (pImageCodecInfo == NULL)
 90     {
 91         return false;
 92     }
 93 
 94     GetImageEncoders(num,size,pImageCodecInfo);
 95 
 96     for(UINT i = 0;i < num; i++)
 97     {
 98         if (wcscmp(pImageCodecInfo[i].MimeType,mformat) == 0)
 99         {
100             *pClsid = pImageCodecInfo[i].Clsid;
101             delete[] pImageCodecInfo;
102             return true;
103         }
104     }
105     delete[] pImageCodecInfo;
106     return false;
107 }
108 
109 String CGDIImageFile::TypeToFormat(TCHAR cType)
110 {
111     String encoder;
112     switch(cType)
113     {
114     case LJ:
115     case Lj:
116         encoder = L"image/jpeg";
117         break;
118     case LB:
119     case Lb:
120         encoder = L"image/bmp";
121         break;
122     case LG:
123     case Lg:
124         encoder = L"image/gif";
125         break;
126     case LP:
127     case Lp:
128         encoder = L"image/png";
129         break;
130     case LT:
131     case Lt:
132         encoder = L"image/tiff";
133         break;
134     default:
135         encoder = L"image/jpeg";
136         break;
137     }
138 return encoder;
139 }
GDIImageFile.cpp

 

使用GDI+的RotateFlip方法 旋转图像并保存图片,布布扣,bubuko.com

使用GDI+的RotateFlip方法 旋转图像并保存图片

标签:des   style   blog   class   code   tar   

原文地址:http://www.cnblogs.com/cgbaby/p/3709896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!