c# winform 读取oracle中blob字段的图片并且显示到pictureBox里,保存进库

C#代码   收藏代码
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     OracleConnection conn = dbc.getConnection();//获得conn连接  
  4.     try  
  5.     {  
  6.         conn.Open();  
  7.         OracleCommand cmd = conn.CreateCommand();  
  8.         cmd.CommandText = "SELECT zp FROM kk.kkbj WHERE xh = 2345 ";//查询获得图片流  
  9.   
  10.         OracleDataReader reader = cmd.ExecuteReader();//创建一个OracleDateReader对象   
  11.         reader.Read();  
  12.   
  13.         MemoryStream ms = new MemoryStream((byte[])reader["zp"]);  
  14.   
  15.         Image image = Image.FromStream(ms, true);  
  16.   
  17.         reader.Close();  
  18.         conn.Close();  
  19.   
  20.         pictureBox1.Image = image;  
  21.     }  
  22.     catch (Exception ee)  
  23.     {  
  24.         MessageBox.Show(ee.Message.ToString());  
  25.     }  
  26.   
  27. }  


----------下边是上传和存入数据库有asp.net和winform(转)----------------------------------  

本文总结如何在.Net Winform和.Net webform(asp.net)中将图片存入sqlserver中并读取显示的方法 
    1,使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来 

    一,上传并存入SqlServer 

    数据库结构 
create table test 

     id identity(1,1), 
     FImage image 


    相关的存储过程 
Create proc UpdateImage 

     @UpdateImage Image 

As 
Insert Into test(FImage) values(@UpdateImage) 
GO 

    在UpPhoto.aspx文件中添加如下: 
C#代码   收藏代码
  1. <input id="UpPhoto" name="UpPhoto" runat="server" type="file">  
  2. <asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>  

    然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码: 
C#代码   收藏代码
  1. private void btnAdd_Click(object sender, System.EventArgs e)  
  2. {  
  3.         //获得图象并把图象转换为byte[]  
  4.         HttpPostedFile upPhoto=UpPhoto.PostedFile;  
  5.         int upPhotoLength=upPhoto.ContentLength;  
  6.         byte[] PhotoArray=new Byte[upPhotoLength];  
  7.         Stream PhotoStream=upPhoto.InputStream;  
  8.         PhotoStream.Read(PhotoArray,0,upPhotoLength);  
  9.   
  10.         //连接数据库  
  11.         SqlConnection conn=new SqlConnection();  
  12.         conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";  
  13.   
  14.         SqlCommand cmd=new SqlCommand("UpdateImage",conn);  
  15.         cmd.CommandType=CommandType.StoredProcedure;  
  16.   
  17.         cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);  
  18.         cmd.Parameters["@UpdateImage"].Value=PhotoArray;  
  19.   
  20.         //如果你希望不使用存储过程来添加图片把上面四句代码改为:  
  21.         //string strSql="Insert into test(FImage) values(@FImage)";  
  22.         //SqlCommand cmd=new SqlCommand(strSql,conn);  
  23.         //cmd.Parameters.Add("@FImage",SqlDbType.Image);  
  24.         //cmd.Parameters["@FImage"].Value=PhotoArray;  
  25.   
  26. conn.Open();  
  27. cmd.ExecuteNonQuery();  
  28. conn.Close();  
  29. }  

    二,从SqlServer中读取并显示出来 

    在需要显示图片的地方添加如下代码: 
C#代码   收藏代码
  1. <asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>  

    ShowPhoto.aspx主体代码: 
C#代码   收藏代码
  1. private void Page_Load(object sender, System.EventArgs e)  
  2. {  
  3.      if(!Page.IsPostBack)  
  4.      {  
  5.                 SqlConnection conn=new SqlConnection()  
  6.                 conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";  
  7.   
  8.                 string strSql="select * from test where id=2";//这里假设获取id为2的图片  
  9.                 SqlCommand cmd=new SqlCommand()  
  10.                 reader.Read();  
  11.                 Response.ContentType="application/octet-stream";  
  12.                 Response.BinaryWrite((Byte[])reader["FImage"]);  
  13.                 Response.End();  
  14.                 reader.Close();  
  15.      }  
  16. }  


    3,在winform中将图片存入sqlserver,并从sqlserver中读取并显示在picturebox中 

    1,存入sqlserver 

    数据库结构和使用的存储过过程,同上面的一样 

    1.1,在窗体中加一个OpenFileDialog控件,命名为ofdSelectPic 

    1.2,在窗体上添加一个打开文件按钮,添加如下单击事件代码: 
   
C#代码   收藏代码
  1. Stream ms;  
  2. byte[] picbyte;  
  3. //ofdSelectPic.ShowDialog();  
  4. if (ofdSelectPic.ShowDialog()==DialogResult.OK)  
  5. {  
  6.    if ((ms=ofdSelectPic.OpenFile())!=null)  
  7.    {  
  8.     //MessageBox.Show("ok");  
  9.     picbyte=new byte[ms.Length];  
  10.     ms.Position=0;  
  11.     ms.Read(picbyte,0,Convert.ToInt32(ms.Length));  
  12.     //MessageBox.Show("读取完毕!");  
  13.   
  14.     //连接数据库  
  15.     SqlConnection conn=new SqlConnection();  
  16.     conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";  
  17.   
  18.     SqlCommand cmd=new SqlCommand("UpdateImage",conn);  
  19.     cmd.CommandType=CommandType.StoredProcedure;  
  20.   
  21.     cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);  
  22.     cmd.Parameters["@UpdateImage"].Value=picbyte;  
  23.   
  24.     conn.Open();  
  25.     cmd.ExecuteNonQuery();  
  26.     conn.Close();  
  27.   
  28.     ms.Close();  
  29.     }  
  30.    }  

    2,读取并显示在picturebox中 

    2.1 添加一个picturebox,名为ptbShow 

    2.2 添加一个按钮,添加如下响应事件: 
     
C#代码   收藏代码
  1. SqlConnection conn=new SqlConnection();  
  2. conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";  
  3.   
  4. string strSql="select FImage from test where id=1";  
  5.   
  6. SqlCommand cmd=new SqlCommand(strSql,conn);  
  7.   
  8. conn.Open();  
  9. SqlDataReader reader=cmd.ExecuteReader();  
  10.         reader.Read();  
  11.   
  12. MemoryStream ms=new MemoryStream((byte[])reader["FImage"]);  
  13.   
  14. Image image=Image.FromStream(ms,true);  
  15.   
  16.         reader.Close();  
  17.         conn.Close();  
  18.   
  19. ptbShow.Image=image;  


黑色头发:http://heisetoufa.iteye.com
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值