This article is a mirror article of machine translation, please click here to jump to the original article.

View: 50580|Reply: 2

[.NET Core] .NET Core watermarking issues on Linux pictures

[Copy link]
Posted on 2021-3-28 14:46:15 | | | |
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:

.NET Core releases a single file path issue
https://www.itsvse.com/thread-9604-1-1.html

.NET Core Linux Error: Couldn't find a valid ICU package installed...
https://www.itsvse.com/thread-9603-1-1.html

.NET Core error in Linux Failure processing application bundle
https://www.itsvse.com/thread-9602-1-1.html
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, pleaseReply


(End)




Previous:.NET Core releases a single file path issue
Next:.NET/C# Rule Engine RulesEngine tutorial
 Landlord| Posted on 2021-6-16 14:10:48 |
Posted on 2022-12-29 11:49:27 |
You can see that you can successfully add a Chinese watermark with the following code:
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com