怎么可以将数据库中的图片显示到页面上

a8265869 2013-09-09 02:18:18
在数据库已经创建好,并且在数据库中存入了,图片的地址,怎么样可以将图片显示到页面,或者各位大大有什么好的方法。求详细教程。本人刚开始研究这方面。
...全文
1113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
men52676521 2013-09-09
  • 打赏
  • 举报
回复
你要确定图片存的位置存在,最好是直接存二进制文件保险,缺点是占地方。用4楼的就可以(路径的话)
wo594261 2013-09-09
  • 打赏
  • 举报
回复

//取图片
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(" into tb_08 values(@ImageList)",con);

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

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

               com.uteNonQuery();

               con.Close();

             }    

}
wo594261 2013-09-09
  • 打赏
  • 举报
回复

//存图片

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;
             }
         }

t_kong 2013-09-09
  • 打赏
  • 举报
回复
如果是winform的话,拉个PictureBox 然后 picture1.Image = Image.FromFile("图片地址");
voodooer 2013-09-09
  • 打赏
  • 举报
回复
如果你是开发web项目的话,

<img src="读取数据库的图片地址"  />
人生导师 2013-09-09
  • 打赏
  • 举报
回复
数据库中存储的图片地址,页面上可以添加Image控件,然后指定控件的source为图片地址就可以了
a8265869 2013-09-09
  • 打赏
  • 举报
回复
好像有种方法是用代码绘制这个图片,只是在数据库中存储一个图片,有代码绘制。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧