Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I've written a code to save desktop images into unknown number of image array. but when I run the code below it shows this problem : "Object reference not set to an instance of an object.". I know that says that I need to declare image like this : image[] i=new image[100], but I don't know my images are 100 or more or less. please help me about it.
code :

private int num;
private Image[] i;
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send("{PRTSC}");
i[num] = Clipboard.GetImage();
num++;
}


Thanks.
Posted
Updated 3-Feb-12 23:57pm
v2

1 solution

Instead of an array, consider using as generic list: List<t> Class[^]. Now with Add[^] method you can add the necessary items and the list expands automatically.
 
Share this answer
 
Comments
mehdi_k 3-Feb-12 15:40pm    
Thanks , it work great.
but when I run it, it uses lots of cpu(about 50%), and my RAM(memory) gets full after 1 or 2 min. is there a way to fix this? I mean this app gets useless with this situation.
Wendelius 3-Feb-12 15:55pm    
Without any more information, I'd guess that the images are filling up the memory. what type did you use for the generics declaration?
mehdi_k 3-Feb-12 16:07pm    
I used image as I am trying to copy images from clipboard. like this : private List<Image> i = new List<Image>();
Wendelius 3-Feb-12 16:20pm    
Could it simply be that the total amount of the image data is so high that it's causing thse problems. For example if each image is 5 MB and you have 100 of them that's total 500 MB plus the overhead of the list (which shouldn't be too high).

Just out of curiosity you could also try using a datatable to store the images. See:DataTable Class[^]. The internals of storing the data are a bit different so it could be performing better (again if the total amount is the problem)
LanFanNinja 3-Feb-12 16:25pm    
I agree that this is most likely the problem.

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