Click here to Skip to main content
15,302,185 members
Articles / Database Development
Posted 23 Aug 2011


9 bookmarked

Loading an Image and Saving it into a database

Rate me:
Please Sign up or sign in to vote.
4.92/5 (9 votes)
23 Aug 2011CPOL
How to load an image from the filesystem and save it into a Database
As part of a project I required to load an image, display it in a picture box and save it into a database. Quite simple really ( at least in theory ).

The whole process can be summarized in the following steps:
1.Load the image file into a picture box
private void btnBadLoad_Click(object sender, EventArgs e) {
  OpenFileDialog dlg = new OpenFileDialog();
  DialogResult dr = dlg.ShowDialog();
  if (dr == DialogResult.OK) {

2.Convert the image from the picture box into an array of bytes using a stream.
private byte[] ImageToByteArray(Image img,ImageFormat format) {
  byte[] result;
  try {
    MemoryStream ms = new MemoryStream();
    img.Save(ms , format);
    result = ms.ToArray();
  } catch (Exception ex) {
  return result;

3.Create an insert command and pass the byte array as a parameter
DbCommand cmd =  _dbu.CreateTextCommand(insQ);
cmd.AddParameter("@ImageId", id);
cmd.AddParameter("@ImageData", imageBytes);

During step 2 a rather obnoxious "Generic GDI+ Exception" appeared without apparent reason.After some research I found this is a designed behavior, as described in the following article.
Apparently when an image is created from a file it gets locked allong with the file and no further streaming is possible.

The solution to this quandary is to change the way the image is loaded in step 1, disposing of the file-locked bitmap as fast as possible.
private void btnLoad_Click(object sender, EventArgs e) {
  OpenFileDialog dlg = new OpenFileDialog();
  DialogResult dr = dlg.ShowDialog();
  if (dr == DialogResult.OK) {
    //Create the file-locked bitmap
    Bitmap b1 = new Bitmap(dlg.FileName);
    //Copy it into the picture image
    pbPicture.Image = (Bitmap)b1.Clone();
    //Say "Hasta la vista baby" to the file-locked bitmap

Happy image saving !!


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


About the Author

Armando de la Torre
Software Developer (Senior) Self-employed
Mexico Mexico
Senior SAP Consultant ( ABAP / MM / Workflow ).
Delphi Developer
C# Asp Developer

Comments and Discussions

GeneralReason for my vote of 5 Very nice solution, Thanks !! Pin
Sunny_Kumar_31-Oct-11 21:21
MemberSunny_Kumar_31-Oct-11 21:21 
GeneralWhy won't you read directly the file content and write it in... Pin
DNikolov29-Aug-11 23:10
MemberDNikolov29-Aug-11 23:10 
GeneralNice way to covert img object to byte array. Dear Amando can... Pin
naseer baloch29-Aug-11 20:09
Membernaseer baloch29-Aug-11 20:09 
Nice way to covert img object to byte array.
Dear Amando can you share how can one load the same image to an image control after quering from database?
GeneralRe: naseer, you can use the following methods : public static cl... Pin
Armando de la Torre30-Aug-11 7:11
MemberArmando de la Torre30-Aug-11 7:11 
GeneralReason for my vote of 5 helpful!! Pin
GPUToaster™23-Aug-11 23:00
MemberGPUToaster™23-Aug-11 23:00 
GeneralReally great Pin
inf1n1te23-Aug-11 8:01
professionalinf1n1te23-Aug-11 8:01 
GeneralThanks! Pin
Dr.Walt Fair, PE23-Aug-11 6:03
professionalDr.Walt Fair, PE23-Aug-11 6:03 
GeneralReason for my vote of 5 Good One!! Nice solution.. Pin
jawed.ace22-Aug-11 21:43
Memberjawed.ace22-Aug-11 21:43 

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.