How to save a picturebox control as a jpeg file after it's edited

how to save image from picturebox to folder in vb net
save picturebox image to file vb net
save image in c#
c save image from picturebox
a generic error occurred in gdi+.
save image in windows application c#
how to save a file in a folder using c# windows application
how to save image in folder in c#

I have a PictureBox on my Windows Forms application.

I load a picture in it and I have enabled the Paint event in my code. It draws a rectangle.

Like this:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics gr = e.Graphics;
    Pen p = new Pen(Color.Red);
    p.Width = 5.0f;
    gr.DrawRectangle(p, 1, 2, 30, 40);
}

And I click the "save" button:

private void button2_Click(object sender, EventArgs e)
{
    pictureBox1.Image.Save(@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000test.jpg",ImageFormat.Jpeg);
}

But the saved file never contains the rectangle that I drew.

Does anyone have any idea?

You probably shouldn't draw directly on the PictureBox.

You need to use a Bitmap instead. Try putting the bitmap in the PictureBox.Image and then call Save().

Check this for more details

c#, You probably shouldn't draw directly on the PictureBox. You need to use a Bitmap instead. Try putting the bitmap in the PictureBox.Image and then call Save​(). (B) Load the bitmap into the control.Image (so you can see the changes caused by the draw) (C) Save the control.Image (2-option) You have a global (or passed) bitmap (probably from a real file) (A) Draw on the bitmap (B) Load the bitmap into the control.Image (so you can see the changes) (C) Save the bitmap

Thanks.Your anwers all helped. This worked

        private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.ImageLocation=@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000.jpg" ;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox1.Image.Save(@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000test.jpg",ImageFormat.Jpeg);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Image);
        Graphics gr = Graphics.FromImage(bmp);
        Pen p = new Pen(Color.Red);
        p.Width = 5.0f;
        gr.DrawRectangle(p, 1, 2, 30, 40);
        pictureBox1.Image = bmp;
    }

[Solved] picture box image save in folder using C# windows , Image.Save method to save the image of the PictureBox to a file. /c-sharp-how-​to-save-a-picturebox-control-as-a-jpeg-file-after-its-edited[^] If you want a more generic procedure to save from file or picturebox, create a procedure which accepts a byte array as a param. The button click for Save PictureBoxImage can convert the image to byte array and pass it, as can the other button working from a file: Function SaveRecord(ingData As Byte(),other params) As Boolean

Here is my solution with additional support to various file types:

    public void ExportToBmp(string path)
    {
        using(var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height))
        {
        pictureBox.DrawToBitmap(bitmap, pictureBox.ClientRectangle);
        ImageFormat imageFormat = null;

        var extension = Path.GetExtension(path);
        switch (extension)
        {
            case ".bmp":
                imageFormat = ImageFormat.Bmp;
                break;
            case ".png":
                imageFormat = ImageFormat.Png;
                break;
            case ".jpeg":
            case ".jpg":
                imageFormat = ImageFormat.Jpeg;
                break;
            case ".gif":
                imageFormat = ImageFormat.Gif;
                break;
            default:
                throw new NotSupportedException("File extension is not supported");
        }

        bitmap.Save(path, imageFormat);
        }
    }

Saving Images, You'll be glad to know that saving an image is quite easy! Add a SaveFileDialog control to your form from the C# toolbox. Change the Name property As the first line of your if statement add these two lines: C#. savePath = saveFD. The next line just gets the picture from the PictureBox, as before. To do the actual saving,  Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object. To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object.

Here is a small example that clarified a few things for me (I was struggling with this a bit too).

pBox is a PictureBox on Form1, make it at least 50x50

appPath was derived from System.Reflection but use any path you like

There are two buttons, one for drawing, one for saving, their click events are in the code below.

Things I learned:

(1) "pBox.Image =" doesn't do anything but initialize the pBox image, it DOES NOT have to be a filename as EVERY example I found used (had problem saving to that same file because it was share locked). Also, if your goal is to see things on the entire control's surface, you'll probably like setting the size at initialize time to the size you need. I used the pBox's size in this example but normally I use the bitmap size (because I typically begin with a real picture file).

(2) I always had problems either seeing my draws show up on the control or seeing my changes saved in the output file (or both). In my prior attempts I would duplicate the draws both on the control and on the bitmap. Of course that isn't necessary but the edited bitmap DOES need to be reloaded into the control.image... and THAT was the piece of this puzzle I was missing.

(A) Create a bitmap from the control.image and draw on the bitmap

(B) Load the bitmap into the control.Image (so you can see the changes caused by the draw)

(C) Save the control.Image

(2-option) You have a global (or passed) bitmap (probably from a real file)

(A) Draw on the bitmap

(B) Load the bitmap into the control.Image (so you can see the changes)

(C) Save the bitmap

    public Form1()
    {
        InitializeComponent();
        pBox.Image = new Bitmap(pBox.Width, pBox.Height);  
    }

    private void DrawStuff1_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(pBox.Image);  
        Graphics g = Graphics.FromImage(bmp);

        g.FillRectangle(Brushes.Red, 5, 5, 25, 25); //hard-coded size to reduce clutter

        pBox.Image = bmp;  //this makes your changes visible
    }

    private void Save_Click(object sender, EventArgs e)
    {
        pBox.Image.Save(appPath + "SavedImage.bmp");
    }

Image.Save Method (System.Drawing), The code saves the bitmap to three JPEG files, each with a different quality level. If any additional data has been written to the stream before saving the image,  Im trying to insert into my table some image from picturebox: MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); byte[] photo = new byte[ms.Length]; ms.Pos

You need paint to image of picture, not to the Graphics control on Paint event.

EDIT:

using( Graphics g = Graphics.FromImage( pictureBox1.Image ) ) {
    // there you will be do, what you do in Paint event
}

// ... somewhere else ...
pictureBox1.Save( _required_parameters_ );

HOW TO: Copy a Picture from a Database Directly to a PictureBox , how to copy an image stored in a database directly into a PictureBox control on a Windows Form without needing to save the image to a file. Tables["BLOBTest​"].Rows.Count; if(c>0) { //BLOB is read into Byte array, then  What I did was to put the jpg as image1, image2, image3, and to put these images in the form one below another. The end result is that the last image falls far below the form and the screen height. Using your program to save the image to file, the output stores only those images that are viewable in the screen, not those below the screen.

VB.Net, In this article, I am going to explain how to use a PictureBox on a Windows. The Image property of the control allows you to set an image both at Gets or sets the image displayed in the control when the main image is loaded We can open and save the picture using saveFileDialog and openFileDialog. Saving Image from webbrowser control Hi,I was wondering how I could save an image from a webbrowser control to a file or picturebox. this attempt fails wi. I'll cover the following topics in the code samples below: IHTMLDocument2IHTMLElement, GetElementsByTagName, CreateCompatibleDC, HttpWebResponse, and HttpWebRequest.

Using PictureBox In Windows Forms, Learn how to save JPEG files with the proper encoding to reduce space as well as to reduce creation speed. Digital Controls · Extending/Subclassing Techniques · File and Directory choosers · Grid Add the following code to save the PictureBox's image: Have a close look at the file size after the save. In the example below, there is a Button control with an image assigned to it. When you click the button, a SaveFileDialog component is instantiated with a filter that allows files of type .gif, .jpeg, and .bmp. If a file of this type is selected in the Save File dialog box, the button's image is saved.

Saving Images: EncoderParameter / ImageCODEC Versus Bitmap , Select an image bit depth (usually either 4, 8, 16, or 24), and type it into the constant. Go to Programming » Graphics and Sound » Graphics 

Comments
  • You shold usie the "using( ... ) { ... }" pattern. Now, you can have some memory leaks. And if some answer solve your answer, you should accept it ;).
  • Hi Can you clarify/give an example ?