none
How to convert images jpg from path to varbinary(MAX) in database ? RRS feed

  • Question

  • Problem

    How to convert images jpg from path to varbinary(MAX) in database in csharp windows form visual studio 2015 ?

    Details

    I have Table ImagesData have two fields 

    SpecialCode  nvarchar(20)

    imagebinary varbinary(max)

    I need to update images from path D:/Images to ImagesData Table based on SpecialCode

    to show more clear

    1- in my hard disk i have drive D have Folder Images so that path will be D:/Images

    2- In D:/Images Folder i have pictures JPG may be 100 images as following

    images Names in D:/Images Path as following

    0001-1 

    000002-5

    00001-3

    0004-2

    008523-1

    In my path  ImagesData Table my data as follwoing

    SpecialCode     imageBinary

    0001/1            

    000002/5

    00001/3

    0004/2

    actually when images Name exist on image path D:/Images matches specialcode in table

    update field imagebinary to binary by convert image on path D:/Images to binary then update it on field imagebinary when specialcode of image on path D:/images matches SpecialCode on table ImagesData

     if not matches not update it .

    Example

    if((imagepath 0001-1 == 0001/1 from table imagesData)

    {

    update field imagebinaryon table imagesData by convert image to binary it because it matches

    }

    else

    {

    not update because it not matched

    }

    Can you help me please on that

    to summarize what i need 

    i need to update field as binary when matched specialcode on table to images name on  path D:/Images


    Sunday, June 23, 2019 9:38 AM

Answers

  • Hi engahmedbarbary,

    Thank you for posting here.

    Based on your description, I have made a sample on my side, you can refer and modify it.

    Code:

            public byte[] imageToByteArray(System.Drawing.Image imageIn)
            {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
            public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                string connstring = @"Data Source=...";
                using (SqlConnection Conn = new SqlConnection(connstring))
                {
                    Conn.Open();
                    string path = @"D:\Images\img1.jpg";
                    byte[] b = imageToByteArray(Image.FromFile(path));
                    string sql = "INSERT INTO TableImage(SpecialCode,imagebinary) VALUES (@SpecialCode,@imagebinary)";
                    SqlCommand cmd = new SqlCommand(sql, Conn);
                    SqlParameter param = cmd.Parameters.Add("@imagebinary", SqlDbType.VarBinary);
                    param.Value = b;
                    SqlParameter param1 = cmd.Parameters.Add("@SpecialCode", SqlDbType.NVarChar);
                    param1.Value = "test1";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Success");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string connstring = @"Data Source=...";
                using (SqlConnection Conn = new SqlConnection(connstring))
                {
                    Conn.Open();
                    string sql = "select imagebinary from TableImage";
                    SqlCommand cmd = new SqlCommand(sql, Conn);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var m = (byte[])reader[0];
                        Image i = byteArrayToImage(m);
                        pictureBox1.Image = i;
                    }
                }
            }

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 10:18 AM

All replies

  • use File.ReadAllBytes to read file into a byte array then pass it along with file name to your update command (how you pass a parameter depends on what data access technology you are using, e.g. if you are using ADO.Net DataSet, search for "parameter ado.net dataset". If you are using ado.net entity framework, search for "parameter ado.net entity framework". 

    If you have no idea how to connect to a database, you should read an ADO.Net book first. There are many recommendations discussed previously in this forum. 




    Visual C++ MVP

    Sunday, June 23, 2019 12:50 PM
  • Hi engahmedbarbary,

    Thank you for posting here.

    Based on your description, I have made a sample on my side, you can refer and modify it.

    Code:

            public byte[] imageToByteArray(System.Drawing.Image imageIn)
            {
                MemoryStream ms = new MemoryStream();
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
            public Image byteArrayToImage(byte[] byteArrayIn)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                string connstring = @"Data Source=...";
                using (SqlConnection Conn = new SqlConnection(connstring))
                {
                    Conn.Open();
                    string path = @"D:\Images\img1.jpg";
                    byte[] b = imageToByteArray(Image.FromFile(path));
                    string sql = "INSERT INTO TableImage(SpecialCode,imagebinary) VALUES (@SpecialCode,@imagebinary)";
                    SqlCommand cmd = new SqlCommand(sql, Conn);
                    SqlParameter param = cmd.Parameters.Add("@imagebinary", SqlDbType.VarBinary);
                    param.Value = b;
                    SqlParameter param1 = cmd.Parameters.Add("@SpecialCode", SqlDbType.NVarChar);
                    param1.Value = "test1";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Success");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string connstring = @"Data Source=...";
                using (SqlConnection Conn = new SqlConnection(connstring))
                {
                    Conn.Open();
                    string sql = "select imagebinary from TableImage";
                    SqlCommand cmd = new SqlCommand(sql, Conn);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var m = (byte[])reader[0];
                        Image i = byteArrayToImage(m);
                        pictureBox1.Image = i;
                    }
                }
            }

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 10:18 AM