The code is as follows:
private void Btnupload_click (object sender, EventArgs e)
{
Create a dialog box object
OpenFileDialog ofd = new OpenFileDialog ();
Set a title for a dialog box
Ofd. Title = "Please select the uploaded image";
Set a filtered picture format
Ofd. Filter = "picture format |*.jpg";
Set whether to allow multiple selections
Ofd. MultiSelect = false;
If you click the OK button
if (OFD. ShowDialog () = = System.Windows.Forms.DialogResult.OK)
{
Get the full path to the file (including the suffix after the name)
String FilePath = Ofd. FileName;
Display the file path in a text box
Txtimgurl.text = FilePath;
Find the file name "1.jpg" in front of the "\" Location
int position = Filepath.lastindexof ("\ \");
The file name "1.jpg" is truncated from the full path.
String fileName = Filepath.substring (position+1);
Reads the selected file, returns a stream
using (Stream stream = ofd. OpenFile ())
{
Create a stream to write to the resulting file stream (Note: Create a folder named "Images", if you use a relative path, you must create it in the DEGUG directory of this program
If it's an absolute path, put it there, I'm using a relative path.
using (FileStream fs = new FileStream (@ "./images/" + fileName, FileMode.CreateNew))
{
Copy the resulting file stream into the write stream
Stream. CopyTo (FS);
Writes data from the write stream to a file
Fs. Flush ();
}
Pictruebox Display this picture, at this time this picture has been copied a copy in the images folder, it is equivalent to upload
As far as uploading to other places, you can change your mind, this is just the demo process.
Pbshow.imagelocation = @ "./images/" + fileName;
}
Uploads a picture to the specified folder and then displays it in the Pictruebox control on the form