这篇文章主要介绍了c#在sql中存取图片image示例,需要的朋友可以参考下

(1)控制台应用程序下演示插入图片

复制代码 代码如下:
public void InsertIMG() { //将需要存储的图片读取为数据流 FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read); Byte[] btye2 = new byte[fs.Length]; fs.Read(btye2 , 0, Convert.ToInt32(fs.Length)); fs.Close();

using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)"; SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image); par.Value = bt; cmd.Parameters.Add(par);

int t=(int)(cmd.ExecuteNonQuery()); if (t > 0) { Console.WriteLine("插入成功"); } conn.Close(); } }

(2)控制台应用程序下读出并生成图片到物理位置

复制代码 代码如下:
public void Read() { byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MyData = (byte[])sdr["ImgFile"];//读取第一个图片的位流 int ArraySize= MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限

FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write); fs.Write(MyData, 0, ArraySize); fs.Close();   //-- 写入到c:\00.jpg。 conn.Close(); Console.WriteLine("读取成功");//查看硬盘上的文件 } }

(3)Web下picshow.aspx页将图片读取出来并写入到浏览器上呈现

复制代码 代码如下:
public void Read() { byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MyData = (byte[])sdr["ImgFile"]; Response.ContentType = "image/gif"; Response.BinaryWrite(MyData); conn.Close(); Response.Write("读取成功"); }

(4)在web中可以如上picshow.aspx页面读取并显示图片,而真正引用该图片时如下示例

复制代码 代码如下:
<img src="picshow.aspx" width="500" height="300" />

(5)Winform下将图片写入到sql数据库image类型字段中的方法和以上方法基本一致,仅区别于可以利用多个对话框来帮助选取存储图片等,各个属性可以方便的利用上

(6)Winform下读取图片在picturebox控件中显示出来

方法一:利用MemoryStream 和System.Drawing.Image

复制代码 代码如下:
public void Read() { byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MyData = (byte[])sdr["ImgFile"];

MemoryStream mystream = new MemoryStream(MyData); //用指定的数据流来创建一个image图片 System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);

System.Windows.Forms.PictureBox picbox = new PictureBox(); picbox.Image = img; picbox.Left = 30; picbox.Top = 80; picbox.Width = 800; picbox.Height = 500; this.Controls.Add(picbox);

mystream.Close(); conn.Close(); } }

方法二:将流直接读取成图片并写入到物理位置,然后再行利用该图片呈现

复制代码 代码如下:
void Read() { using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read();

byte[] Image_img = (byte[])sdr["ImgFile"]; if (Image_img.Length == 0) { return; } int filelength = Image_img.Length; string imageName = "1.jpg"; string myUrl = Environment.CurrentDirectory + "\\" + imageName; FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write); BinaryWriter BW = new BinaryWriter(fs); BW.BaseStream.Write(Image_img, 0, filelength); BW.Flush(); BW.Close(); System.Windows.Forms.PictureBox picbox = new PictureBox();

//为picbox添加图片方法一 //picbox.ImageLocation = myUrl; //picbox.Width = 800; //picbox.Height = 300;

//为picbox添加图片方法二 Bitmap bitmap = new Bitmap(myUrl); picbox.Width = 100;//bitmap.Width; picbox.Height = 80;//bitmap.Height; picbox.Image = (Image)bitmap; picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; picbox.Left = 20; picbox.Top = 30;

this.Controls.Add(picbox); conn.Close();

} }

最新文章

  1. ios面试技巧
  2. U-boot与linux的关系
  3. [Leetcode19] Remove Nth Node From End of List
  4. log4j日志
  5. php + ajax + html 简单跨域问题
  6. CentOS 6.5升级Python后yum不可用的解决方案
  7. yii2
  8. Jack Straws(判断线段是否相交 + 并查集)
  9. sqlserver事务与回滚
  10. 设计模式 - 出厂模式(factory pattern) 详细说明
  11. sql表命名规范
  12. gridview中button事件处理
  13. C#中级-通过注册表读取Windows Service程序执行路径
  14. C# 同步更新网盘和本地的文件夹及文件
  15. 虚拟机oom
  16. egret请求参数
  17. FullBg-网页图片背景自适应大小
  18. 解决ajax请求(SpringMVC后台)响应415/400/405错误
  19. Qt-QML-C++交互实现文件IO系统-后继-读取XML文件和创建XML文件
  20. [SP16580]QTREE7

热门文章

  1. java连接SQL数据库(JDBC)相关设置
  2. UVA 10200 Prime Time 水
  3. hdoj1004--Let the Balloon Rise
  4. 初识Django---视图
  5. js数组的操作方法
  6. Redis缓存集群方案
  7. Q&amp;A:string、vector、iterator、bitset
  8. 结合两张表person和address
  9. Announcing the Release of ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 for VS2012
  10. 剑指offer--21.链表中倒数第k个结点