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


86

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

基本思想是:创建一个500x500的空白画布,将150x150图像放置在该空白画布的中间,然后将500x500图像复制到上面,以便透明的中间部分可以使150x150发光。

我知道如何在Java,PHP和Python中进行操作...我只是不知道在C#中使用哪些对象/类,只需将图像复制到另一个对象中即可。


Answers:


99

基本上,我在我们的一个应用程序中使用了此功能:我们想在一个视频帧上覆盖一个播放图标:

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) { }
    }
}

10
谢谢!今天完全保存了我的培根
Jason More

@downvoter会详细说明,以便我可以提高答案?
Andreas Niedermair 2014年

5
@AndreasNiedermair投票人很可能复制了您的代码,但无法正常工作
Jean-Paul

它是一个黄金答案!
DmitryBoyko '19


33

毕竟,我发现了一种更简单的新方法来试试..

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

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

4
很棒。g.Clear(Color.Transparent)如果要合并PNG图片以用于动画精灵
syclee 2012年

1
finalImage =新的System.Drawing.Bitmap(width,height); 为宽/高的高值引发错误
zeetit 2013年

@Anant Dabhi好的,很抱歉带回一个老问题,但是我将其转换为VB.NET。如果下一张图像上未使用的像素/空白像素是透明的,如果将它们彼此叠放,会否覆盖其他照片?如果没有,有什么办法吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.