Click here to Skip to main content
15,886,026 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

Extract Image Files from Database Image Data

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
20 Feb 2014CPOL1 min read 39.2K   2.6K   8   5
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.

Image 1

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.

    C#
    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.
    C#
    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.
    C#
    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.  

C#
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)


Written By
Software Developer (Senior)
India India
I am Pallav Singh working as a Sr. Software Engg. in a software Company, New Delhi since 7 years. I have created a Page in facebook & Developed a Website for those who want to develop their Programming Skills & Logics.

My Coding Community:
https://www.esoftcode.com

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)

Comments and Discussions

 
Questionthe database Acces missing Pin
Daniel Truth Martireo19-Feb-20 19:33
Daniel Truth Martireo19-Feb-20 19:33 
Questionconversion of data from .dbf file of Foxpro to jpg Pin
dsp16-Mar-15 8:05
dsp16-Mar-15 8:05 
AnswerRe: conversion of data from .dbf file of Foxpro to jpg Pin
PallavSingh6-Mar-15 20:06
professionalPallavSingh6-Mar-15 20:06 
QuestionExtract Image Files from Database Image Data Pin
Member 114838322-Mar-15 10:17
Member 114838322-Mar-15 10:17 
AnswerRe: Extract Image Files from Database Image Data Pin
PallavSingh6-Mar-15 20:02
professionalPallavSingh6-Mar-15 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.