c# - 在上传时将图像转换为 png

标签 c# httppostedfilebase

当用户上传一张jpg/gif/bmp图片时,我希望将这张图片转为png图片,再转为base64字符串。

我一直在努力让它工作,但我真的遇到了困难,有人可以帮我吗?

我当前没有图像转换的代码如下:

public ActionResult UploadToBase64String(HttpPostedFileBase file)
        {

                var binaryData = new Byte[file.InputStream.Length];
                file.InputStream.Read(binaryData, 0, (int) file.InputStream.Length);
                file.InputStream.Seek(0, SeekOrigin.Begin);
                file.InputStream.Close();

                string base64String = Convert.ToBase64String(binaryData, 0, binaryData.Length);

...
}

最佳答案

你根本没有在那里转换它..你可以使用这样的东西:

using System.Drawing;

Bitmap b = (Bitmap)Bitmap.FromStream(file.InputStream);

using (MemoryStream ms = new MemoryStream()) {
    b.Save(ms, ImageFormat.Png);

    // use the memory stream to base64 encode..
}

关于c# - 在上传时将图像转换为 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294737/

相关文章:

httppostedfilebase - 在 SQL 中将 httppostedfilebase 保存为图像

forms - 在 MVC4 中上传,在我的操作中有 2 个参数但文件为空

asp.net-mvc - 在 Bootstrap 模式中使用时,MVC 中的文件上传返回 null

azure - 使用 WindowsAPICodePack 和 Shell 检索视频/音频持续时间

css - 字符串到 HttpPostedFilebase

c# - TPL Dataflow 被削减了吗?

c# - HttpClient : How to upload multiple files at once

C# - 在运行时更新 pictureBox.Image 导致错误

c# - 领域服务注入(inject)领域实体

c# - 林克 : How to store MD5 hash column in database