叠加两个不同类型的图像

发布于 2024-11-27 10:08:07 字数 284 浏览 2 评论 0原文

我需要覆盖以下图像:

  1. System.Windows.Controls.Image image1
  2. System.Drawing.Bitmap image2;

我需要输出类型为 System.Windows.Media.ImageSource 。

我想到了以下方法:将 image2 转换为 Bitmap 并使用 System.Drawing.Graphics 覆盖两个图像,但我不知道如何转换 image2。

I need to overlay the following images:

  1. System.Windows.Controls.Image image1
  2. System.Drawing.Bitmap image2;

I need the output to be of type System.Windows.Media.ImageSource.

I thought of the following way: Convert image2 into a Bitmap and the overlay the two images using System.Drawing.Graphics, but I don't know how to convert image2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-12-04 10:08:07

一种选择是按照上面的建议将两个项目叠加在网格中,然后使用 此技术将控件呈现为位图文件。

或者,您可以将这两个文件转换为位图并使用以下代码循环它们(此代码通过将位图 2 分层到位图 1 上来组合图像,假设位图 2 中的任何白色值都是透明度值 - 您可以通过更改一行来更改此混合条件):

Bitmap bitmap1;
Bitmap bitmap2;
Bitmap result;
for(int x = 0; x<bitmap1.Width;x++)
{
    for(int y=0; y<bitmap1.Height;y++)
    {
        //condition for choosing which pixel to pick - based on how you want to overlay them (this code assumes white is transparent)
        if(bitmap2.GetPixel(x,y) == Colors.White)
        {
            result.SetPixel(x,y) = bitmap.GetPixel(x,y);
        }
        else
        {
            result.SetPixel(x,y) = bitmap2.GetPixel(x,y);
        }
    }
}

这将为您提供位图结果,然后您可以对其进行任何您想要的操作。

第一个选项速度更快,因为它是由 WPF 渲染代码加速的,但是我听说人们一直在努力强制渲染的位图不立即输出到文件,所以如果您希望它在内存中而不再次加载文件,那么它是不是你最好的选择。

第二个选项可以让您更好地控制图像的组合方式,但速度可能会较慢。

One option is overlaying the two items in a grid as suggested above and then using this technique to render the control to a bitmap file.

Alternatively, you could convert both files to a bitmap and loop over them using the following code (this code combines the images by layering bitmap2 on bitmap1, assuming any white values in bitmap2 are transparency values - you can change this blending condition by altering one line):

Bitmap bitmap1;
Bitmap bitmap2;
Bitmap result;
for(int x = 0; x<bitmap1.Width;x++)
{
    for(int y=0; y<bitmap1.Height;y++)
    {
        //condition for choosing which pixel to pick - based on how you want to overlay them (this code assumes white is transparent)
        if(bitmap2.GetPixel(x,y) == Colors.White)
        {
            result.SetPixel(x,y) = bitmap.GetPixel(x,y);
        }
        else
        {
            result.SetPixel(x,y) = bitmap2.GetPixel(x,y);
        }
    }
}

This will give you the Bitmap result, which you can then do whatever you want with.

The first option is faster, as it is accelerated by the WPF render code, however I've heard people have struggled to force the rendered bitmap to not output immediately to a file, so if you want it in memory without loading the file again it's not your best bet.

The second option gives you much more control over how the images are combined, but is potentially slower.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文