合并两个影像创建于C#.NET一个单一的形象影像、形象、两个、创建于

2023-09-02 10:55:46 作者:抬头展望45°天空

我有一个要求,其中我需要合并产生到使用C#.NET单个图像两种不同PNG / JPEG图像。将有源图像上定义的,其中我需要插入另一个图像的特定位置。任何人都可以提出一些链接?

I have a requirement wherein I need to merge two different png/jpeg images resulting into a single image using C#.Net. There will be a particular location defined on the source image wherein I need to insert another image. Can anybody suggest some links ?

推荐答案

这个方法合并两个图像之一,你可以修改code,以满足您的需求,其他的顶部:

This method merge two images one in the top of the other you can modify the code to meet for your needs:

    public static Bitmap MergeTwoImages(Image firstImage, Image secondImage)
    {
        if (firstImage == null)
        {
            throw new ArgumentNullException("firstImage");
        }

        if (secondImage == null)
        {
            throw new ArgumentNullException("secondImage");
        }

        int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;

        int outputImageHeight = firstImage.Height + secondImage.Height + 1;

        Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        using (Graphics graphics = Graphics.FromImage(outputImage))
        {
            graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size),
                new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel);
            graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),
                new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);
        }

        return outputImage;
    }