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

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

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

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

我需要(知道):

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

非常感谢你提前!

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

编辑:可能的解决方法(不是问题的解决方案)

A PictureBox Problem,第四个答案(来自用户comecme)告诉我有2个pictureBox,一个在另一个之上.我必须做的唯一(额外)事情是使它与这种方法一起工作:

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

哪个pictureBox2将是顶部的那个.

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

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

解决方法

这是我的问题的真正答案.
1)使用List< Bitmap>存储您想要混合的所有图像;
2)创建一个新的位图来保存最终图像;
3)使用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,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.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么