Upload a picture to the specified folder and then display it in the Pictruebox control on the form

Source: Internet
Author: User

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

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.