C # simple code to save pictureBox photos as disk files without errors,

Source: Internet
Author: User

C # simple code to save pictureBox photos as disk files without errors,

When developing the Personnel Archive System, you generally need to use pictureBox to operate on the person's photos, including choosing to save the photos, saving other photos, and deleting the photos, such:

Save the photo to the database and delete it from the database. The user wrote a lot of practical code, which is very useful. However, if you want to save the photos displayed on pictureBox as disk files, a general error occurs in "GDI + ". There are a lot of suggestions on the Internet that do not meet my requirements. Either the saved photos cannot be displayed normally, or the code is very tedious. After my tests, the method is actually very simple. C # Can Save pictureBox photos as a disk file without making any mistakes. The main code is as follows:

If (SaveFileDialog. ShowDialog () = DialogResult. OK)

{

PictureName = SaveFileDialog. FileName;

 

If (pictureBox1.Image! = Null)

{

************* ********************

Using (MemoryStream mem = new MemoryStream ())

{

// This sentence is very important. Otherwise, the image cannot be properly saved or an error occurs (the key is this sentence)

Bitmap bmp = new Bitmap (pictureBox1.Image );

// Save to memory

// Bmp. Save (mem, pictureBox1.Image. RawFormat );

// Save the file to the disk

Bmp. Save (@ pictureName, pictureBox1.Image. RawFormat );

Bmp. Dispose ();

 

MessageBox. Show ("the photo is saved successfully! "," System prompt ");

}

************* ********************

}

 

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.