C# 二进制保存读取图片

ExpandedBlockStart.gif保存
FileStream fs = new FileStream(textBox2.Text.Trim(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            
byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 
0, bytes.Length);
            
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
            SqlConnection con 
= new SqlConnection(str);
            con.Open();
            
string sql = "insert into picture ([name],pic) values(@name,@pic)";
            SqlCommand cmd 
= new SqlCommand(sql, con);
            SqlParameter[] parm 
= new SqlParameter[]
                                      {
                                          
new SqlParameter("@name",SqlDbType.VarChar,50),
                                          
new SqlParameter("@pic",SqlDbType.Image,bytes.Length) 
                                         };
            parm[
0].Value = textBox1.Text;
            parm[
1].Value = bytes;
            cmd.Parameters.Add(parm[
0]);
            cmd.Parameters.Add(parm[
1]);
            
if (cmd.ExecuteNonQuery() > 0)
                MessageBox.Show(
"ok");
ExpandedBlockStart.gif读取
String sql = "select * from picture where [id]=2";
            
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
            SqlConnection con 
= new SqlConnection(str);
            con.Open();
            SqlCommand cmd
=new SqlCommand(sql,con);
            SqlDataReader sdr 
= cmd.ExecuteReader();
            
if(sdr.HasRows)
            {
                sdr.Read();
                
byte[] bytes = (byte[]) sdr["pic"];
                MemoryStream ms 
=new MemoryStream(bytes);
                Image image 
= Image.FromStream(ms);
                pictureBox1.Image 
= image;
            }
            sdr.Close();
            con.Close();
ExpandedBlockStart.gifHTML
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
        SqlConnection con 
= new SqlConnection(str);
        con.Open();
        
string sql = "select * from picture where [id]=2";
        SqlCommand cmd
=new SqlCommand(sql,con);
        SqlDataReader sdr 
= cmd.ExecuteReader();
        
if(sdr.HasRows)
        {
            sdr.Read();
            
byte[] bytes = (byte[]) sdr["pic"];
            Response.ContentType 
= "image/jpg";
            Response.OutputStream.Write(bytes,
0,bytes.Length);
            Response.End();
        }
        sdr.Close();
        con.Close();
//Response.ContentType = "text/HTML"
//Response.ContentType = "image/GIF"
//Response.ContentType = "image/JPEG"
//Response.ContentType = "text/plain"
//Response.ContentType = "image/JPEG"


转载于:https://www.cnblogs.com/itliyi/archive/2010/09/02/1816444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值