How to save graphics created on a PictureBox?

后端 未结 2 1339
独厮守ぢ
独厮守ぢ 2020-12-12 02:05

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

相关标签:
2条回答
  • 2020-12-12 03:02

    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);
    
    0 讨论(0)
  • 2020-12-12 03:05

    The method PictureBox.CreateGraphics() should not be used unless you know what you are doing because it can cause some not-so-obvious problems. For example, in you scenario, the image in pictureBox3 will disappear when you minimize or resize the window.

    A better way is to draw to a bitmap, which you also can save:

    var croppedImage = new Bitmap(pictureBox3.Width, pictureBox3.Height);
    var g = Graphics.FromImage(croppedImage);
    g.DrawImage(crop, new Point(0, 0), rectCropArea, GraphicsUnit.Pixel);
    g.Dispose();
    //Now you can save the bitmap
    croppedImage.Save(...);
    pictureBox3.Image = croppedImage;
    

    Btw, please use more reasonable variable names, especially for pictureBox1..3.

    0 讨论(0)
提交回复
热议问题