C#怎么改变图像的大小,比例?

 我来答
晁松兰展词
2019-12-10 · TA获得超过3.7万个赞
知道大有可为答主
回答量:1.2万
采纳率:25%
帮助的人:1038万
展开全部
使用缩略图的方法就可以实现图片的大小变换,具体的方法如下:
System.Drawing.Image
image
=
System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(SourceImagePath));
int
num
=
((ThumbnailImageWidth
/
4)
*
3);
int
width
=
image.Width;
int
height
=
image.Height;
//计算图片的比例
if
((((double)
width)
/
((double)
height))
>=
1.3333333333333333f)
{
num
=
((height
*
ThumbnailImageWidth)
/
width);
}
else
{
ThumbnailImageWidth
=
((width
*
num)
/
height);
}
if
((ThumbnailImageWidth
<
1)
||
(num
<
1))
{
return;
}
//用指定的大小和格式初始化
Bitmap
类的新实例
网管网www.bitscn.com
Bitmap
bitmap
=
new
Bitmap(ThumbnailImageWidth,
num,
PixelFormat.Format32bppArgb);
//从指定的
Image
对象创建新
Graphics
对象
Graphics
graphics
=
Graphics.FromImage(bitmap);
//清除整个绘图面并以透明背景色填充
graphics.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制
原图片
对象
graphics.DrawImage(image,
new
Rectangle(0,
0,
ThumbnailImageWidth,
num));
image.Dispose();
try
{
//将此
原图片
以指定格式并用指定的编解码参数保存到指定文件
string
savepath
=
(ThumbnailImagePath==null?SourceImagePath:ThumbnailImagePath);
SaveImage(bitmap,HttpContext.Current.Server.MapPath(savepath),GetCodecInfo((string)htmimes[sExt]));
}
catch(System.Exception
e)
{
throw
e;
}
finally
{
bitmap.Dispose();
graphics.Dispose();
}
}
#endregion
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式