更改图像的返回类型.net Core2

时间:2018-02-08 13:42:30

标签: asp.net-mvc asp.net-core

我有一个扩展方法的代码片段,我有一个带有返回类型画布的.net核心博客。

如何将返回类型指定给图像并将其显示在图像标签上。以下是我的代码。

而不是返回新的FileStreamResult(ms," image / jpg")

我想做这样的事情

ViewData [" Image"] = new FileStreamResult(ms," image / jpg")

然后将其分配给我视图中的图像占位符 感谢

                using (Image img = Image.FromStream(pic.OpenReadStream()))
            {
                // ResizeImageASPNETCore.Extends.Resize(img, 50, 50);
                Stream ms = new MemoryStream(img.Resize(500, 500).ToByteArray());
                Stream ms2 = new MemoryStream(img.Resize(50, 50).ToByteArray());

                return new FileStreamResult(ms, "image/jpg");`enter code here`

,扩展类是

        #endregion

        #region get resized bitmap
        var canvas = new Bitmap(width, height);

        using (var graphics = Graphics.FromImage(canvas))
        {
            graphics.CompositingQuality = CompositingQuality.HighSpeed;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.DrawImage(current, 0, 0, width, height);
        }

        return canvas;
        #endregion
    }

    public static byte[] ToByteArray(this Image current)
    {
        using (var stream = new MemoryStream())
        {
            current.Save(stream, current.RawFormat);
            return stream.ToArray();
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

一种方法是对图像进行base64编码并将src设置为该值。

检查出来:Embedding Base64 Images