gpt4 book ai didi

c# - 使用 MVC2 从 SQL 数据库动态调整图像大小

转载 作者:太空狗 更新时间:2023-10-29 23:59:07 24 4
gpt4 key购买 nike

我有一个简单的 MVC2 应用程序,可以将文件作为图像 blob 从浏览器上传到 MS SQL 数据库。

然后我可以返回类似这样的结果:

        public FileContentResult ShowPhoto(int id)
{
TemporaryImageUpload tempImageUpload = new TemporaryImageUpload();
tempImageUpload = _service.GetImageData(id) ?? null;
if (tempImageUpload != null)
{
byte[] byteArray = tempImageUpload.TempImageData;
return new FileContentResult (temp, "image/jpeg");
}
return null;
}

但我想返回这些调整为缩略图和画廊大小 View 的图像。在这个结果中可以这样做吗?我一直在玩很棒的 imageresizer.net,但它似乎想将图像存储在我想避免的服务器上。是否可以即时执行此操作?

我需要保留原始文件,如果可能的话,我不想将图像作为文件存储在服务器上。

谢谢指点!

最佳答案

ImageResizer.NET 允许您将流传递给它以调整大小,请参阅 Managed API usage

您将使用的方法是:

ImageResizer.ImageBuilder.Current.Build(对象源,对象目标,ResizeSettings设置)

我修改了你的方法,以这种方式进行,但未经测试。希望对您有所帮助。

public FileContentResult ShowPhoto(int id)
{
TemporaryImageUpload tempImageUpload = new TemporaryImageUpload();
tempImageUpload = _service.GetImageData(id) ?? null;
if (tempImageUpload != null)
{
byte[] byteArray = tempImageUpload.TempImageData;
using(var outStream = new MemoryStream()){
using(var inStream = new MemoryStream(byteArray)){
var settings = new ResizeSettings("maxwidth=200&maxheight=200");
ImageResizer.ImageBuilder.Current.Build(inStream, outStream, settings);
var outBytes = outStream.ToArray();
return new FileContentResult (outBytes, "image/jpeg");
}
}
}
return null;
}

关于c# - 使用 MVC2 从 SQL 数据库动态调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com