【问题标题】:How do I overlay an image in .NET如何在 .NET 中叠加图像
【发布时间】:2022-04-21 20:10:18
【问题描述】:

我有一个 .png 图像,我希望覆盖在基础图像上。

我的叠加图像仅包含一条红色斜线。我需要将红线覆盖在基本图像上,其坐标与覆盖图像中的坐标相同。

问题是我没有坐标位置。

我需要使用 C# 以编程方式找到它。叠加图像将始终是透明的或白色背景。什么代码可以从叠加图像中找到线坐标?

【问题讨论】:

  • 什么是“同坐标”?也许一些插图会更清楚。
  • 如果在叠加图像中我的行从 20,20 开始并在 120,50 结束,那么我需要该行出现在我的基本图像中的 20,20 并以 120,50 结束。

标签: c#


【解决方案1】:

您可以创建新图像,先渲染背景图像,然后在其上渲染叠加图像。由于叠加层具有 alpha 通道,并且线条放置在应有的位置(我的意思是线条的顶部和左侧有不透明的空间),因此您不需要坐标。图解代码:

Image imageBackground = Image.FromFile("bitmap1.png");
Image imageOverlay = Image.FromFile("bitmap2.png");

Image img = new Bitmap(imageBackground.Width, imageBackground.Height);
using (Graphics gr = Graphics.FromImage(img))
{
    gr.DrawImage(imageBackground, new Point(0, 0));
    gr.DrawImage(imageOverlay, new Point(0, 0));
}
img.Save("output.png", ImageFormat.Png);

【讨论】:

  • 谢谢,但上面的代码第二张图片与背景图片重叠。例如,如果我的背景图片是一张脸,并且说用户已经对其进行了编辑以在脸上添加耳朵。两个图像的保存方式不同。我想以这样一种方式合并图像,即脸部应该在用户添加它的正确位置有耳朵。
  • 谢谢,我首先遇到了您的代码问题,因为我生成的图像比我的屏幕尺寸大。这导致图像的一部分被裁剪。我通过使用 gr.DrawImage(imageBackground, new System.Drawing.Rectangle(0, 0, imageBackground.Width, imageBackground.Height)); 解决了它gr.DrawImage(imageOverlay, new System.Drawing.Rectangle(0, 0, imageBackground.Width, imageBackground.Height));而不是 gr.DrawImage(imageBackground, new Point(0, 0)); gr.DrawImage(imageOverlay, new Point(0, 0));
【解决方案2】:

如果您使用的是 WPF,为什么不使用 path 代替图像,如果它是一条简单的线?这将允许它缩放到任何大小,并具有操纵其尺寸的方法。

如果您使用的是 Winforms,您可以利用一些 similar graphics drawing capabilities。获取图像的尺寸应该很容易,假设您使用的是 PictureBox,以下属性就足够了:

myPictureBox.Top
myPictureBox.Bottom
myPictureBox.Left
myPictureBox.Right

同样,对于 WPF 图像:

myImage.Margin

【讨论】:

    【解决方案3】:

    我已经需要在图像周围创建一个空白区域,我使用 ImageFactory 库来做到这一点。

    这里是代码。我猜你有能力弄清楚如何适应你的需要。

        public static Image ResizedImage(Image image, int desiredWidth, int desiredHeight)
        {
            Image res = (Bitmap)image.Clone();
            Image resizedImage;
            ImageLayer imageLayer = new ImageLayer();
            try
            {
                if (res != null)
                {
                    //white background
                    res = new Bitmap(desiredWidth, desiredHeight, res.PixelFormat);
                    //image to handle
                    using (var imgf = new ImageFactory(true))
                    {
                        imgf
                            .Load(image)
                            .Resize(new ResizeLayer(new Size(desiredWidth, desiredHeight),
                                                    ResizeMode.Max,
                                                    AnchorPosition.Center,
                                                    false));
                        resizedImage = (Bitmap)imgf.Image.Clone();
                    }
                    //final image
                    if (resizedImage != null)
                    {
                        imageLayer.Image = resizedImage;
                        imageLayer.Size = new Size(resizedImage.Width, resizedImage.Height);
                        imageLayer.Opacity = 100;
                        using (var imgf = new ImageFactory(true))
                        {
                            imgf
                                .Load(res)
                                .BackgroundColor(Color.White)
                                .Overlay(imageLayer);
                            res = (Bitmap)imgf.Image.Clone();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message;
            }
            return res;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-03-07
      • 1970-01-01
      • 2010-09-29
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多