本文介绍了如何将图像转换为二进制格式并保存/检索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要你们的一点帮助.我有一些图片.我需要将图像以二进制格式存储在sql server数据库中,并且从数据库检索并放置在Windows应用程序的图片框中时,同一图像必须从二进制转换回.

你能帮我做到这一点吗?

谢谢与问候,
Venkata Sandeep P

Hi Everybody,

I want small help from you guys. I have some images. I need the images to store in sql server database with the format of binary and the same image has to convert back from binary while retrieving from database and placed in picture box in windows application.

Can you please help me to do this.

Thanks& Regards,
Venkata Sandeep P

推荐答案

public byte[] ObjectToByteArray(Object obj)
        {
            if (obj == null)
                return null;
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }


将字节数组转换为对象


Convert a byte array to an Object

public Object ByteArrayToObject(byte[] arrBytes)
      {
           MemoryStream memStream = new MemoryStream();
           BinaryFormatter binForm = new BinaryFormatter();
           memStream.Write(arrBytes, 0, arrBytes.Length);
           memStream.Seek(0, SeekOrigin.Begin);
           Object obj = (Object)binForm.Deserialize(memStream);
           return obj;
       }



using system.io;
using system.data.sqlclient; 



public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection();
        string _path;
        public Form1()
        {
            InitializeComponent();
        }

        //convert Image to binary and save in DB
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                    _path = openFileDialog1.FileName;

                   
                    InsertInSQL(_path);
                    
                
            }
          
        }

        private void InsertInSQL(string _path)
        {
            con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
        string strQ = "insert into dbo.PicTBL(Pic)values(@p)";
        SqlCommand command = new SqlCommand(strQ,con);
        command.Parameters.AddWithValue("@p",ImageToBinary(_path));
        con.Open();
        command.ExecuteNonQuery();
        con.Close();
        }      

        public static byte[] ImageToBinary(string _path)
        {
            FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);
                   byte[] b = new byte[fS.Length];
                   fS.Read(b, 0, (int)fS.Length);
                   fS.Close();
                   return b;
        }

                    
 



         //Convert Binary to imge and save in a folder



        private void button1_Click_1(object sender, EventArgs e)
        {
                     

            DataTable dt = Rimage();

            foreach (DataRow row in dt.Rows)
            {
                byte[] b = (byte[])row["Pic"];
                Image img = BinaryToImage(b);
                img.Save("D:\\NewFolder\\" + row["ID"].ToString() + ".jpg");
            }

            
       }

         private Image BinaryToImage(byte[] b)
          {
            if (b == null) 
                return null;
         
    
           MemoryStream memStream = new MemoryStream();
           memStream.Write(b, 0, b.Length);

           return Image.FromStream(memStream);
 	
           }

               private DataTable Rimage()
                 {
                 con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select * from dbo.PicTBL";
                 cmd.Connection = con;
                 SqlDataAdapter adp = new SqlDataAdapter(cmd);
                 DataTable dt = new DataTable();
                 con.Open();
                 adp.Fill(dt);
              
                 return dt;

        }
       

       
    }


这篇关于如何将图像转换为二进制格式并保存/检索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:02