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 ");
}
************* ********************
}
}