在VC中怎样用GDI+缩放图片文件?

blackmango 2005-04-04 09:27:38
如题。例如我有一张2032×1354的JPEG照片a.jpg,有什么方法可以直接把它缩放为75%大小的图片b.jpg呢?我知道使用Graphics的DrawImage方法可以实现stretch,只是那是在窗口中显示的;如果我想在一个Console程序里面直接实现转换该怎么来呢?
...全文
535 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackmango 2005-04-05
  • 打赏
  • 举报
回复
我找到一个解决的方法啦:通过位图来转换
//wRatio hRatio分别为Width和Height调整的百分比(%)
int 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/100;
int dstHeight=srcHeight*hRatio/100;
// 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.
LPWSTR lpExt=PathFindExtensionW(lpSrcFile);
lpExt++;
LPWSTR lpEncoder;
switch(*lpExt)
{
case L'J':
case L'j':
lpEncoder=L"image/jpeg";
break;
case L'P':
case L'p':
lpEncoder=L"image/png";
break;
case L'B':
case L'b':
lpEncoder=L"image/bmp";
break;
case L'G':
case L'g':
lpEncoder=L"image/gif";
break;
case L't':
case L'T':
lpEncoder=L"image/tiff";
break;
default:
lpEncoder=L"image/jpeg";
}

CLSID imgClsid;
//GetEncoderClsid()函数是在MSDN中的例子
GetEncoderClsid(lpEncoder, &imgClsid);
bitmap.Save(lpDstFile,&imgClsid,NULL);

return 0;
}
速马 2005-04-04
  • 打赏
  • 举报
回复
可以用Graphics.FromImage来获得Graphics对象

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧