Click here to Skip to main content
12,452,213 members (55,725 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VB Visual-Studio C#4.0 VS2010 , +
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.
Posted 8-Mar-12 20:12pm
Updated 8-Mar-12 20:34pm
v2
Comments
ProEnggSoft 9-Mar-12 2:35am
   
Edit - C# code indentation corrected - PES
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();
Then insert it to the database:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myImageColumn) VALUES (@IM)", con))
        {
        com.Parameters.AddWithValue("@IM", myImageAsAByteArray);
        com.ExecuteNonQuery();
        }
    }
How does that code differ from yours?
  Permalink  
Comments
SAKryukov 9-Mar-12 3:00am
   
Good points, my 5. I only went so far to advise on using memory stream instead of file.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.

—SA
  Permalink  
Comments
Aradhana Rajgor 9-Mar-12 2:57am
   
I tried your suggestion but it is also giving me the error.....
it is " Parameter not valid "
SAKryukov 9-Mar-12 3:01am
   
This is something unrelated. I don't know what is it without looking at your code.
--SA
sushil.mate 9-Mar-12 4:50am
   
Dispose the objects after your work, dispose that buffer variable in the end.
SAKryukov 9-Mar-12 23:48pm
   
Using statement does disposal on the end of block.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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)
        {
            this.openFileDialogLogo.ShowDialog(this);
            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();
            fs.Close();
        }
 
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....
  Permalink  
Comments
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 9 Mar 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100