Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

c# - How to save graphics created on a PictureBox?

In c# and Visual Studio Windows forms I have loaded an image into a picture box (pictureBox2) and then cropped it and show in another picture box (pictureBox3).

Now I want to save what is inside pictureBox3 as an image file.

How can I do this?

private void crop_bttn_Click(object sender, EventArgs e)
{
    Image crop = GetCopyImage("grayScale.jpg");
    pictureBox2.Image = crop;

    Bitmap sourceBitmap = new Bitmap(pictureBox2.Image, 
                                     pictureBox2.Width, pictureBox2.Height);
    Graphics g = pictureBox3.CreateGraphics();

    g.DrawImage(sourceBitmap, new Rectangle(0, 0, 
                pictureBox3.Width, pictureBox3.Height), rectCropArea, GraphicsUnit.Pixel);
    sourceBitmap.Dispose();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Never use control.CreateGraphics! Either draw into a Bitmap bmp using a Graphics g = Graphics.FromImage(bmp) or in the Paint event of a control, using the e.Graphics parameter..

Here is a cropping code that draws into a new Bitmap and that makes use of your controls etc but changes a few things:

  • It uses a Graphics object that is created from a new Bitmap
  • It make use of using clauses to make sure it won't leak
  • It takes the size of the pictureBox3.ClientSize so it won't include any borders..

private void crop_bttn_Click(object sender, EventArgs e)
{
    Image crop = GetCopyImage("grayScale.jpg");
    pictureBox2.Image = crop;
    Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width, 
                                    pictureBox3.ClientSize.Height);
    using (Bitmap sourceBitmap = new Bitmap(pictureBox2.Image, 
                 pictureBox2.ClientSize.Width, pictureBox2.ClientSize.Height))
    {
        using (Graphics g = Graphics.FromImage(targetBitmap))
        {
            g.DrawImage(sourceBitmap, new Rectangle(0, 0, 
                        pictureBox3.ClientSize.Width, pictureBox3.ClientSize.Height), 
                        rectCropArea, GraphicsUnit.Pixel);
        }
    }
    if (pictureBox3.Image != null) pictureBox3.Image.Dispose();
    pictureBox3.Image = targetBitmap;
    targetBitmap.Save(somename, someFormat);
}

The alternative would be to..:

  • move all your code to the Paint event
  • replace the Graphics g = pictureBox3.CreateGraphics(); be Graphics g = e.Graphics;
  • insert these two lines to the click event:

Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width, 
                                pictureBox3.ClientSize.Height);
pictureBox3.DrawToBitmap(targetBitmap, pictureBox3.ClientRectangle);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...