简体   繁体   English

在MVC3中使用C#更改图像大小?

[英]Change image size with C# in MVC3?

[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);
   }
}

How do I change the image size to big(800 x 600) to small (100x80) and save them? 如何将图像大小更改为大(800 x 600)到小(100x80)并保存?

You could try this library: http://nuget.org/packages/ImageResizer 你可以尝试这个库: http//nuget.org/packages/ImageResizer

It does support asp.net-mvc: http://imageresizing.net/ 它确实支持asp.net-mvc: http//imageresizing.net/

Or you could get a pure C# lib and use it on your app. 或者你可以得到一个纯粹的C#lib并在你的应用程序上使用它。 See these posts: 看这些帖子:
Resize an Image C# 调整图像大小C#
https://stackoverflow.com/a/2861813/368070 https://stackoverflow.com/a/2861813/368070

And this snippet I found : http://snippets.dzone.com/posts/show/4336 我发现这个片段: http//snippets.dzone.com/posts/show/4336

The simplest way of doing it from the framework methods itself will be to use the DrawImage() method of the Graphics Class. 从框架方法本身执行此操作的最简单方法是使用Graphics类的DrawImage()方法。

The example code could be like: 示例代码可能是:

//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 */);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM