代码改变世界

.NET Core中使用水印

2019-06-09 20:42  Wizardlsw  阅读(1619)  评论(0编辑  收藏  举报

.NET Core中使用水印

在项目中,我需要给上传的图片添加水印,通过网上查找针对.NET Core 的找到两个方案:

  1. 使用 CoreCompat.System.Drawing 及用于非Windows的runtime.osx.10.10-x64.CoreCompat.System.Drawingruntime.linux-x64.CoreCompat.System.Drawing
  2. 使用 System.Drawing.Common

遇到问题

经过尝试,发现方案1在.NET Core 2.2里面有类型冲突问题,如下:

The type 'Color' exists in both 'CoreCompat.System.Drawing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c0a7ed9c2333b592' and 'System.Drawing.Primitives, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

故我采用了方案2,一开始很顺利:本地Windows上测试、debug都正常!

但当我把代码部署到CentOS服务器上的时候,出现问题, 得到错误消息如下:

ERROR:The type initializer for 'Gdip' threw an exception.
at System.Drawing.SafeNativeMethods.Gdip.GdipLoadImageFromDelegate_linux(StreamGetHeaderDelegate getHeader, StreamGetBytesDelegate getBytes, StreamPutBytesDelegate putBytes, StreamSeekDelegate doSeek, StreamCloseDelegate close, StreamSizeDelegate size, IntPtr& image)
at System.Drawing.Image.InitFromStream(Stream stream)
at System.Drawing.Image.LoadFromStream(Stream stream, Boolean keepAlive)

问题解决

很快在GitHub上找到接近答案的答案:ERROR:The type initializer for 'Gdip' threw an exception
但按照上面的步骤(我稍做整理):

sudo yum install libgdiplus


cd /usr/lib
sudo ln -s libgdiplus.so gdiplus.dll

export LD_LIBRARY_PATH=/usr/lib

很遗憾,没有找到/usr/lib/libgdiplus.so,但我确实安装成功了。所以,经过摸索,我找到了,由于我的服务器是64位的,实际安装目录在/usr/lib64,且文件名是libgdiplus.so.0libgdiplus.so.0.0.0,前者似乎是softlink。

OK到此成功解决,似乎都不需要重启服务,步骤如下:

sudo yum install libgdiplus

cd /usr/lib64
ln -s libgdiplus.so gdiplus.dll

export LD_LIBRARY_PATH=/usr/lib

参考文章

Watermark images on the fly in ASP.NET Core
在ASP.NET Core中给上传图片功能添加水印
ERROR:The type initializer for 'Gdip' threw an exception

其他

安装libgdiplus的成功输出:

[root@izuf****1agz ****]# yum install libgdiplus
Loaded plugins: fastestmirror
Determining fastest mirrors
base                                                                            | 3.6 kB  00:00:00     
epel                                                                            | 5.3 kB  00:00:00     
extras                                                                          | 3.4 kB  00:00:00     
nginx                                                                           | 2.9 kB  00:00:00     
packages-microsoft-com-prod                                                     | 2.9 kB  00:00:00     
updates                                                                         | 3.4 kB  00:00:00     
(1/6): epel/x86_64/updateinfo                                                   | 974 kB  00:00:00     
(2/6): extras/7/x86_64/primary_db                                               | 200 kB  00:00:00     
(3/6): updates/7/x86_64/primary_db                                              | 5.0 MB  00:00:00     
(4/6): epel/x86_64/primary_db                                                   | 6.7 MB  00:00:00     
(5/6): packages-microsoft-com-prod/primary_db                                   | 170 kB  00:00:00     
(6/6): nginx/x86_64/primary_db                                                  |  46 kB  00:00:05     
Resolving Dependencies
--> Running transaction check
---> Package libgdiplus.x86_64 0:2.10-10.el7 will be installed
--> Processing Dependency: libtiff.so.5(LIBTIFF_4.0)(64bit) for package: libgdiplus-2.10-10.el7.x86_64
--> Processing Dependency: libXrender.so.1()(64bit) for package: libgdiplus-2.10-10.el7.x86_64
--> Processing Dependency: libcairo.so.2()(64bit) for package: libgdiplus-2.10-10.el7.x86_64
--> Processing Dependency: libexif.so.12()(64bit) for package: libgdiplus-2.10-10.el7.x86_64
--> Processing Dependency: libgif.so.4()(64bit) for package: libgdiplus-2.10-10.el7.x86_64
--> Processing Dependency: libtiff.so.5()(64bit) for package: libgdiplus-2.10-10.el7.x86_64
--> Running transaction check
---> Package cairo.x86_64 0:1.15.12-3.el7 will be installed
--> Processing Dependency: libpixman-1.so.0()(64bit) for package: cairo-1.15.12-3.el7.x86_64
--> Processing Dependency: libGL.so.1()(64bit) for package: cairo-1.15.12-3.el7.x86_64
--> Processing Dependency: libEGL.so.1()(64bit) for package: cairo-1.15.12-3.el7.x86_64
---> Package giflib.x86_64 0:4.1.6-9.el7 will be installed
---> Package libXrender.x86_64 0:0.9.10-1.el7 will be installed
---> Package libexif.x86_64 0:0.6.21-6.el7 will be installed
---> Package libtiff.x86_64 0:4.0.3-27.el7_3 will be installed
--> Processing Dependency: libjbig.so.2.0()(64bit) for package: libtiff-4.0.3-27.el7_3.x86_64
--> Running transaction check
---> Package jbigkit-libs.x86_64 0:2.0-11.el7 will be installed
---> Package libglvnd-egl.x86_64 1:1.0.1-0.8.git5baa1e5.el7 will be installed
--> Processing Dependency: libglvnd(x86-64) = 1:1.0.1-0.8.git5baa1e5.el7 for package: 1:libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64
--> Processing Dependency: mesa-libEGL(x86-64) >= 13.0.4-1 for package: 1:libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64
--> Processing Dependency: libGLdispatch.so.0()(64bit) for package: 1:libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64
---> Package libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7 will be installed
--> Processing Dependency: mesa-libGL(x86-64) >= 13.0.4-1 for package: 1:libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64
---> Package pixman.x86_64 0:0.34.0-1.el7 will be installed
--> Running transaction check
---> Package libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7 will be installed
---> Package mesa-libEGL.x86_64 0:18.0.5-4.el7_6 will be installed
--> Processing Dependency: mesa-libgbm = 18.0.5-4.el7_6 for package: mesa-libEGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libxshmfence.so.1()(64bit) for package: mesa-libEGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libwayland-server.so.0()(64bit) for package: mesa-libEGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libwayland-client.so.0()(64bit) for package: mesa-libEGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libglapi.so.0()(64bit) for package: mesa-libEGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libgbm.so.1()(64bit) for package: mesa-libEGL-18.0.5-4.el7_6.x86_64
---> Package mesa-libGL.x86_64 0:18.0.5-4.el7_6 will be installed
--> Processing Dependency: libXxf86vm.so.1()(64bit) for package: mesa-libGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libXfixes.so.3()(64bit) for package: mesa-libGL-18.0.5-4.el7_6.x86_64
--> Processing Dependency: libXdamage.so.1()(64bit) for package: mesa-libGL-18.0.5-4.el7_6.x86_64
--> Running transaction check
---> Package libXdamage.x86_64 0:1.1.4-4.1.el7 will be installed
---> Package libXfixes.x86_64 0:5.0.3-1.el7 will be installed
---> Package libXxf86vm.x86_64 0:1.1.4-1.el7 will be installed
---> Package libwayland-client.x86_64 0:1.15.0-1.el7 will be installed
---> Package libwayland-server.x86_64 0:1.15.0-1.el7 will be installed
---> Package libxshmfence.x86_64 0:1.2-1.el7 will be installed
---> Package mesa-libgbm.x86_64 0:18.0.5-4.el7_6 will be installed
---> Package mesa-libglapi.x86_64 0:18.0.5-4.el7_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================
 Package                    Arch            Version                             Repository        Size
=======================================================================================================
Installing:
 libgdiplus                 x86_64          2.10-10.el7                         epel             163 k
Installing for dependencies:
 cairo                      x86_64          1.15.12-3.el7                       base             741 k
 giflib                     x86_64          4.1.6-9.el7                         base              40 k
 jbigkit-libs               x86_64          2.0-11.el7                          base              46 k
 libXdamage                 x86_64          1.1.4-4.1.el7                       base              20 k
 libXfixes                  x86_64          5.0.3-1.el7                         base              18 k
 libXrender                 x86_64          0.9.10-1.el7                        base              26 k
 libXxf86vm                 x86_64          1.1.4-1.el7                         base              18 k
 libexif                    x86_64          0.6.21-6.el7                        base             347 k
 libglvnd                   x86_64          1:1.0.1-0.8.git5baa1e5.el7          base              89 k
 libglvnd-egl               x86_64          1:1.0.1-0.8.git5baa1e5.el7          base              44 k
 libglvnd-glx               x86_64          1:1.0.1-0.8.git5baa1e5.el7          base             125 k
 libtiff                    x86_64          4.0.3-27.el7_3                      base             170 k
 libwayland-client          x86_64          1.15.0-1.el7                        base              33 k
 libwayland-server          x86_64          1.15.0-1.el7                        base              39 k
 libxshmfence               x86_64          1.2-1.el7                           base             7.2 k
 mesa-libEGL                x86_64          18.0.5-4.el7_6                      updates          102 k
 mesa-libGL                 x86_64          18.0.5-4.el7_6                      updates          162 k
 mesa-libgbm                x86_64          18.0.5-4.el7_6                      updates           38 k
 mesa-libglapi              x86_64          18.0.5-4.el7_6                      updates           44 k
 pixman                     x86_64          0.34.0-1.el7                        base             248 k

Transaction Summary
=======================================================================================================
Install  1 Package (+20 Dependent packages)

Total download size: 2.5 M
Installed size: 8.0 M
Is this ok [y/d/N]: y
Downloading packages:
(1/21): giflib-4.1.6-9.el7.x86_64.rpm                                           |  40 kB  00:00:00     
(2/21): jbigkit-libs-2.0-11.el7.x86_64.rpm                                      |  46 kB  00:00:00     
(3/21): libXdamage-1.1.4-4.1.el7.x86_64.rpm                                     |  20 kB  00:00:00     
(4/21): libXfixes-5.0.3-1.el7.x86_64.rpm                                        |  18 kB  00:00:00     
(5/21): cairo-1.15.12-3.el7.x86_64.rpm                                          | 741 kB  00:00:00     
(6/21): libXrender-0.9.10-1.el7.x86_64.rpm                                      |  26 kB  00:00:00     
(7/21): libXxf86vm-1.1.4-1.el7.x86_64.rpm                                       |  18 kB  00:00:00     
(8/21): libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm                            |  89 kB  00:00:00     
(9/21): libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm                        |  44 kB  00:00:00     
(10/21): libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm                       | 125 kB  00:00:00     
(11/21): libtiff-4.0.3-27.el7_3.x86_64.rpm                                      | 170 kB  00:00:00     
(12/21): libwayland-client-1.15.0-1.el7.x86_64.rpm                              |  33 kB  00:00:00     
(13/21): libgdiplus-2.10-10.el7.x86_64.rpm                                      | 163 kB  00:00:00     
(14/21): libwayland-server-1.15.0-1.el7.x86_64.rpm                              |  39 kB  00:00:00     
(15/21): libexif-0.6.21-6.el7.x86_64.rpm                                        | 347 kB  00:00:00     
(16/21): libxshmfence-1.2-1.el7.x86_64.rpm                                      | 7.2 kB  00:00:00     
(17/21): mesa-libEGL-18.0.5-4.el7_6.x86_64.rpm                                  | 102 kB  00:00:00     
(18/21): mesa-libGL-18.0.5-4.el7_6.x86_64.rpm                                   | 162 kB  00:00:00     
(19/21): mesa-libgbm-18.0.5-4.el7_6.x86_64.rpm                                  |  38 kB  00:00:00     
(20/21): mesa-libglapi-18.0.5-4.el7_6.x86_64.rpm                                |  44 kB  00:00:00     
(21/21): pixman-0.34.0-1.el7.x86_64.rpm                                         | 248 kB  00:00:00     
-------------------------------------------------------------------------------------------------------
Total                                                                  5.2 MB/s | 2.5 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mesa-libglapi-18.0.5-4.el7_6.x86_64                                                1/21 
  Installing : 1:libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64                                         2/21 
  Installing : libXfixes-5.0.3-1.el7.x86_64                                                       3/21 
  Installing : libwayland-server-1.15.0-1.el7.x86_64                                              4/21 
  Installing : libXrender-0.9.10-1.el7.x86_64                                                     5/21 
  Installing : libxshmfence-1.2-1.el7.x86_64                                                      6/21 
  Installing : mesa-libgbm-18.0.5-4.el7_6.x86_64                                                  7/21 
  Installing : libXdamage-1.1.4-4.1.el7.x86_64                                                    8/21 
  Installing : jbigkit-libs-2.0-11.el7.x86_64                                                     9/21 
  Installing : libtiff-4.0.3-27.el7_3.x86_64                                                     10/21 
  Installing : pixman-0.34.0-1.el7.x86_64                                                        11/21 
  Installing : libexif-0.6.21-6.el7.x86_64                                                       12/21 
  Installing : libXxf86vm-1.1.4-1.el7.x86_64                                                     13/21 
  Installing : 1:libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64                                    14/21 
  Installing : mesa-libGL-18.0.5-4.el7_6.x86_64                                                  15/21 
  Installing : giflib-4.1.6-9.el7.x86_64                                                         16/21 
  Installing : libwayland-client-1.15.0-1.el7.x86_64                                             17/21 
  Installing : 1:libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64                                    18/21 
  Installing : mesa-libEGL-18.0.5-4.el7_6.x86_64                                                 19/21 
  Installing : cairo-1.15.12-3.el7.x86_64                                                        20/21 
  Installing : libgdiplus-2.10-10.el7.x86_64                                                     21/21 
  Verifying  : libgdiplus-2.10-10.el7.x86_64                                                      1/21 
  Verifying  : mesa-libEGL-18.0.5-4.el7_6.x86_64                                                  2/21 
  Verifying  : libtiff-4.0.3-27.el7_3.x86_64                                                      3/21 
  Verifying  : libwayland-client-1.15.0-1.el7.x86_64                                              4/21 
  Verifying  : libxshmfence-1.2-1.el7.x86_64                                                      5/21 
  Verifying  : giflib-4.1.6-9.el7.x86_64                                                          6/21 
  Verifying  : libXxf86vm-1.1.4-1.el7.x86_64                                                      7/21 
  Verifying  : libXrender-0.9.10-1.el7.x86_64                                                     8/21 
  Verifying  : mesa-libglapi-18.0.5-4.el7_6.x86_64                                                9/21 
  Verifying  : 1:libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64                                    10/21 
  Verifying  : mesa-libgbm-18.0.5-4.el7_6.x86_64                                                 11/21 
  Verifying  : libXdamage-1.1.4-4.1.el7.x86_64                                                   12/21 
  Verifying  : libwayland-server-1.15.0-1.el7.x86_64                                             13/21 
  Verifying  : libexif-0.6.21-6.el7.x86_64                                                       14/21 
  Verifying  : pixman-0.34.0-1.el7.x86_64                                                        15/21 
  Verifying  : mesa-libGL-18.0.5-4.el7_6.x86_64                                                  16/21 
  Verifying  : libXfixes-5.0.3-1.el7.x86_64                                                      17/21 
  Verifying  : 1:libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64                                        18/21 
  Verifying  : jbigkit-libs-2.0-11.el7.x86_64                                                    19/21 
  Verifying  : 1:libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64                                    20/21 
  Verifying  : cairo-1.15.12-3.el7.x86_64                                                        21/21 

Installed:
  libgdiplus.x86_64 0:2.10-10.el7                                                                      

Dependency Installed:
  cairo.x86_64 0:1.15.12-3.el7                       giflib.x86_64 0:4.1.6-9.el7                       
  jbigkit-libs.x86_64 0:2.0-11.el7                   libXdamage.x86_64 0:1.1.4-4.1.el7                 
  libXfixes.x86_64 0:5.0.3-1.el7                     libXrender.x86_64 0:0.9.10-1.el7                  
  libXxf86vm.x86_64 0:1.1.4-1.el7                    libexif.x86_64 0:0.6.21-6.el7                     
  libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7         libglvnd-egl.x86_64 1:1.0.1-0.8.git5baa1e5.el7    
  libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7     libtiff.x86_64 0:4.0.3-27.el7_3                   
  libwayland-client.x86_64 0:1.15.0-1.el7            libwayland-server.x86_64 0:1.15.0-1.el7           
  libxshmfence.x86_64 0:1.2-1.el7                    mesa-libEGL.x86_64 0:18.0.5-4.el7_6               
  mesa-libGL.x86_64 0:18.0.5-4.el7_6                 mesa-libgbm.x86_64 0:18.0.5-4.el7_6               
  mesa-libglapi.x86_64 0:18.0.5-4.el7_6              pixman.x86_64 0:0.34.0-1.el7                      

Complete!