C#把图片转换字节存入数据库在读取显示出来。

系统 1886 0
//获取图片的详细信息 并把图片转换到字节 

private void  button1_Click( object  sender, EventArgs e)
        {
             string  pPath = Application.StartupPath +  @" \QQ.jpg " ;
            FileInfo fi =  new  FileInfo(pPath);
             byte [] Temp = imageToByteArray(pPath);
            Image img = byteArrayToImage(Temp);
             int  W = img.Width;
             int  H = img.Height;

             string  Fm = fi.Extension; // 扩展名
             long  size = fi.Length;  //
             string  Fn = fi.Name;
             string  info =  " 名称: "  + Fn +  "   分辨率: "  + W +  " * "  + H;
            info +=  "   格式: "  + Fm +  "   大小: "  + ((size >  1024 ) ? (( float )(( float )size /  1024.0 )).ToString( " 0.00 " ) +  " KB "  : size +  " B " );
             this .label1.Text = info;
             this .pictureBox1.Height = H;
             this .pictureBox1.Width = W;
             this .pictureBox1.Image = img;
        }

         private   void  button2_Click( object  sender, EventArgs e)
        {
             string  pPath = Application.StartupPath +  @" \QQ.jpg " ;
             byte [] Temp = imageToByteArray(pPath);
            StringBuilder Sb =  new  StringBuilder();
             for  ( int  i =  0 ; i < Temp.Length; i++)
            {
                Sb.Append(Temp[i].ToString());
            }
            richTextBox1.Text = Sb.ToString();

        }
         ///   <summary>
        
///  图片转为Byte字节数组
        
///   </summary>
        
///   <param name="FilePath"> 路径 </param>
        
///   <returns> 字节数组 </returns>
         private   byte [] imageToByteArray( string  FilePath)
        {
             using  (MemoryStream ms =  new  MemoryStream())
            {

                 using  (Image imageIn = Image.FromFile(FilePath))
                {

                     using  (Bitmap bmp =  new  Bitmap(imageIn))
                    {
                        bmp.Save(ms, imageIn.RawFormat);
                    }

                }
                 return  ms.ToArray();
            }
        }
         ///   <summary>
        
///  字节数组生成图片
        
///   </summary>
        
///   <param name="Bytes"> 字节数组 </param>
        
///   <returns> 图片 </returns>
         private  Image byteArrayToImage( byte [] Bytes)
        {
             using  (MemoryStream ms =  new  MemoryStream(Bytes))
            {
                Image outputImg = Image.FromStream(ms);
                 return  outputImg;
            }
         }

 

 

//从数据库里面读取出来

private void button1_Click(object sender, EventArgs e)

        {

          openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

            if(openFileDialog1.ShowDialog()==DialogResult.OK)

            {

              string fullpath =openFileDialog1.FileName;//文件路径

              FileStream fs = new FileStream(fullpath, FileMode.Open);

                byte[] imagebytes =new byte[fs.Length];

                BinaryReader br = new BinaryReader(fs);

                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));

                //打开数据库

                SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");

                con.Open();

                SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);

                com.Parameters.Add("ImageList", SqlDbType.Image);

                com.Parameters["ImageList"].Value = imagebytes;

               com.ExecuteNonQuery();

               con.Close();

             }    

}

 

C#把图片转换字节存入数据库在读取显示出来。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论