覆盖两个或多个位图以在Picturebox中显示(C#)

问题描述 投票:6回答:1

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

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

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

我需要(知道):

  1. 如何使用alpha值覆盖两个或多个位图;
  2. 请不要像素操作,不能承受性能成本。

非常感谢你提前!

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

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

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

哪个pictureBox2将是顶部的那个。

我不会认为这是这个问题的答案,因为我认为这是一种解决方法(特别是因为有超过10个图片盒似乎不太理想!哈哈)。这就是为什么我会打开这个问题等待我的问题的真正答案。

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

c# bitmap picturebox alpha alphablending
1个回答
8
投票

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

代码:

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。

© www.soinside.com 2019 - 2024. All rights reserved.