图片格式未知 [英] Image format Unknown

查看:52
本文介绍了图片格式未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.Net Core 2.2将图像读取为Base64格式,并且会引发

I am trying to read an image into Base64 format using .Net Core 2.2 and it throws

ArgumentException:图片格式未知.System.Drawing.Image.FromFile(字符串文件名,布尔useEmbeddedColorManagement)

ArgumentException: Image format is unknown. System.Drawing.Image.FromFile(string filename, bool useEmbeddedColorManagement)

filePath是正确的,我已经对文件进行了两次和三次检查.

The filePath is correct, and I have double and triple checked that the file exists.

我尝试用大写和小写扩展名命名我的PNG.

I have attempted naming my PNGs with both upper and lowercase extension.

这不是在Windows上本地发生的,但是一旦将应用程序部署到Linux上就会发生.有问题的代码如下.

This does not happen locally on windows, but once the application is deployed to linux is happens. The code in question is below.

    public static string ImageToBase64(string filePath)
    {
        System.Diagnostics.Debug.WriteLine($"File Path for Image: {filePath}");
        string base64String;
        using (var image = Image.FromFile(Path.GetFullPath(filePath)))
        {
            using (var ms = new MemoryStream())
            {
                image.Save(ms, image.RawFormat);
                image.Dispose();
                byte[] imageBytes = ms.ToArray();
                base64String = Convert.ToBase64String(imageBytes);
                imageBytes = null;
                ms.Dispose();
            }
        }
        GC.Collect();
        return base64String;
    }

编辑

我还尝试过使用带有位图的文件流.

I also tried using a filestream with bitmap.

    public static string ImageToBase64(string filePath)
    {
        string base64String;
        using(var fs = new FileStream(filePath,FileMode.Open))
        using (var image = new Bitmap(fs))
        {
            using (var ms = new MemoryStream())
            {
                image.Save(ms, ImageFormat.Png);
                image.Dispose();
                byte[] imageBytes = ms.ToArray();
                base64String = Convert.ToBase64String(imageBytes);
                imageBytes = null;
                ms.Dispose();
            }
        }
        GC.Collect();
        return base64String;
    }

这将引发以下异常:

抛出异常:"System.ArgumentException"System.Drawing.Common.dll:'图像格式未知.堆栈跟踪:

Exception thrown: 'System.ArgumentException' in System.Drawing.Common.dll: 'Image format is unknown.' Stack trace:

在System.Drawing.Image.InitializeFromStream(流流)在System.Drawing.Bitmap..ctor(流流)处

at System.Drawing.Image.InitializeFromStream(Stream stream) at System.Drawing.Bitmap..ctor(Stream stream)

推荐答案

虽然我仍然不确定为什么Image.FromFile和使用Bitmap无法正常工作,但我设法通过使用

While I am still unsure of why Image.FromFile and using Bitmap didn't work, I managed to get it working by using ImageSharp from SixLabors.

工作代码如下.

    public static string ImageToBase64(string filePath)
    {
        string base64String;
        using (var image = SixLabors.ImageSharp.Image.Load(filePath))
        {
            using (var ms = new MemoryStream())
            {
                image.Save(ms, new PngEncoder());
                image.Dispose();
                byte[] imageBytes = ms.ToArray();
                base64String = Convert.ToBase64String(imageBytes);
                imageBytes = null;
                ms.Dispose();
            }
        }
        GC.Collect();
        return base64String;
    }

这篇关于图片格式未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆