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

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

问题描述

我正在尝试使用 .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(string filename, bool useEmbeddedColorManagement)

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

文件路径是正确的,我已经两次和三次检查文件是否存在.

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' inSystem.Drawing.Common.dll: '图像格式未知.'堆栈跟踪:

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

在 System.Drawing.Image.InitializeFromStream(Stream 流)在 System.Drawing.Bitmap..ctor(Stream stream)

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

推荐答案

虽然我仍然不确定为什么 Image.FromFile 和使用 Bitmap 不起作用,但我设法通过使用 ImageSharp 来自 SixLabors.

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天全站免登陆