首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在C#/.NET中合并两个图像

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

Stack Overflow用户
提问于 2009-01-21 12:52:44
回答 3查看 143.8K关注 0票数 94

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

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-01-21 12:59:58

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

代码语言:javascript
复制
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) { }
    }
}
票数 106
EN

Stack Overflow用户

发布于 2009-01-21 12:58:41

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

代码语言:javascript
复制
using (Graphics grfx = Graphics.FromImage(image))
{
    grfx.DrawImage(newImage, x, y)
}

图形位于命名空间System.Drawing

票数 63
EN

Stack Overflow用户

发布于 2012-07-13 22:56:56

在这之后,我发现了一个新的更简单的方法试试这个..

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

代码语言:javascript
复制
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();
        }
    }
}
票数 36
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/465172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档