【问题标题】:Overlay two or more Bitmaps to show in Picturebox (C#)覆盖两个或多个位图以显示在 Picturebox (C#)
【发布时间】:2013-01-02 01:46:08
【问题描述】:

在我的 C# 程序中,我有一个 Picturebox,我想在其中显示视频流(连续帧)。我收到原始数据,然后将其转换为位图或图像。我可以一次显示一张图像而不会出现问题(重现视频流)。

现在我的问题是我想合并 2 个或多个具有相同大小和 alpha 值 (ARGB) 的位图(如图层)并将其显示在图片框上

我已经阅读了很多关于 SO 的网站和帖子,但很多都使用 Graphics 类,我就是无法在我的应用程序上绘制它(很可能是因为我是 C# 新手!并且已经有我的程序设置,所以我不想改变结构)。

我需要(知道)什么:

  1. 如何用 alpha 值覆盖两个或多个位图;
  2. 请不要进行像素操作,我们无法承受这样的性能成本。

非常感谢您!

注意:我认为这个问题不应该被标记(或关闭)为重复,因为我在 SO 中找到的所有内容都是通过像素操作或通过 Graphics 类完成的。 (但我可能错了!)

编辑:可能的解决方法(不是问题的解决方案
A PictureBox Problem,第四个答案(来自用户 comecme)告诉我有 2 个图片框,一个在另一个之上。为了使它与这种方法一起工作,我必须做的唯一(额外)事情是:

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox2.Parent = pictureBox1;
}

pictureBox2 将是最上面的那个。

我不会认为这是解决此问题的方法,因为我认为这是一种解决方法(特别是因为拥有 10 个以上的图片框似乎并不理想!哈哈)。这就是为什么我会留下这个问题,等待对我的问题的真实答案。

编辑:已解决!检查我的答案。

【问题讨论】:

  • hm.. 尝试使用SlimDX + HLSL code 快速叠加两张图片。 (仍然会有在 GPU 端完成的像素操作,因此速度很快)
  • 0x69 thks 的答案,但这个应用程序是一个更大的“系统”的一部分,要向公众发布,所以我期待一些更容易/不管外部框架的东西..
  • 性能提升往往伴随着复杂性的损失。
  • 我确实知道 0x69 :) 但我现在正在搜索关于拥有两个具有“父母关系”的图片框,正如我刚刚在 SO 上找到的那样。不完全是我想要的,但结果是一样的。找到答案后,我会更新我的问题。谢谢

标签: c# bitmap picturebox alpha alphablending


【解决方案1】:

这是我的问题的真实答案。
1) 使用List<Bitmap> 存储您要混合的所有图像;
2) 创建一个新的位图来保存最终图像;
3) 使用using 语句在最终图像的graphics 之上绘制每个图像。

代码:

List<Bitmap> images = new List<Bitmap>();  
Bitmap finalImage = new Bitmap(640, 480);

...

//For each layer, I transform the data into a Bitmap (doesn't matter what kind of
//data, in this question) and add it to the images list
for (int i = 0; i < nLayers; ++i)
{
    Bitmap bitmap = new Bitmap(layerBitmapData[i]));
    images.Add(bitmap);
}

using (Graphics g = Graphics.FromImage(finalImage))
{
    //set background color
    g.Clear(Color.Black);

    //go through each image and draw it on the final image (Notice the offset; since I want to overlay the images i won't have any offset between the images in the finalImage)
    int offset = 0;
    foreach (Bitmap image in images)
    {
        g.DrawImage(image, new Rectangle(offset, 0, image.Width, image.Height));
    }   
}
//Draw the final image in the pictureBox
this.layersBox.Image = finalImage;
//In my case I clear the List because i run this in a cycle and the number of layers is not fixed 
images.Clear();

感谢 this tech.pro webpage 中的 Brandon Cannaday

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2013-11-07
    • 2012-02-10
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多