首页 > 代码库 > 使用GDI+的RotateFlip方法 旋转图像并保存图片
使用GDI+的RotateFlip方法 旋转图像并保存图片
使用GDI+旋转图像并保存图片的方法,代码如下:
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 };
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 L‘J‘: 115 case L‘j‘: 116 encoder = L"image/jpeg"; 117 break; 118 case L‘B‘: 119 case L‘b‘: 120 encoder = L"image/bmp"; 121 break; 122 case L‘G‘: 123 case L‘g‘: 124 encoder = L"image/gif"; 125 break; 126 case L‘P‘: 127 case L‘p‘: 128 encoder = L"image/png"; 129 break; 130 case L‘T‘: 131 case L‘t‘: 132 encoder = L"image/tiff"; 133 break; 134 default: 135 encoder = L"image/jpeg"; 136 break; 137 } 138 return encoder; 139 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。