.net 6.0图片转Base64部署到Linux系统上报The type initializer for ‘Gdip‘ threw an exception

3 篇文章 0 订阅
2 篇文章 0 订阅

在业务当中需要将图片文件转为Base64:windows上可以运行正常执行,部署到Linux系统上报The type initializer for ‘Gdip‘ threw an exception

图片转Base64代码如下

/// <summary>
/// 图片转为base64编码的文本
/// </summary>
/// <param name="ImageFileName"></param>
/// <returns></returns>
public static string ImgPathNameToBase64String(string ImageFileName)
{
    try
    {
        Bitmap bmp = new Bitmap(ImageFileName);//仅在 Windows 上支持 System.Drawing.Common。Linux系统会报错

        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] arr = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(arr, 0, (int)ms.Length);
        ms.Close();
        String strBase64 = Convert.ToBase64String(arr);
        return strBase64;
    }
    catch (Exception ex)
    {
        Console.WriteLine("图片转为base64编码的文本》异常:::" + ex);
        return "";
    }
}

/// <summary>
/// 图片转为base64编码的文本
/// </summary>
/// <param name="ImageFileName"></param>
/// <returns></returns>
public static string ImgToBase64String(Stream stream)
{
    try
    {
        Bitmap bmp = new Bitmap(stream);//仅在 Windows 上支持 System.Drawing.Common。Linux系统会报错

        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] arr = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(arr, 0, (int)ms.Length);
        ms.Close();
        String strBase64 = Convert.ToBase64String(arr);
        return strBase64;
    }
    catch (Exception ex)
    {
        Console.WriteLine("图片转为base64编码的文本》异常:::" + ex);
        return "";
    }
}

搜索引擎上的文章解决方案:https://blog.csdn.net/u010476739/article/details/124317037
在这里插入图片描述
此文章中的问题与我的问题是相似的,由于我使用的是docker部署,我没有使用此解决方案;因为docker 容器与虚拟机很相似,容器类似于虚拟机,只是容器不是完整的操作系统,容器通常只包含必要的操作系统包和应用程序,这是它们的轻量级。不可能每次部署都往docker容器当中安装一些包,这样即增加部署时常(网络不好的情况下甚至会部署失败),还违反了容器的轻量级。如果各位网友不是使用容器化部署的话可以尝试使用此方案

在微软官方:https://learn.microsoft.com/zh-cn/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only

和另一个文章:https://blog.csdn.net/IT_rookie_newbie/article/details/126559363

这里表明System.Drawing.Common仅在 Windows 上支持
在这里插入图片描述
而我的方法当中有Bitmap bmp = new Bitmap() 是System.Drawing.Common类库的
在这里插入图片描述
Microsoft建议是替换类库
在这里插入图片描述
于是我换成FileStream fs = new FileStream() 到此问题解决了,代码如下

/// <summary>
/// 图片文件转换成Base64字符串
/// </summary>
/// <param name="fileName">文件绝对路径</param>
/// <returns></returns>
public static String ImgFileToBase64(string fileName)
{
    try
    {
        FileStream fs = new FileStream(fileName, FileMode.Open);
        byte[] bt = new byte[fs.Length];
        fs.Read(bt, 0, bt.Length);
        string strBase64 = Convert.ToBase64String(bt);
        fs.Close();
        return strBase64;
    }
    catch (Exception ex)
    {
        Console.WriteLine("图片文件转换成Base64字符串》异常:::" + ex);
        return "";
    }
}

!!!记录遇到的bug问题,为遇到类似问题,可查看文章,寻找排查问题解决思路

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值