Requirements: Now that .NET Core can be cross-platform, publish the project to the Linux environment to run, and the project will include some image processing, such as: generating verification codes, watermarking images, etc.
First, create a .NET Core 3.1 console project (the source code is attached at the end of the article), and image processing needs to be installed:System.Drawing.Common
Use the nuget package to manage the installation with the following command:
Publish the project as a single file, the target platform is: linux-x64, and the publishing configuration is as follows:
<?xml version="1.0" encoding="utf-8"?> <!--
https://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration>Release</Configuration> <Platform>Any CPU</Platform> <PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir> <PublishProtocol>FileSystem</PublishProtocol> <TargetFramework>netcoreapp3.1</TargetFramework> <RuntimeIdentifier>linux-x64</RuntimeIdentifier> <SelfContained>true</SelfContained> <PublishSingleFile>True</PublishSingleFile> <PublishTrimmed>False</PublishTrimmed> </PropertyGroup> </Project> (The generated single executable file actually contains the environment required for the execution of the program, and there is no need for the target computer to install the SDK, and the disadvantage is that the file size will become very large.)
Upload the published file to the CentOS server, andGive the file execution permissions! Otherwise, the program will not run with the following command:
Then execute the file, and the problems you may encounter are as follows:
If the error is as follows:
Unhandled exception. System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libgdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibgdiplus: cannot open shared object file: No such file or directory 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.GdipLoadImageFromFile(String filename, IntPtr& image) at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at System.Drawing.Image.FromFile(String filename) at ImageWatermark.Program.Main(String[] args) Aborted Use the following commandInstall libgdiplus-devel:
Try to re-execute, and you can find that the execution succeeded, as shown in the following image:
View the watermarked image and open the "57f2305a-6850-41af-80ab-8d2bf89569e5.jpg" file, the effect is as follows:
Discovery,Chinese characters are garbled, there is no problem with English characters, check the font installed by the system, the command is as follows:
If the command is not available in the system, we need to install the relevant software package first, the command is as follows:
Solution, install the open-source commercially available google-noto font with the following command:
Modify the font used in the source code, republish and upload to the server, and the execution result is as follows:
You can see that you can successfully add a Chinese watermark with the following code:
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
(End)
|