【问题标题】:Changing image size from stream从流中更改图像大小
【发布时间】:2015-05-01 13:48:25
【问题描述】:

我在这里搜索过这方面的帮助,但没有什么能完全符合我的需要。我有一张要上传的图片,我想在将其保存为 azure 之前更改其大小。

所以目前我的代码是:

public ActionResult UserDetails(HttpPostedFileBase photo)
    {     var inputFile = new Photo()
            {
                FileName = photo.FileName,                    
                Data = () => photo.InputStream
            };
//then I save to Azure

例如,我如何将 photo.InputStream 更改为 100x 100 像素?

【问题讨论】:

  • 到目前为止你尝试过什么?有 大量 在线教程用于在 C# 中调整图像大小。
  • 我已经能够使用 WebImage 更改图像大小,但是我只有图像作为 WebImage 对象,我无法将其保存为:Data= () => WebImageObject.... . 不起作用

标签: c# azure model-view-controller


【解决方案1】:

这是我的做法:

byte[] imageBytes; 

//Of course image bytes is set to the bytearray of your image      

using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
    {
        using (Image img = Image.FromStream(ms))
        {
            int h = 100;
            int w = 100;

            using (Bitmap b = new Bitmap(img, new Size(w,h)))
            {
                using (MemoryStream ms2 = new MemoryStream())
                {
                    b.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageBytes = ms2.ToArray();
                }
            }
        }                        
    }    

从那里,我使用MemoryStream 上传。我使用 blob 存储并使用 UploadFromStreamAsync 加载到 blob。

这是它的基本视图。

【讨论】:

  • 有没有一种方法可以在不保存到磁盘的情况下转换图像格式?比如在内存流中改变格式,然后保存到数据库中。
  • @sairfan 我写这篇文章已经有一段时间了,但是看看代码,看起来保存的是 ms2 对象,它是一个 MemoryStream。我尚未测试将其转换为不同的 ImageFormat,但我想您可以将格式更改为此处找到的另一种受支持的 ImageFormat:docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
相关资源
最近更新 更多