我有一个扩展方法的代码片段,我有一个带有返回类型画布的.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();
}
}
}
}