gpt4 book ai didi

c# - 将图像转换为不同格式后,应用程序不断返回空文件

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:34 25 4
gpt4 key购买 nike

我正在尝试制作一个 MVC 应用程序,让客户端上传图像,将图像转换为不同的格式(在本例中为 .tif)并将其返回给客户端。

我遇到的问题是图像始终为 0kB。我似乎无法弄清楚问题所在。

View :

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype="multipart/form-data" }))
{
<input accept=".bmp, .gif, .jpg, .jpeg" name="file" type="file" />
<input type="submit" value="Submit" />
}

Controller Action :

    [HttpPost]
[ActionName("Index")]
public ActionResult UploadImages(HttpPostedFileBase file)
{
var tiffEncoder = ImageCodecInfo.GetImageEncoders()
.SingleOrDefault(dec => dec.FormatID == ImageFormat.Tiff.Guid);
Stream imgStream = new MemoryStream();

using (EncoderParameters encParams = new EncoderParameters(1))
using (Image img = Image.FromStream(file.InputStream, true, true))
{
encParams.Param[0] = new EncoderParameter(Encoder.Quality, 100);
img.Save(imgStream, tiffEncoder, encParams);
}
var result = File(imgStream, "image/tiff", "image.tif");
return result;
}

经过调试,我清楚地看到 FileStream 属性包含数据。

enter image description here

最佳答案

imgStream 写入后需要倒带。在将其传递到 File

之前设置 imgStream.Position = 0;

关于c# - 将图像转换为不同格式后,应用程序不断返回空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922722/

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