前言
在項目遷移到 .net core 上面后,我們可以使用 System.Drawing.Common
組件來操作 Image,Bitmap 類型,實現(xiàn)生成驗證碼、二維碼,圖片操作等功能。System.Drawing.Common
組件它是依賴于 GDI+ 的,然后在 Linux 上并沒有 GDI+,面向谷歌編程之后發(fā)現(xiàn),Mono 團隊使用 C語言 實現(xiàn)了GDI+
接口,提供對非Windows系統(tǒng)的 GDI+ 接口訪問能力,這個應該就是libgdiplus
。所以想讓代碼在 linux 上穩(wěn)定運行有關 System.Drawing.Common
的代碼的時候,必須安裝組件libgdiplus
。而現(xiàn)在大多是 docker 進行發(fā)布,如果快速簡單的安裝 libgdiplus
?
安裝 libgdiplus
方案一
基于微軟提供的 mcr.microsoft.com/dotnet/core/aspnet:3.1
重新構(gòu)建一個帶libgdiplus
的鏡像,但是帶來的問題是,將來版本更新了,都得重新構(gòu)建一遍。當然寫腳本自動構(gòu)建,那就沒問題了。哈哈
方案二
這也是我目前采用的,構(gòu)建應用鏡像的時候安裝 libgdiplus
,但是因為系統(tǒng)鏡像源是國外,導致安裝 libgdiplus
十分緩慢,不忍直視。我們把系統(tǒng)包源地址修改成阿里云包源地址,問題就迎刃而解了。 參考 Dockerfile
如下:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
RUN sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list
RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
ARG PROJECT
WORKDIR /app
...
替換包源地址,注意哦,官方鏡像使用的是 debian
而不是 ubuntu
的源,一開始我一直以為 ubuntu
搞半天沒成功。
sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list
番外:缺少中文字體咋辦呢?好辦
除了遭遇以上問題外,還遇到了字體缺失,導致的生成圖片中有關中文字體全部是亂碼的情況,這里的中文是指我們通過程序自己畫上去的。對于這個問題嘛?缺啥補啥唄,缺字體補字體?;谏厦娴?Dockerfile
調(diào)整:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
RUN sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list
RUN apt-get update -y && apt-get install -y libgdiplus locales fontconfig && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
RUN sed -ie 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/g' /etc/locale.gen && locale-gen && mkdir /usr/share/fonts/truetype/deng/
ADD ./fonts/* /usr/share/fonts/truetype/deng/
RUN fc-cache -vf && fc-list
ENV LANG zh_CN.UTF-8
ARG PROJECT
WORKDIR /app
...
到此這篇關于Linux/Docker 中使用 System.Drawing.Common 踩坑記錄分享的文章就介紹到這了,更多相關linux docker 使用System.Drawing.Common內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!