2

同じサイズのPNGのリストがあります。すべての画像を並べて、長い画像を作成する必要があります。私は進歩していません。:)

すべての画像パスをList<>にロードし、画像の高さと画像数の長さに幅を掛けたビットマップを作成しました。(すべての画像は同じ寸法です)。

次に、各画像を確認し、基本的にビットマップの正しい位置に貼り付ける必要があります。誰かがこれを行うためのルーチンで私を助けることができますか?すべてのアイテムを繰り返し処理し、ビットマップに貼り付けます。

私はこれを試しましたが、次のように失敗します:一般的な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");
}

私はまだループをしていません。最初の画像を追加しようとしています。

4

1 に答える 1

0

回答あり!ロックされたファイルが原因でエラーが発生しました。実際には、私のフォルダに対する許可。そして、これは私が他の誰かを助けることを願っている素晴らしいルーチンです.

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();
                }
            }
        }
于 2012-07-22T10:03:05.200 に答える