How to save pictures in the database?

      In the development process, will inevitably encounter problems save the picture, there are many ways to solve, here I put the pictures saved to the database in binary form, this form may not be the most efficient way, but also a good method . Ha ha, the following simple demo can be used as reference:
 1 Code #region Code
 2
 3         //单击"浏览"按钮
 4
 5         private void button1_Click(object sender, System.EventArgs e)
 6
 7         {
 8
 9              DialogResult result=this.openFileDialog1.ShowDialog();
10
11              if(result==DialogResult.OK)
12
13              {
14
15                   this.textBox1.Text=this.openFileDialog1.FileName.ToString
16
17();
18
. 19                    Image IMG  =  Bitmap.FromFile ( the this .textBox1.Text);
20 is 21 is the this .pictureBox1.Image = IMG; 22 is 23 is               }
                   

24 25 26 is 27          }

       

2829//click "OK" button3031 isPrivatevoid the button2_Click (Object SENDER, System.EventArgs E)3233 is { 34 is 35 // into the database operation, picture type parameters PicToBinary () returns the byte [] to 36 37 [ the picture stored in the database in the form of byte 38 is 39

         

          

         

              



         }
40 41 is // convert the picture array is byte 42 is 43 is Private byte [] PicToBinary () 44 is 45 { 46 is 47 // Create a set of parameters 48 49 String  path  = the this .textBox1.Text.Trim (); 50 51 is byte [] Source  = null ; 52 is 53 is IF ( ! path.Equals ( "" &&  the File.Exists (path)) 54 is 55 { 56 is 57 is                    the FileStream FS =

         

          

         

              

               

               

              

              

new new  the FileStream
58 59 (path, FileMode.Open, FileAccess.Read); // Create a file stream 60 61 is                    Source = new new byte [( int ) fs.Length]; 62 is 63 is                    fs.Read (Source, 0 , ( int ) FS .length); 64 65                    image IMG  =  Bitmap.FromStream (FS); // the file stream is converted to image 66 67 IF (img.Width  > 300 ||  img.Height  > 400 ) 68 69 {


 





                      

                   
70 71 is                        MessageBox.Show ( " image is too large. Please upload images 400 * 300 or less " ); 72 73 is return ; 74 75                    }


                       

76 77                    fs.Flush (); 78 79                    fs.Close (); 80 81               }                   





8283return Source;8485         }

              

86878889#endregion

 

         

Reproduced in: https: //www.cnblogs.com/yangjie5188/archive/2008/02/16/1070437.html

Guess you like

Origin blog.csdn.net/weixin_33969116/article/details/93499768