C # simple code to implement PictureBox photo Save as disk file without error

Source: Internet
Author: User

When developing a personnel file system, it is common to use PictureBox to manipulate photos of people, including choosing to save photos, save photos, delete photos, such as:

Save photos to the database and delete from the database, users write a lot of practical code, very useful. However, when you save a photo displayed on PictureBox as a disk file, it is very easy to see "GDI + generic Errors". There are many suggestions on the Internet that do not meet my requirements, or the saved photos can not be displayed properly, or the code is very cumbersome. After I test, in fact, the method is very simple, C # a code can be implemented PictureBox photo saved as a disk file without error. The main code is as follows:

if (savefiledialog.showdialog () = = DialogResult.OK)

{

Picturename = Savefiledialog.filename;

if (picturebox1.image! = null)

{

Save Photo *********************************

using (MemoryStream mem = new MemoryStream ())

{

This sentence is very important, otherwise it can't save the picture correctly or error (the key is this one)

Bitmap bmp = new Bitmap (picturebox1.image);

Save to Memory

Bmp. Save (Mem, PictureBox1.Image.RawFormat);

Save to disk File

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

Bmp. Dispose ();

MessageBox.Show ("Save the photo successfully!") "," System Prompt ");

}

Save Photo *********************************

}

}

C # simple code to implement PictureBox photo Save as disk file without error

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.