Read save file, image upload read in database

Source: Internet
Author: User
Tags save file

I. Input/output stream

1. Concept:

The input and output stream is primarily used to save, read, and store the contents of the file in memory.

2. How to use:

Using System.IO;

The System.IO namespace contains types that allow you to read and write files and data streams, and types that provide basic file and directory support.

3. The code snippet realizes the reading and saving function:

Read text:

OpenFileDialog op = new OpenFileDialog (); Op.showdialog ()//Open File Dialog//read the selection into the document StreamReader SD = new StreamReader (op. Filename,system.text.encoding.default);//write text box in This.textBox1.Text = SD. ReadToEnd (); Sd. Close ();//Closed stream

To save the document:

SaveFileDialog save = new SaveFileDialog (), if (save. ShowDialog () = = DialogResult.OK) {       string filename = save. FileName;       StreamWriter SW = new StreamWriter (filename);//Open a hard disk space       SW. Write (this.textBox1.Text);//writes the text content       SW. Close ();}

Second, the image upload to the database, read

1. Upload

Typically, the image is converted into binary data after uploading:

            Openfiledialog1.filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif"; Rules can only read the picture format, read a picture from memory            DialogResult dia = Openfiledialog1.showdialog ();            if (dia = = DialogResult.OK)            {                string filename = openfiledialog1.filename;                FileStream fs = new FileStream (filename, FileMode.Open, FileAccess.Read);//Read the picture into the stream                byte[] imagebytes = new Byte[fs. length];//binary Array to temporarily store the binary encoding of the image                BinaryReader br = new BinaryReader (fs);//binary reader                imagebytes = br. Readbytes (Convert.ToInt32 (fs. Length));//Read the picture into a binary array            }    

Then connect the database and upload the imagebytes data to the library.
2. Read

First, the image data in the database is read, and the object defining a byte[] type is used to receive.

            Write the image to memory            MemoryStream ms = new MemoryStream (imgbytes, 0, Imgbytes. Length);            Ms. Write (imgbytes, 0, Imgbytes. Length);            Image img = image.fromstream (ms);            Build a PictureBox control, accept the read to the picture            This.pictureBox1.SizeMode = pictureboxsizemode.zoom;            This.pictureBox1.Image = img;

Read save file, image upload read in database

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.