在C#/.NET中合并两个图像

97

简单的想法:我有两张图片要合并,一张是500x500,在中间是透明的,另一张是150x150。

基本思路是这样的:创建一个空画布,大小为500x500,将150x150的图像放在空白画布的中心,然后将500x500的图像复制过来,使其透明的中心能让150x150的图像显示出来。

我知道如何在Java、PHP和Python中实现它......我只是不知道在C#中使用哪些对象/类,如果有一个快速将一个图像复制到另一个图像的示例就足够了。


这个有帮助吗? http://www.daniweb.com/forums/thread87993.html - Dror
4个回答

110

基本上我在我们的一个应用程序中使用它: 我们想在视频的一个帧上叠加一个播放图标:

Image playbutton;
try
{
    playbutton = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
    return;
}

Image frame;
try
{
    frame = Image.FromFile(/*somekindofpath*/);
}
catch (Exception ex)
{
    return;
}

using (frame)
{
    using (var bitmap = new Bitmap(width, height))
    {
        using (var canvas = Graphics.FromImage(bitmap))
        {
            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            canvas.DrawImage(frame,
                             new Rectangle(0,
                                           0,
                                           width,
                                           height),
                             new Rectangle(0,
                                           0,
                                           frame.Width,
                                           frame.Height),
                             GraphicsUnit.Pixel);
            canvas.DrawImage(playbutton,
                             (bitmap.Width / 2) - (playbutton.Width / 2),
                             (bitmap.Height / 2) - (playbutton.Height / 2));
            canvas.Save();
        }
        try
        {
            bitmap.Save(/*somekindofpath*/,
                        System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex) { }
    }
}

63
这将在一个图像中添加另一个图像。
using (Graphics grfx = Graphics.FromImage(image))
{
    grfx.DrawImage(newImage, x, y)
}

图形位于命名空间 System.Drawing 中。


38

经过这一番尝试后,我找到了一个更简单的方法,请试试...

它可以将多张照片合并在一起:

public static System.Drawing.Bitmap CombineBitmap(string[] files)
{
    //read all images into memory
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
    System.Drawing.Bitmap finalImage = null;

    try
    {
        int width = 0;
        int height = 0;

        foreach (string image in files)
        {
            //create a Bitmap from the file and add it to the list
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

            //update the size of the final bitmap
            width += bitmap.Width;
            height = bitmap.Height > height ? bitmap.Height : height;

            images.Add(bitmap);
        }

        //create a bitmap to hold the combined image
        finalImage = new System.Drawing.Bitmap(width, height);

        //get a graphics object from the image so we can draw on it
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
        {
            //set background color
            g.Clear(System.Drawing.Color.Black);

            //go through each image and draw it on the final image
            int offset = 0;
            foreach (System.Drawing.Bitmap image in images)
            {
                g.DrawImage(image,
                  new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                offset += image.Width;
            }
        }

        return finalImage;
    }
    catch (Exception ex)
    {
        if (finalImage != null)
            finalImage.Dispose();

        throw ex;
    }
    finally
    {
        //clean up memory
        foreach (System.Drawing.Bitmap image in images)
        {
            image.Dispose();
        }
    }
}

5
运行良好。如果你想要合并PNG图像来制作动画精灵,可以使用 g.Clear(Color.Transparent)。 - syclee
1
当width/height的值很高时,finalImage = new System.Drawing.Bitmap(width, height);会抛出错误。 - cosmoloc
@Anant Dabhi,好的,我很抱歉又提出一个旧问题,但是我已经将它转换为VB.NET了。如果下一张图片上未使用的像素/空白像素是透明的,那么它会覆盖其他照片吗?如果不行,有没有什么办法可以做到呢? - user5473961

0

这将水平地将图像并排放置,生成的图像高度与第一张图像相同。

private Image IconCombiner(Image OriginalImage, Image ImageToAdd)
    {
        var bitmap = new Bitmap(OriginalImage.Width + ImageToAdd.Width, OriginalImage.Height);
        
        using (Graphics grfx = Graphics.FromImage(bitmap))
        {
            grfx.DrawImage(OriginalImage, 0, 0);

            grfx.DrawImage(ImageToAdd, OriginalImage.Width, 0);
        }

        return bitmap;
    }

使用方法如下:

Image myNewCombinedImage = IconCombiner(Image.FromFile(@"YourPathToFile1"), Image.FromFile(@"YourPathToFile2")) ;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接