如何将图像转换为二进制格式并保存/检索? [英] How to convert an image into binary format and save/retrieve it?

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

问题描述

大家好,

我需要你们的一点帮助.我有一些图片.我需要将图像以二进制格式存储在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

推荐答案

是的,您可以轻松地做到这一点.设置字段的数据类型
您要在其中存储图像的图像".现在,您必须将图像转换为字节数组(字节[]).现在通过查询或存储过程将数据插入表中.要检索图像,请使用反向过程.

要将图像或任何其他对象转换为字节数组,可以使用下面编写的函数.....
Yes you can do this easily. Set the datatype of the field
"Image" in which you want to store the Image. Now you have to convert the image in byte array(byte[]). Now by Query or Stored procedure insert data in the table. To retrieve the Image use reverse process.

To convert image or any other object to byte array you can use the function written below.....
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;
       }


在这里,仔细阅读本文,它具有比您想要的更多的内容: ^ ]
Here, go through this article, it has more than you seek: C# Photo Album Viewer[^]


您应该这样做:

you shuold do this:

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;

        }
       

       
    }


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆