Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I am storing image files in database table and at the time when i try to retrieve it from database and assign it to PictureBox it gives me error of " OUT OF MEMORY"
i tried to store it in a varbinary format as well as in Image format but nothing worked.
i think there is some problem with my code. Please Help me in finding where i am lacking.....

Here is the piece of code that gives me error

DataTable dtLogo = new DataTable();
dbConnection dbConn = new dbConnection();
dtLogo = dbConn.executeSelectQuery("Select logo_data From tbl_Logo Where master_ID = " + master_ID);

if (dtLogo.Rows.Count == 1)
    String sFilePath_Image = "";
    sFilePath_Image = System.IO.Path.GetTempFileName();
    m_barrLogo = (byte[])dtLogo.Rows[0]["logo_data"];
    System.IO.File.WriteAllBytes(sFilePath_Image, m_barrLogo);
    Picturebox1.Image = Image.FromFile(sFilePath_Image);
    Picturebox1.SizeMode = PictureBoxSizeMode.StretchImage;    

Here in above code master_ID is the id of master table for which the logo ( Image ) is stored.
Updated 8-Mar-12 20:34pm
ProEnggSoft 9-Mar-12 2:35am    
Edit - C# code indentation corrected - PES

First things first - you don't need to save it to a file in order to load it as an image. Since you have it as bytes, just convert it to a stream:
m_barrLogo = (byte[])dtLogo.Rows[0]["logo_data"];
Picturebox1.Image = Image.FromStream(new MemoryStream(m_barrLogo));

Secondly, I suspect the problem may well be in how you are storing the image, not in how you are retrieving it.
If you have the data as an Image, convert it to bytes:
MemoryStream ms = new MemoryStream();
return ms.ToArray();

Then insert it to the database:
using (SqlConnection con = new SqlConnection(strConnect))
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myImageColumn) VALUES (@IM)", con))
        com.Parameters.AddWithValue("@IM", myImageAsAByteArray);
How does that code differ from yours?
Share this answer
Sergey Alexandrovich Kryukov 9-Mar-12 3:00am    
Good points, my 5. I only went so far to advise on using memory stream instead of file.
If the file too big?

I'm not sure it can help you (as usual with insufficient memory), but writing the image in file is excessive. You already have an array of byte, create an image from it using memory stream:
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(m_barrLogo)) {
    Picturebox1.Image = Image.FromStream(stream);

Try to do it anyway; this is better.

By the way, never use auto-generated names like PictireBox1. They violate Microsoft naming conventions and are bad for support. Always rename all such names to something semantic. You are given the refactoring engine for this purpose.

Share this answer
Aradhana Rajgor 9-Mar-12 2:57am    
I tried your suggestion but it is also giving me the error.....
it is " Parameter not valid "
Sergey Alexandrovich Kryukov 9-Mar-12 3:01am    
This is something unrelated. I don't know what is it without looking at your code.
Sushil Mate 9-Mar-12 4:50am    
Dispose the objects after your work, dispose that buffer variable in the end.
Sergey Alexandrovich Kryukov 9-Mar-12 23:48pm    
Using statement does disposal on the end of block.
Hi SAKryukov,

Here i am providing you the code through which i am storing the data as well as the code from which i am retrieving data.

Store :

private void btnUploadImage_Click(object sender, EventArgs e)
            string strFn = this.openFileDialogLogo.FileName;            
            logoExtension = Path.GetExtension(strFn);
            this.picTradeLogo.Image = Image.FromFile(strFn);
            this.picTradeLogo.SizeMode = PictureBoxSizeMode.StretchImage;
            FileInfo fImage = new FileInfo(strFn);
            this.m_lImageFileLength = fImage.Length;
            FileStream fs = new FileStream(strFn, FileMode.Open, FileAccess.Read, FileShare.Read);
            m_barrLogo = new byte[Convert.ToInt32(this.m_lImageFileLength)];
            string _imageType = openFileDialogLogo.GetType().ToString();

private void iSave_ItemClick(object sender,DevExpress.XtraBars.ItemClickEventArgs e)
           DataTable dtLogo = new DataTable();
           dbConnection dbConn = new dbConnection();

           dtLogo = dbConn.executeStoredProcedure_Datatable("sp_trademark_logo_save", 
new SqlParameter("@trademark_ID", trademark_ID),
new SqlParameter("@logo_extension", logoExtension),
new SqlParameter("@logo_data", m_barrLogo));

now at the time of retrieving the Values it calls Function Named GetEditValues()
private void GetEditValues()
  DataTable dtLogo = new DataTable();
  dbConnection dbConn = new dbConnection();
  dtLogo = dbConn.executeSelectQuery("Select * From tbl_Logo Where trademark_ID = " + trademark_ID);
  byte[] buffer = (byte[])dtLogo.Rows[0]["logo_data"]; 
  MemoryStream memStream = new MemoryStream();
  memStream.Write(buffer, 0, buffer.Length);
  picTradeLogo.Image = Image.FromStream(memStream);

and here above function i am getting error....
Share this answer
lukeer 9-Mar-12 4:54am    
Don't post additional information as a solution.
Use the "Improve question" link instead. It's located beneath your original question.
(But you have nicely "pre"-tagged it.)

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900