c#中文件与二进制流文件的转换

60 篇文章 0 订阅

c# SQL数据库image图片文件的保存于读取     

       SqlConnection con = new SqlConnection("Persist Security Info=False;User id=sa;pwd=123;database=WDERPDATA_001;server=(local)");
            con.Open();
            SqlDataAdapter dat = new SqlDataAdapter("select*from TLFGL_LFRY", con);
            DataSet set = new DataSet();
            dat.Fill(set);
            DataTable tb = set.Tables[0];
            DataRow rw = tb.Rows[0];
            DataColumn cl = tb.Columns[0];
            //MessageBox.Show( cl.ColumnName.ToString());
            byte[] MyData = new byte[0];
           // MyData=tb.Rows[1]["AU_LFRY_ZJZ"];
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

          
            Image _Image = Image.FromStream(new MemoryStream((byte[])tb.Rows[0]["AU_LFRY_ZJZ"]) );

            pictureBox1.Image = _Image;

 

 

            Bitmap bmmpp = new Bitmap(pictureBox1.Image);
            MemoryStream MS = new MemoryStream();
            //Bitmap bmp = new Bitmap(VisitorCheckIn.Class1.bmpname);
            bmmpp.Save(MS, ImageFormat.Bmp);
            Byte[] ImageData = new Byte[MS.Length];
            MS.Position = 0;
            MS.Read(ImageData, 0, Convert.ToInt32(MS.Length));
            string sql00 = "UPDATE  TLFGL_LFRY SET AU_LFRY_ZJZ=@Photo WHERE  AU_LFRY_ZJH='19850725'";
            SqlCommand dbCmd = new SqlCommand(sql00, con);
            dbCmd.CommandType = CommandType.Text;
            SqlParameter parameter = new SqlParameter("@Photo", SqlDbType.Image, int.MaxValue);
            parameter.Value = ImageData;
            dbCmd.Parameters.Add(parameter);
            dbCmd.ExecuteNonQuery();

 

 

 

将文件转换成二进制方法:

     /// <summary>
    /// 将文件转换成二进制
    /// </summary>
    /// <param name="Path">文件路径</param>
    /// <returns></returns>
    public static byte[] ConvertToBinary(string Path)
    {
        FileStream stream = new FileInfo(Path).OpenRead();
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
        return buffer;
    }

 

将二进制转换成文件方法:

using System.IO;

 

string filePath = "D://a.jpg";   //文件路径
byte[] br = ConvertToBinary(filePath);
FileStream fstream = File.Create(filePath, br.Length);
try
{  
     fstream.Write(br, 0, br.Length);   //二进制转换成文件
}
catch(Exception ex)
{
     //抛出异常信息
}
 finally
{
     fstream.Close();    

 

 

附加:二进制与图片互转

    Image aa = new Bitmap(@"E:/photo/tm.jpg");

    System.IO.MemoryStream stream
= new System.IO.MemoryStream();
     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter 
     = new   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, aa); //将图像序列化成二进制流
                        
            stream.Position
= 0;
            Image bb
= (Image)formatter.Deserialize(stream);  //将二进制流序列成Image

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值