GDI+调整图像文件尺寸大小
2017-05-18 10:46
260 查看
stdafx.h
#include "gdiplus.h"
using namespace Gdiplus;
#pragma comment( lib, "gdiplus.lib" )
class CImageResizeApp : public CWinApp
{
public:
CImageResizeApp();
GdiplusStartupInput m_Gdistart;
ULONG_PTR m_GdiplusToken;
BOOL CImageResizeApp::InitInstance()
{
GdiplusStartup(&m_GdiplusToken,&m_Gdistart,NULL);
}
int CImageResizeApp::ExitInstance()
{
// TODO: 在此添加专用代码和/或调用基类
GdiplusShutdown(m_GdiplusToken);
return CWinApp::ExitInstance();
}
void ResizePicture(LPCWSTR lpSrcFile,LPCWSTR lpDstFile,int wRatio,int hRatio)
{
Image srcImg(lpSrcFile);
int srcWidth=srcImg.GetWidth();
int srcHeight=srcImg.GetHeight();
//计算调整后的Width和Height
int dstWidth=srcWidth*wRatio;
int dstHeight=srcHeight*hRatio;
// Construct a Graphics object based on the image.
Graphics imgGraphics(&srcImg);
Bitmap bitmap(dstWidth,dstHeight,&imgGraphics);
Graphics bmpGraphics(&bitmap);
bmpGraphics.DrawImage(&srcImg,0,0,dstWidth,dstHeight);
// Save the altered image.
const wchar_t* lpExt=wcsrchr(lpSrcFile, '.');
if(!lpExt)
return;
lpExt++;
LPWSTR lpEncoder;
switch(*lpExt)
{
case 'J ':
case 'j ':
lpEncoder=L"image/jpeg ";
break;
case 'P ':
case 'p ':
lpEncoder=L"image/png ";
break;
case 'B ':
case 'b ':
lpEncoder=L"image/bmp ";
break;
case 'G ':
case 'g ':
lpEncoder=L"image/gif ";
break;
case 't ':
case 'T ':
lpEncoder=L"image/tiff ";
break;
default:
lpEncoder=L"image/jpeg ";
}
CLSID imgClsid;
GetEncoderClsid(lpEncoder, &imgClsid);
bitmap.Save(lpDstFile,&imgClsid,NULL);
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num= 0;
UINT size= 0;
ImageCodecInfo* pImageCodecInfo= NULL;
GetImageEncodersSize(&num, &size);
if(size== 0)
{
return -1;
}
pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo== NULL)
{
return -1;
}
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j=0; j< num; ++j)
{
//if(wcsstr(pImageCodecInfo[j].MimeType, format))
{
*pClsid= pImageCodecInfo[1].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
call ResizePicture(L"QQ2.png",L"test.png",2,2);
#include "gdiplus.h"
using namespace Gdiplus;
#pragma comment( lib, "gdiplus.lib" )
class CImageResizeApp : public CWinApp
{
public:
CImageResizeApp();
GdiplusStartupInput m_Gdistart;
ULONG_PTR m_GdiplusToken;
BOOL CImageResizeApp::InitInstance()
{
GdiplusStartup(&m_GdiplusToken,&m_Gdistart,NULL);
}
int CImageResizeApp::ExitInstance()
{
// TODO: 在此添加专用代码和/或调用基类
GdiplusShutdown(m_GdiplusToken);
return CWinApp::ExitInstance();
}
void ResizePicture(LPCWSTR lpSrcFile,LPCWSTR lpDstFile,int wRatio,int hRatio)
{
Image srcImg(lpSrcFile);
int srcWidth=srcImg.GetWidth();
int srcHeight=srcImg.GetHeight();
//计算调整后的Width和Height
int dstWidth=srcWidth*wRatio;
int dstHeight=srcHeight*hRatio;
// Construct a Graphics object based on the image.
Graphics imgGraphics(&srcImg);
Bitmap bitmap(dstWidth,dstHeight,&imgGraphics);
Graphics bmpGraphics(&bitmap);
bmpGraphics.DrawImage(&srcImg,0,0,dstWidth,dstHeight);
// Save the altered image.
const wchar_t* lpExt=wcsrchr(lpSrcFile, '.');
if(!lpExt)
return;
lpExt++;
LPWSTR lpEncoder;
switch(*lpExt)
{
case 'J ':
case 'j ':
lpEncoder=L"image/jpeg ";
break;
case 'P ':
case 'p ':
lpEncoder=L"image/png ";
break;
case 'B ':
case 'b ':
lpEncoder=L"image/bmp ";
break;
case 'G ':
case 'g ':
lpEncoder=L"image/gif ";
break;
case 't ':
case 'T ':
lpEncoder=L"image/tiff ";
break;
default:
lpEncoder=L"image/jpeg ";
}
CLSID imgClsid;
GetEncoderClsid(lpEncoder, &imgClsid);
bitmap.Save(lpDstFile,&imgClsid,NULL);
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num= 0;
UINT size= 0;
ImageCodecInfo* pImageCodecInfo= NULL;
GetImageEncodersSize(&num, &size);
if(size== 0)
{
return -1;
}
pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo== NULL)
{
return -1;
}
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j=0; j< num; ++j)
{
//if(wcsstr(pImageCodecInfo[j].MimeType, format))
{
*pClsid= pImageCodecInfo[1].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
call ResizePicture(L"QQ2.png",L"test.png",2,2);
相关文章推荐
- 获得图片文件的图像尺寸大小
- 【Matlab学习笔记】【编程实例】一(将两幅图像调整为相同的尺寸大小)
- 获得图片文件的图像尺寸大小(支持GIF,JPG,BMP格式)
- 再学 GDI+[87]: TGPImage(7) - 调整图像大小
- 图像文件的大小并返回图像的尺寸以及文件类型
- Image Resizer for Windows资源管理器直接使用右键调整一个或多个图像文件的尺寸
- GDI+ 调整图像的大小
- CxImage在VC2005中更改图片的尺寸(调整控件与图像一样尺寸)
- 调整视图控制器xib文件中标准视图的尺寸
- 计算图像文件大小
- 如何调整图像大小
- c# 压缩图像文件大小
- GDI+ 在Delphi程序的应用 -- 线性调整图像亮度
- 再学 GDI+[92]: TGPImage(12) - 调整图像透明度
- OpenCV/第5章 图像尺寸调整
- 使用VMware Converter调整虚拟磁盘文件大小
- ASP.NET上传图片,服务器端验证,宽度,高度,文件类型,尺寸,文件大小,图片上传,图片格式检查
- Ext3 和 ext4 文件系统在线调整大小内幕
- GDI+ 在Delphi程序的应用 -- 调整图像亮度
- 笔记:把界面上静态文本控件的尺寸根据文本的内容自动调整大小