在我的 C# 程序中,我有一个 Picturebox,我想在其中显示视频流(连续帧)。我收到原始数据,然后将其转换为位图或图像。我可以一次显示一张图像,没有问题(重现视频流)。
现在我的问题是我想要合并 2 个或多个具有相同大小和 alpha 值 (ARGB) 的位图(如图层)并将其显示在图片框上.
我在这里阅读了很多关于 SO 的网站和帖子,但很多都使用 Graphics 类,而我只是无法在我的应用程序上绘制它(很可能是因为我是 C# 新手!并且已经有了我的程序设置,所以我不想改变结构)。
我需要什么(知道):
- 如何用 alpha 值叠加两个或多个位图;
- 请不要进行像素操作,无法承担这样的性能成本。
提前非常感谢!
NOTE:我认为这个问题不应该被标记(或关闭)为重复,因为我在 SO 中找到的所有内容都是通过像素操作或通过 Graphics 类完成的。 (但我可能是错的!)
编辑:可能的解决方法(不是问题的解决方案)
In
图片框问题 https://stackoverflow.com/questions/4144371/a-picturebox-problem,第四个答案(来自用户 comecme)告诉我有 2 个图片框,一个在另一个上面。为了使其与这种方法一起工作,我必须做的唯一(额外)事情是:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.Parent = pictureBox1;
}
其中 pictureBox2 将位于顶部。
我不会认为这是这个问题的答案,因为我认为这是一种解决方法(特别是因为拥有超过 10 个图片框似乎并不理想!哈哈)。这就是为什么我会留下这个问题等待real回答我的问题。
编辑:解决了!检查我的答案。
这里是real回答我的问题。
1)使用List<Bitmap>
存储您想要混合的所有图像;
2)创建一个新的Bitmap来保存最终的图像;
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();
积分转到布兰登坎纳迪 in 这个 tech.pro 网页 https://web.archive.org/web/20130214003927/http://tech.pro/tutorial/921/combining-images-with-csharp.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)