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