C#如何合并两个位图? [英] C# How can I merge two Bitmaps?

查看:445
本文介绍了C#如何合并两个位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种在Paint事件中将两个位图合并在一起的方法.我的代码如下:

I'm trying to find a way to merge two Bitmaps together in a Paint event. My code looks like this:

private void GraphicsForm_Paint(object sender, PaintEventArgs e)
{
    try
    {
        Bitmap1 = new Bitmap(1366, 768);
        Bitmap2 = new Bitmap(1366, 768);
        OutputBitmap = ...//and this is where I've stuck :(
    }
    catch
    {
    }
}

问题更大,因为绘制到Bitmap2上的Graphics对象在另一个类中. 我还希望将Bitmap2绘制在OutputBitmap上的Bitmap1后面.

The problem is more problematic, because the Graphics object which draws onto Bitmap2 is in an other class. I also want Bitmap2 to be drawn behind Bitmap1 on the OutputBitmap.

谁能给我一个很好的建议,如何将这两个位图(彼此后面)合并到一个输出位图上?

Can anyone give me a good advice how to merge these two Bitmaps (behind eachother, but) onto one output bitmap?

谢谢:)

推荐答案

假设您的位图具有透明区域,请尝试创建一个位图,然后按所需顺序将另外两个位图绘制到其中:

Assuming your bitmaps have transparent areas, try creating one bitmap and draw the other two bitmaps into it in the order you want:

private Bitmap MergedBitmaps(Bitmap bmp1, Bitmap bmp2) {
  Bitmap result = new Bitmap(Math.Max(bmp1.Width, bmp2.Width),
                             Math.Max(bmp1.Height, bmp2.Height));
  using (Graphics g = Graphics.FromImage(result)) {
    g.DrawImage(bmp2, Point.Empty);
    g.DrawImage(bmp1, Point.Empty);
  }
  return result;
}

这篇关于C#如何合并两个位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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