覆盖两个或多个位图中的PictureBox显示(C#) [英] Overlay two or more Bitmaps to show in Picturebox (C#)

查看:570
本文介绍了覆盖两个或多个位图中的PictureBox显示(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#程序我有一个PictureBox中,我要显示视频(连续帧)流。我收到的原始数据,我再转化为位图或图像。我可以显示一次一个图像没有问题(再现视频流)。

In my C# program I have a Picturebox in which i want to show a stream of video (consecutive frames). I receive raw data, that I then transform into Bitmap or Image. I can show one image at a time without a problem (to reproduce the video stream).

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

Now my issue is that I want to merge 2 or more bitmaps (like layers) with the same size and alpha values (ARGB) and show it on the picturebox.

我看过很多的网站和帖子在这里SO,但许多使用Graphics类的,我只是因为我是新的C#不能得出它在我的应用程序(非常可能!而且已经有我的程序设置,所以我不想改变结构)。

I have read lots of websites and posts here on SO, but many use the Graphics class, and I just can't draw it on my application (very likely because i'm new to C#! and already have my program setup, so I didn't want to change the structure).

我需要什么(要知道):

What i need (to know):


  1. 如何叠加两个或两个以上的位图与alpha值;

  2. NO像素操作请不起性能的成本。

感谢你这么多提前!

注意:我觉得这个问题不应该被标记(或关闭)为重复的,因为一切都在我发现SO要么是与像素处理或通过图形类完成。 (但我可能是错的!)

NOTE: I think this question shouldn't be marked (or closed) as duplicate, because everything i found in SO is done either with pixel manipulation or through the Graphics class. (but I might be wrong!)

编辑:可能的解决方法(而不是解决问​​题的办法)结果

一个PictureBox问题,第四答案(来自用户comecme)告诉我有2个图片框,一个在顶部的另一个。唯一的(额外)的事情我不得不这样做,使其与这种方法工作是:

Possible workaround (NOT the solution to the question)
In A PictureBox Problem, the 4th answer (from user comecme) tells me to have 2 picturebox, one on top of the other. The only (extra) thing I had to do to make it work with this approach was:

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

其中pictureBox2将在上面之一。

Where pictureBox2 will be the one on top.

我不认为这是一个回答这个问题,因为我认为这是一个解决方法(特别是因为有超过10 pictureboxes似乎并不理想!大声笑)。这就是为什么我会离开这个问题开等待真正回答我的问题。

I won't consider this an answer to this problem, because I consider it a workaround (specially because having more than 10 pictureboxes doesn't seem ideal! lol). That's why I will leave this question opened waiting for a real answer to my question.

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

推荐答案

下面是在真正回答我的问题。结果
1)使用列表与LT;位图> 来存储所有要混合的图像;结果
2)创建一个新的位图来保存最终的图像;结果
3)使用使用语句中的最终图像的的图形的上方绘制每个图像。

Here is the real answer to my problem.
1) Use a List<Bitmap> to store all the images you want to blend;
2) Create a new Bitmap to hold the final image;
3) Draw each image on top of the final image's graphics using the using statement.

的code:

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();

积分转至布兰登Cannaday 网页tech.pro 。

这篇关于覆盖两个或多个位图中的PictureBox显示(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆