首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从多个PNG创建单个PNG

从多个PNG创建单个PNG
EN

Stack Overflow用户
提问于 2012-07-22 16:33:00
回答 1查看 575关注 0票数 2

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

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

然后我需要遍历每个图像,并基本上将其粘贴到我的BitMap中的正确位置。有没有人可以帮我做个例行公事?遍历我的所有项目-并将它们粘贴到我的位图上。

我已经尝试过了,但是失败了: Generic GDI Error。

代码语言:javascript
复制
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");
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-22 18:03:05

答案已找到!错误是由锁定的文件引起的。实际上,是对我文件夹的权限。这是一个奇妙的例行公事,我希望它能帮助其他人。

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

https://stackoverflow.com/questions/11598805

复制
相关文章

相似问题

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