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