Click here to Skip to main content
12,819,421 members (28,890 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 20 Feb 2014

Extract Image Files from Database Image Data

, 20 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Converting data column (contains OLE db image binary Format) to image file (.jpg)


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).


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.
                cmd = new OleDbCommand("select * from data", cn);
                da = new OleDbDataAdapter(cmd);
                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;
                            bitmap.Save(path + "\\" + dr["file"].ToString() + ".jpg");
                            lblfile.Text = dr["file"].ToString();

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);
	return bmp;

Points of Interest

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

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:

Facebook Page Link :

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)

You may also be interested in...


Comments and Discussions

Questionconversion of data from .dbf file of Foxpro to jpg Pin
dsp16-Mar-15 9:05
memberdsp16-Mar-15 9:05 
AnswerRe: conversion of data from .dbf file of Foxpro to jpg Pin
PallavSingh6-Mar-15 21:06
professionalPallavSingh6-Mar-15 21:06 
QuestionExtract Image Files from Database Image Data Pin
Member 114838322-Mar-15 11:17
memberMember 114838322-Mar-15 11:17 
AnswerRe: Extract Image Files from Database Image Data Pin
PallavSingh6-Mar-15 21:02
professionalPallavSingh6-Mar-15 21: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 20 Feb 2014
Article Copyright 2014 by PallavSingh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid