SixLabors / ImageSharp.Drawing

:pen: Extensions to ImageSharp containing a cross-platform 2D polygon manipulation API and drawing operations.
https://sixlabors.com/products/imagesharp-drawing/
Other
285 stars 38 forks source link

In the environment of .net 6.0, add watermark to png image, the image is abnormal #176

Closed dodu2014 closed 2 years ago

dodu2014 commented 2 years ago

Prerequisites

Description

.net 5 以前的的环境(含.net5) 没有问题 .net 6 环境下 上传对png设置水印时, 图片会变花

image

Steps to Reproduce

          using Image image = Image.Load(ms.ToArray());
          image.Mutate(x => {

            // 如果设置压缩大小, 则先进行压缩
            if ((maxWidth > 0 || maxHeight > 0) && maxWidth < image.Width && maxHeight < image.Height) {
              if (maxWidth == 0) {
                maxWidth = image.Width * maxHeight / image.Height;
              }
              if (maxHeight == 0) {
                maxHeight = image.Height * maxWidth / image.Width;
              }
              x.Resize(new ResizeOptions {
                Mode = ResizeMode.Max,
                Size = new Size(maxWidth, maxHeight)
              });
            }

            // 灰度处理
            //x.Grayscale();

            // 如果设置有水印内容
            if (!string.IsNullOrEmpty(waterMark)) {

              Font font = SystemFonts.CreateFont("Microsoft YaHei", waterMarkFontsize); // 缩放水印模式时, 字体大小是被忽略的, 会根据图片大小自动处理
              Color color = Color.Gray.WithAlpha(0.5f); // 设置字体颜色和透明度(0: 全透明, 1: 不透明)
              PointF location = new(waterMarkMargin, waterMarkMargin); // 水印位置(默认左上角的位置)
              Size imgSize = x.GetCurrentSize(); // 从上下文获取当前尺寸
              FontRectangle fontSize = TextMeasurer.Measure(waterMark, new RendererOptions(font)); // 根据文字和字体, 测量字体尺寸

              if (waterMarkPosition == WaterMarkPositionEnum.scaling) {
                x.ApplyScalingWaterMark(font, waterMark, color, waterMarkMargin, false);
              } else {
                switch (waterMarkPosition) {
                  case WaterMarkPositionEnum.topright:
                    location.X = imgSize.Width - waterMarkMargin - fontSize.Width;
                    location.Y = waterMarkMargin;
                    break;
                  case WaterMarkPositionEnum.bottomleft:
                    location.X = waterMarkMargin;
                    location.Y = imgSize.Height - waterMarkMargin - fontSize.Height;
                    break;
                  case WaterMarkPositionEnum.bottomright:
                    location.X = imgSize.Width - waterMarkMargin - fontSize.Width;
                    location.Y = imgSize.Height - waterMarkMargin - fontSize.Height;
                    break;
                }
                x.DrawText(waterMark, font, color, location);
              }

            }
          });

          image.Save(fullPathFileName);

System Configuration

.net 6.0

tocsoft commented 2 years ago

This looks like the .net 6 bug that was patched in version v1.0.4 of SixLabors.ImageSharp (the main package).

Please can you make sure you are referencing v1.0.4 of SixLabors.ImageSharp. If you are only referencing SixLabors.ImageSharp.Drawing at the moment just add SixLabors.ImageSharp as an additional reference.

dodu2014 commented 2 years ago

Thank you, The problem has been resolved.