netcore在CentOS7 下使用處理圖片的問題

請看代碼,當你在centos下要把圖片轉爲Base64的時候html

 1             MemoryStream ms = new MemoryStream();
 2             try
 3             {
 4                 Bitmap bmp = new Bitmap(filePath);
 5                 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 6 
 7                 byte[] arr = new byte[ms.Length];
 8                 ms.Position = 0;
 9                 ms.Read(arr, 0, (int)ms.Length);
10                 return Convert.ToBase64String(arr);
11             }
12             catch (Exception ex)
13             {
14                 Core.Helpers.Log4NetHelper.WriteError(typeof(string),ex);
15                 return "";
16             }
17             finally
18             {
19                 ms.Close();
20             }

明明你在本地或windows服務器是正常的,但是在linux下就會拋出以下異常:linux

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libgdiplus': The specified module could not be found.
   at System.Runtime.InteropServices.FunctionWrapper`1.get_Delegate()
   at System.Drawing.SafeNativeMethods.Gdip.GdiplusStartup(IntPtr& token, StartupInput& input, StartupOutput& output)
   at System.Drawing.SafeNativeMethods.Gdip..cctor()
   --- End of inner exception stack trace ---
   at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(String filename, IntPtr& bitmap)
   at System.Drawing.Bitmap..ctor(String filename, Boolean useIcm)

看了報錯信息知道 linux沒有 libgdipluswindows

解決方案:centos

#locate libdl
#cd /usr/lib64 #ln -s libdl-2.17.so libdl.so

若是 locate libdl 報如下錯誤:
-bash: locate: command not found

其緣由是沒有安裝mlocate這個包bash

安裝一下包:#yum  -y install mlocate服務器

再更新一下庫:#updatedbapp

最後最關鍵的是安裝一下 libgdiplus 庫,搞定!spa

 

yum install libgdiplus-devel
相關文章
相關標籤/搜索