在 MVC3 中使用 C# 更改图像大小
本文关键字:图像 MVC3 | 更新日期: 2023-09-27 18:20:22
[HttpPost]
public ActionResult AddImage(Image model)
{
if (model.ImageData != null && model.ImageData.ContentLength > 0)
{
var fileName = Path.GetFileName(model.ImageData.FileName);
var pathBig = Path.Combine(Server.MapPath("~/UploadedImages"), fileName);
var pathSmall = Path.Combine(Server.MapPath("~/UploadedImages"), "small_" + fileName);
// --> How to change image size to big(800 x 600)
// and small (100x80) and save them?
model.ImageData.SaveAs(pathBig);
model.ImageData.SaveAs(pathSmall);
}
}
如何将图像大小更改为大(800 x 600(到小(100x80(并保存它们?
你可以试试这个库:http://nuget.org/packages/ImageResizer
它确实支持 asp.net-mvc:http://imageresizing.net/
或者,你可以获取一个纯 C# 库并在你的应用上使用它。请参阅这些帖子:
调整图像大小 C#
https://stackoverflow.com/a/2861813/368070
我找到的这个片段:http://snippets.dzone.com/posts/show/4336
从框架方法本身执行此操作的最简单方法是使用 Graphics 类的 DrawImage(( 方法。
示例代码可能如下所示:
//For first scale
Bitmap bmp = new Bitmap(800, 600);
Graphics gf = Graphics.FromImage(bmp);
Image userpic = Image.FromStream(/*pass here the image byte stream*/)
gf.DrawImage(userpic, new Rectangle(0,0,800,600))
gf.Save(/* the save path */);
//For second scale
Bitmap bmp = new Bitmap(100, 80);
Graphics gf = Graphics.FromImage(bmp);
Image userpic = Image.FromStream(/*pass here the image byte stream*/)
gf.DrawImage(userpic, new Rectangle(0,0,100,80))
gf.Save(/* the save path */);