Extract Image Files from Database

Pallav Kumar Singh
23-01-2016 17:37:08
 Download Attachment(s)
2_1_SampleDatabase.zip
2_DBtoFile.zip
Rate:

This blog is useful for those who are hungry to learn. In this blog, I have converted image data column (contains OLE db image binary Format) to image file (.JPG). One of my friends asked me how to extract image from database and save it as an image file. Then I decided to give him this code. That's why I have developed this and it was very helpful for him.

Code Description

The first thing you know about extracting image from database is a MemoryStream. You should know about it. And you should know about Byte[] data type as well. Some knowledge about file handling in C# is better to understand the code in this project.

So, now here it is, we have to go for three simple steps. Think what you want to do is extract image from database and what you need to do.

        1. Select a path where you want to extract images.

FolderBrowserDialog folderDlg = new FolderBrowserDialog();

folderDlg.ShowNewFolderButton = true;

// Show the FolderBrowserDialog.

DialogResult result = folderDlg.ShowDialog();

if (result == DialogResult.OK)

{

    path = folderDlg.SelectedPath;

    lblpath.Text = "Path : " + folderDlg.SelectedPath;

    Environment.SpecialFolder root = folderDlg.RootFolder;

}  

        2. Select an access database or any other database.

OpenFileDialog fld1 = new OpenFileDialog();

fld1.InitialDirectory = @"C:\";

fld1.Multiselect = false;

fld1.Filter = "Access Database *.mdb|*.mdb";

fld1.Title = "Select Database";

DialogResult result = fld1.ShowDialog();

if (result == DialogResult.OK)

{

    cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fld1.FileName + ";Persist Security Info=False;Jet OLEDB:Database Password=";

    lbldb.Text = "DataBase Selected : " + fld1.FileName;

}

        3. Convert it into image files.

         cn.Open();

            cmd = new OleDbCommand("select * from data", cn);

            da = new OleDbDataAdapter(cmd);

            da.Fill(dt);

            if (dt.Rows.Count > 0)

            {

                foreach (DataRow dr in dt.Rows)

                {

                    if (dr["Img"] != DBNull.Value)

                    {

                        img = ByteArrToImg((Byte[])dr["Img"]);                        

                        Bitmap bitmap = new Bitmap(img);                        

                        Graphics graphics = Graphics.FromImage(bitmap);                        

                        graphics.SmoothingMode = SmoothingMode.AntiAlias;

                        graphics.Flush();

                        bitmap.Save(path + "\\" + dr["file"].ToString() + ".jpg");

                        graphics.Dispose();

                        img.Dispose();

                        lblfile.Text = dr["file"].ToString();

                    }

                }

            }

 cn.Close(); 

        4. In the last segment, I used a method ByteArrToImg((Byte[])dr["Img"]). In this method, I converted Byte data to Image format.  

Bitmap ByteArrToImg(byte[] b)

{

MemoryStream ms = new MemoryStream();

byte[] imgData = b;

ms.Write(imgData, 0, Convert.ToInt32(imgData.Length));

Bitmap bmp = new Bitmap(ms, false);

ms.Dispose();

return bmp;

If you are interested in doing some different work, then this is all for you.


C#,Images,Database



Sr. Software Engg. (India)
pallav.kumar837@gmail.com
8010265036








Search