Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Extract Image Files from Database Image Data

0.00/5 (No votes)
20 Feb 2014 1  
Converting data column (contains OLE db image binary Format) to image file (.jpg)

Introduction

This tip is useful for those who are hungry to learn. In this tip, I have converted image data column (contains OLE db image binary Format) to image file (.JPG).

Background  

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.

Using the Code

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(); 

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;
} 

Points of Interest

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here