Statement: the basics come from this: http://blog.163.com/chenjunfeng_2007/blog/static/119385083201019102742570/
1. open:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Namespace windowsformsapplication5
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
/* Use toolstripmenuitem to open a local image */
Private void open toolstripmenuitem_click (Object sender, eventargs E)
{
String picpath;
// Filter statement, "Open the lower part of the dialog box and ask for the displayed content | the real filter suffix name"
Openfiledialog1.filter = "image file (JPG, JPEG, BMP, GIF, ICO, pen, Tif) | *. JPG ;*. JPEG ;*. BMP ;*. GIF ;*. ICO ;*. PNG ;*. TIF ;*. WMF ";
Openfiledialog1.showdialog ();
Picpath = openfiledialog1.filename;
If (file. exists (picpath ))
{
Picturebox1.image = image. fromfile (picpath );
}
}
/* Use toolstripmenuitem to open a local image */
Private void save toolstripmenuitem_click (Object sender, eventargs E)
{
// Stream filestream;
Savefiledialog saveimagedialog = new savefiledialog ();
Saveimagedialog. Title = "save file instance ";
Saveimagedialog. Filter = @ "Jpeg | *. jpg | BMP | *. BMP | GIF | *. GIF ";
Saveimagedialog. showdialog ();
If (saveimagedialog. filename! = NULL)
{
// System. Io. Stream FS = (system. Io. Stream) saveimagedialog. openfile ();
// Filestream = new filestream (openfiledialog1.filename. tostring (), filemode. Open, fileaccess. Read );
String filename = saveimagedialog. filename. tostring ();
If (filename! = "" & Filename! = NULL)
{
/* Each character in the string is considered to have a digit position: the first character is 1, the second character is 2, and so on.
If the third parameter is not specified, the Child string from the position specified by the second parameter to the end of the string is returned.
If the parameter is not of the string type, the string () function is used to convert it to a string and then the conversion result is calculated.
Substring ("12345", 234) ---->)
Substring ("12345", 2) ------> (2345 )*/
/* Here fileextname is used as the switch statement's toggle, but there is a compilation Error */
String fileextname = filename. substring (filename. lastindexof ("."), 1). tostring ();
System. Drawing. imaging. imageformat = NULL;
If (fileextname! = NULL)
{
Switch (saveimagedialog. filterindex)
{
Case 1:
Imageformat = system. Drawing. imaging. imageformat. JPEG;
Break;
Case 2:
Imageformat = system. Drawing. imaging. imageformat. BMP;
Break;
}
If (imageformat = NULL)
{
Imageformat = system. Drawing. imaging. imageformat. JPEG;
}
Try
{
This. picturebox1.image. Save (filename, imageformat );
}
Catch
{
MessageBox. Show ("sorry, an exception occurred when saving the image ");
}
}
}
}
}
}
}