Click here to Skip to main content
Click here to Skip to main content
Go to top

Extract Image Files from Database Image Data

, 20 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PallavSingh
Software Developer (Senior)
India India
I am Pallav Singh working as a Sr. Software Engg. in a software Company in New Delhi since 5 years. I have created a Page in facebook for those who like to develop their Programming Skills & Logics.
 
Facebook Page Link :
https://www.facebook.com/SoftwareCode
 
You can go on this page and ask any questions related to programming and logic (C#, ASP.Net, Sql Server, Oracle, Java, Foxpro, Access, Windows etc)
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Feb 2014
Article Copyright 2014 by PallavSingh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid