编程语言
首页 > 编程语言> > c# – 在上传时将图像转换为png

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

作者:互联网

当用户上传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,httppostedfilebase
来源: https://codeday.me/bug/20190713/1450164.html