Click here to Skip to main content
15,888,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

I am trying to retrieve an image from database. My code is as below, but it gives an error.

C#
DataTable tab = obj.createtable("Select pics from picto");

            byte[] ImgByte = (byte[])tab.Rows[0][0];
            FileStream fs = new FileStream("test.GIF", FileMode.Create);
            fs.Write(ImgByte, 0, ImgByte.Length);
            fs.Close();
            fs = null;
            pictureBox2.Image = Image.FromFile("test.GIF");


The last line gives this error "Out of memory"
Please help me.

Thanks for your kind reply.
I do not have only one image. I am making a program for a school to maintain their record. I will store some details about the student and also their pictures. I have changed my code.

C#
byte[] ImgByte = (byte[])tab.Rows[0][0];
int arraysize = new int();
arraysize = ImgByte.GetUpperBound(0);
MemoryStream fs = new MemoryStream(ImgByte,true);
fs.Write(ImgByte, 0, arraysize + 1);
Bitmap bm = new Bitmap(fs);
pictureBox2.Image = bm;
fs.Close();
fs = null;


This gives "Parameter Not Valid" error.
Posted
Updated 13-Mar-10 12:42pm
v3

kachura wrote:
("Select pics from picto");


This obviously selects all the pictures from the database, so how much memory that uses depends on how many pics are in there. Perhaps if you only select one picture, you won't run out of memory?
 
Share this answer
 
v2
You need to step back and THINK about what you are doing. Your second set of code actually would make the memory consumption problem much worse, as you have increased the number of copies of the byte array you have in memory at one time:

1. the copy in the Datatable
2. the copy in ImgByte
3. the copy in the MemoryStream
4. the copy in the bitmap
5. the copy in the PictureBox

In your first version you only had 3 copies floating around (since you closed and destroyed the filestream - although it is unlikely the memory would actually get garbage collected in time to make much difference).

also, you don't need to "new" int arraysize. Just declare it locally since you are done with it after this code.

And you might actually pay heed to Christian's advice and stop carrying on a conversation by posting follow up comments/questions with the "answer" button - use the "New Message" link at the bottom of the page for that.
 
Share this answer
 
Then probably size of the image would be very high. :)
 
Share this answer
 
A couple of things:

1 - The button you pushed is called 'answer' for a reason, edit your post or use the forum below.

2 - Well, this may be the only image in your DB, but if that's the case, it doesn't change that your code seems to assume you have one picture on your DB, which would still mean that it makes no sense to build a DB at all.

As someone else said, I guess that one picture is too big for the amount of RAM you have, and how much RAM is already being used by your code and other programs. Out of memory means what it says, changing your code can't fix that, unless it fixes it in the manner I suggested, by trying to limit memory usage.

Of course, your byte array is still in memory when you load the picture from the HDD ,which means your pic is at a minimum in memory twice, although a GIF takes up much more room when loaded, than when still a GIF in a byte array.
 
Share this answer
 
v3
I deleted your fake 'answer' and put it in your post. Notice how now when I try to answer, I can actually SEE your new code ?


kachura wrote:
I am making a program for a school to maintain their record. I will store some details about the student and also their pictures


So how does it make sense that the school has only ONE student ? How big is the picture ? Why did you change all of your code, EXCEPT the line that loads ALL the pictures and not just a specific one ?

It would also help if you told us what line gets the error. And, as Rob pointed out, you continue to make copy after copy of your image in memory.

I assume this is a homework assignment and not something that will be used in real life. You should perhaps talk to your teacher at this point.
 
Share this answer
 

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