c# - 从多个 PNG 创建一个 PNG

标签 c# graphics

我有一个相同大小的 PNG 列表。我需要创建一个长图像,所有图像彼此相邻。我没有进步。 :)

我已经将所有图像路径加载到一个列表<>中,然后创建了一个位图,其中图像的高度和图像数量的长度乘以它们的宽度。 (所有图像尺寸相同)。

然后我需要遍历每个图像,基本上将其粘贴到我的 BitMap 中的正确位置。任何人都可以帮助我做这件事吗?遍历我的所有项目 - 并将它们粘贴到我的位图上。

我试过了,但失败了:通用 GDI 错误。

using(Bitmap newFiles = new Bitmap(outputFileWidth, outputFileHeight))
{
     using(Graphics graphics = Graphics.FromImage(newFiles))
     {
         graphics.DrawImage(
            testImage,
            new Rectangle(0, 0, originalWidth, originalHeight),
            new Rectangle(new Point(), testImage.Size),
            GraphicsUnit.Pixel);
     }
     newFiles.Save(@"c:\test.png");
}

我还没有完成循环。我只是想添加第一张图片。

最佳答案

找到答案!错误是由于锁定的文件。实际上,我的文件夹的权限。这是一个很棒的例程,我希望能帮助到其他人。

public Bitmap Combine()
        {
            //read all images into memory
            List<Bitmap> images = new List<Bitmap>();
            Bitmap finalImage = null;

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

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

                    //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 (Graphics g = Graphics.FromImage(finalImage))
                {
                    //set background color
                    g.Clear(Color.Black);

                    //go through each image and draw it on the final image
                    int offset = 0;
                    foreach (Bitmap image in images)
                    {
                        g.DrawImage(image,
                          new 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 (Bitmap image in images)
                {
                    image.Dispose();
                }
            }
        }

关于c# - 从多个 PNG 创建一个 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11598805/

相关文章:

JAVA背景动画(LinearGradientPaint)

c# - 运行程序的多个实例以加快进程?

c# - Windows.System.UserProfile.UserInformation.NameAccessAllowed 总是返回 false

c# - 处理 WebBrowser 控件上的关键事件?

c# - Ninject 中的泛型和属性绑定(bind)

java - 在 Java 中扩展 Graphics 类

c# - WPF - 屏幕保护程序图形性能改进

c# - 未定义表达式参数

graphics - 在 Love2D 中剪切多边形以仅在圆内绘制

java - 旋转图像对象