Using System.Drawing.Common in Linux/Docker

Using System.Drawing.Common in Linux/Docker

Preface

After the project is migrated to .net core, we can use the System.Drawing.Common component to operate Image and Bitmap types to generate verification codes, QR codes, image operations and other functions. The System.Drawing.Common component depends on GDI+, but there is no GDI+ on Linux. After programming for Google, I found that the Mono team implemented the GDI+ interface in C language, providing access to the GDI+ interface for non-Windows systems. This should be libgdiplus . Therefore, if you want to run the System.Drawing.Common code stably on Linux, you must install the component libgdiplus . Now most of them are released by docker. How to install libgdiplus quickly and easily?

Install libgdiplus

Solution 1

Rebuild an image with libgdiplus based on mcr.microsoft.com/dotnet/core/aspnet:3.1 provided by Microsoft, but the problem is that it will have to be rebuilt again if the version is updated in the future. Of course, if you write a script to automatically build it, then there will be no problem. Ha ha

Solution 2

This is also what I am currently using. I install libgdiplus when building the application image. However, because the system image source is from abroad, the installation of libgdiplus is very slow and unbearable. We changed the system package source address to the Alibaba Cloud package source address, and the problem was solved. The reference Dockerfile is as follows:

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
...

Replace the package source address. Please note that the official image uses the source of debian instead of ubuntu . At first, I thought it was ubuntu and I failed for a long time.

sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list

Extra: What to do if Chinese fonts are missing? Easy

In addition to the above problems, we also encountered missing fonts, which resulted in all the Chinese fonts in the generated pictures being garbled. The Chinese here refers to the ones we drew ourselves through the program. What about this question? Just make up for what you lack. If you lack fonts, just make up for the fonts. Based on the above Dockerfile adjustment:

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
...

This is the end of this article about sharing the pitfalls of using System.Drawing.Common in Linux/Docker. For more relevant content about using System.Drawing.Common in linux docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Summary of commonly used tool functions in Vue projects

>>:  Brief analysis of mysql scheduled backup tasks

Recommend

Using zabbix to monitor the ogg process (Linux platform)

The ogg process of a database produced some time ...

Get a list of your top 10 most frequently used terminal commands in Linux

I think the commands I use most often are: Choice...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Vue3 list interface data display details

Table of contents 1. List interface display examp...

Example method of deploying react project on nginx

Test project: react-demo Clone your react-demo pr...

How to position the header at the top using CSS sticky layout

Application scenarios: One of the new requirement...

How to use Docker to build OpenLDAP+phpLDAPadmin unified user authentication

1. Background Use LDAP to centrally manage operat...

Mac node deletion and reinstallation case study

Mac node delete and reinstall delete node -v sudo...

Linux centOS installation JDK and Tomcat tutorial

First download JDK. Here we use jdk-8u181-linux-x...

Analysis of Facebook's Information Architecture

<br />Original: http://uicom.net/blog/?p=762...

js memory leak scenarios, how to monitor and analyze them in detail

Table of contents Preface What situations can cau...

JS realizes the case of eliminating stars

This article example shares the specific code of ...

A brief discussion on using Vue to complete the mobile apk project

Table of contents Basic Configuration Entry file ...