简体   繁体   中英

Saving Image from a picture box ,image was drawn by graphics object

I have this code throwing exception on "pictureBox2.Image.Save(st + "patch1.jpg");" I think there is nothing saved on pictureBox2.Image ,But I have created graphics g on it. how do I save the image of pictureBox2.Image?

        Bitmap sourceBitmap = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
        Graphics g = pictureBox2.CreateGraphics();
        g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height),rectCropArea, GraphicsUnit.Pixel);
        sourceBitmap.Dispose();
        g.Dispose();
        path = Directory.GetCurrentDirectory();
        //MessageBox.Show(path);
        string st = path + "/Debug";
        MessageBox.Show(st);
        pictureBox2.Image.Save(st + "patch1.jpg");

A couple of problems.

First, CreateGraphics is a temporary drawing surface, not suitable for saving anything. I suspect you want to actually create a new image and display it in the second PictureBox:

Bitmap newBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(newBitmap)) {
  g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
}
pictureBox2.Image = newBitmap;

Secondly, use the Path.Combine function to create your file string:

string file = Path.Combine(new string[] { Directory.GetCurrentDirectory(), "Debug", "patch1.jpg" });
newBitmap.Save(file, ImageFormat.Jpeg);

That path has to exist or else the Save method will throw a GDI+ exception.

Graphics g = pictureBox2.CreateGraphics();

You should read up the documentation on this method you are calling, it's not at all what you want. It's for painting to a control outside of OnPaint, which is bad practice and will get overwritten by the next OnPaint, AND it has nothing to do with the PictureBox.Image property, absolutely nothing.

What are you actually trying to do? You want to save a crop of an image which is displayed in a PictureBox control? Do you need to have a preview of the crop operation before you save it to disk? Do you need to update this preview when the crop rectangle changes? Provide some more details.

Do it the other way round. Create a destination bitmap and a Graphics instance for that bitmap. Then copy the source picturebox image into that bitmap. Finally, assign that bitmap to the second picture box

Rectangle rectCropArea = new Rectangle(0, 0, 100, 100);
Bitmap destBitmap = new Bitmap(pictureBox2.Width, pictureBox2.Height);
Graphics g = Graphics.FromImage(destBitmap);
g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
g.Dispose();
pictureBox2.Image = destBitmap;
pictureBox2.Image.Save(@"c:\temp\patch1.jpg");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM