1

私は、ファイルからの画像を重ねてペイントするPictureBoxを持っています(慣れている場合は、フォトショップのレイヤーの概念のように)。PNGであり、不透明度インデックスを備えているため、これらの画像は画像合成の最適な候補です。しかし、それを実行してファイルに保存する方法がわかりません。

次のコードサンプルでは、​​2つのPNG画像をビットマップオブジェクトにロードし、PictureBoxにペイントしました。

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Rectangle DesRec = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
    Bitmap bmp;
    Rectangle SrcRec;

    bmp = (Bitmap)Image.FromFile(Application.StartupPath + "\\Res\\base.png");
    SrcRec = new Rectangle(0, 0, bmp.Width, bmp.Height);

    e.Graphics.DrawImage(bmp, DesRec, SrcRec, GraphicsUnit.Pixel);

    bmp = (Bitmap)Image.FromFile(Application.StartupPath + "\\Res\\layer1.png");
    SrcRec = new Rectangle(0, 0, bmp.Width, bmp.Height);

    e.Graphics.DrawImage(bmp, DesRec, SrcRec, GraphicsUnit.Pixel);
}

コンポジションをファイル、できれば別のPNGファイルに保存するにはどうすればよいですか?

4

2 に答える 2

3

中間のメモリ内ビットマップへの描画を開始し、それを保存します(そして、本当に必要な場合は、最終的に画像ボックスに描画します)。このようなもの:

var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

using (var graphics = Graphics.FromImage(bmp))
{
    // ...
    graphics.DrawImage(...);
    // ...
}

bmp.Save("c:\\test.png", ImageFormat.Png);
于 2013-03-20T13:50:23.457 に答える
1

Thanks both of you. I've decided to do as Efran Cobisi suggested and changed the program so that it does the composing in memory first. Then I can use it where ever and however I want.

My new code to reflect the changes is as follows-

// Image objects to act as layers (which will hold the images to be composed)
Image Layer0 = new Bitmap(Application.StartupPath + "\\Res\\base.png");
Image Layer1 = new Bitmap(Application.StartupPath + "\\Res\\layer1.png");

//Creating the Canvas to draw on (I'll be saving/using this)
Image Canvas = new Bitmap(222, 225);

//Frame to define the dimentions
Rectangle Frame = new Rectangle(0, 0, 222, 225);

//To do drawing and stuffs
Graphics Artist = Graphics.FromImage(Canvas);

//Draw the layers on Canvas
Artist.DrawImage(Layer0, Frame, Frame, GraphicsUnit.Pixel);
Artist.DrawImage(Layer1, Frame, Frame, GraphicsUnit.Pixel);

//Free up resources when required
Artist.dispose();

//Show the Canvas in a PictureBox
pictureBox1.Image = Canvas;

//Save the Canvas image
Canvas.Save("MYIMG.PNG", ImageFormat.Png);

Apparently, the images (Canvas) are being saved with opacity index intact.

于 2013-03-20T15:45:59.963 に答える