C#把圖片轉換字節存入數據庫在讀取顯示出來。


//獲取圖片的詳細信息 並把圖片轉換到字節 

privatevoid 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();

             }    

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM