简体   繁体   English

dotnet core 2.1+ System.Drawing.Common's grapic DrawImage 在 linux 中丢失透明度

[英]dotnet core 2.1+ System.Drawing.Common's grapic DrawImage loss transparency in linux

I'm trying add watermark to a jpeg picture using dotnetcore's system.drawing.common@4.5, everything is ok at my pc development environment, but when it run at linux server, the image's quality loss.我正在尝试使用 dotnetcore 的 system.drawing.common@4.5 为 jpeg 图片添加水印,在我的 PC 开发环境中一切正常,但是当它在 linux 服务器上运行时,图像的质量会下降。

string inputFile = "inputFile.jpg";
string waterMarkFile = "waterMark.png";
var wmImage = Image.FromFile(waterMarkFile);
FileInfo input = new FileInfo(inputFile);
using (var stream = new MemoryStream())
{
    using (var fs = input.OpenRead())
    {
        fs.CopyTo(stream);
        using (var outputStream = new MemoryStream())
        {
            using (var img = Image.FromStream(stream))
            {
                using (var graphic = Graphics.FromImage(img))
                {
                    int initX = img.Width / 2;
                    int initY = img.Height / 2;
                    int wmWidth = img.Width * 0.1 > wmImage.Width ? (int)(img.Width * 0.1) : img.Width;
                    int wmHeight = (wmImage.Height * wmWidth) / wmImage.Width;
                    int offsetX = wmWidth / 2;
                    int offsetY = wmHeight / 2;
                    for (int x = initX; x < img.Width + wmWidth; x += (int)(wmWidth *  2.5))
                    {
                        for (int y = initY; y < img.Height + wmHeight; y += (int)(wmHeight * 2.5))
                        {
                            graphic.DrawImage(wmImage, x - offsetX, y - offsetY, wmWidth, wmHeight);
                            if (x != initX || y !=initY)
                            {
                                graphic.DrawImage(wmImage, initX - (x - initX) - offsetX , y - offsetY, wmWidth, wmHeight);
                                graphic.DrawImage(wmImage, initX - (x - initX) - offsetX, initY - (y - initY) - offsetY, wmWidth, wmHeight);
                                graphic.DrawImage(wmImage, x - offsetX, initY - (y - initY) - offsetY, wmWidth, wmHeight);
                            }
                        }
                    }
                    img.Save($"wm_{inputFile}");
                }
            }
        }
    }
}
wmImage.Dispose();

The problem still exists.问题仍然存在。 I had the same problem with Debian based aspnet core images (3.1-buster-slim, 5.0-buster-slim).我在使用基于 Debian 的 aspnet 核心映像(3.1-buster-slim、5.0-buster-slim)时遇到了同样的问题。 It looks like an old version of libgdiplus will be installed here.看起来这里将安装旧版本的 libgdiplus。 To run the app on the local environment is no solution for me (everythings has to run on kubernetes pods...)在本地环境中运行应用程序对我来说不是解决方案(一切都必须在 kubernetes pods 上运行......)

I found two solutions:我找到了两个解决方案:

  1. Use Ubuntu based images (3.1-focal, 5.0-focal)使用基于 Ubuntu 的镜像(3.1-focal,5.0-focal)
  2. Use Debian (buster-slim) image, clone current libgdiplus form github an compile inside the image.使用 Debian (buster-slim) 映像,从 github 克隆当前的 libgdiplus 并在映像内进行编译。

I created Dockerfiles for both scenarios and published a GitHub repository :我为这两个场景创建了 Dockerfiles 并发布了一个 GitHub存储库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM