【问题标题】:Merging two images in C#/.NET在 C#/.NET 中合并两个图像
【发布时间】:2010-10-02 16:26:25
【问题描述】:

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

基本思路是这样的:创建一个 500x500 的空画布,将 150x150 的图像放置在空画布的中间,然后将 500x500 的图像复制过来,这样它的透明中间就可以让 150x150 的图像透过。

p>

我知道如何在 Java、PHP 和 Python 中做到这一点...我只是不知道在 C# 中使用什么对象/类,一个将图像复制到另一个图像的快速示例就足够了。

【问题讨论】:

标签: c# .net image


【解决方案1】:

这会将图像添加到另一个图像。

using (Graphics grfx = Graphics.FromImage(image))
{
    grfx.DrawImage(newImage, x, y)
}

图形位于命名空间System.Drawing

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 谢谢!今天完全救了我的培根
    • @downvoter 需要详细说明,以便我可以改进我的答案吗?
    • @AndreasNiedermair 否决投票者可能复制粘贴了您的代码并且没有工作
    • 这是一个黄金答案!
    【解决方案3】:

    在这一切之后,我找到了一个新的更简单的方法试试这个..

    它可以将多张照片拼接在一起:

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

    【讨论】:

    • 效果很好。 g.Clear(Color.Transparent) 如果你想为动画精灵合并 PNG 图像
    • finalImage = new System.Drawing.Bitmap(width, height);高宽/高值抛出错误
    • @Anant Dabhi 好的,很抱歉带回一个老问题,但我将其转换为 VB.NET .. 如果我将它们放在一起,如果未使用的像素/空白,这会覆盖其他照片下一张图片上的像素是透明的吗?如果没有,有什么办法吗?
    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 2018-01-19
    • 2012-10-10
    • 2012-02-10
    • 1970-01-01
    • 2011-01-20
    相关资源
    最近更新 更多