Hi,
I have a app which is loading between 200-1000 32x32px pictureboxes. All pictureboxes are on screen so I need to load them all and can't just load some. I set up a imageList and then create a picturebox for each image.
As it stands it takes a few secs for the whole set of images to display to the screen screen. Is there a better way to load that many small images than loading 1000 pictureboxes?
If posting the code would help, I will post it when I get home from work.
Thanks
Jay
Loading
Sascha BPosted Mar 22, 2011, 12:30 PM
Preparation:
declare these two variables above public form1()
Bitmap bitmap,img;
Graphics bmpgraphic;
First of all you need a background worker. (find it in the toolbox)
got into the "go_work" method
and make something like this.
bitmap = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format64bppPArgb); // Where w is your width for all your images! Also the height.
bmpgrafik = Graphics.FromImage(bitmap); // This is the connection between the Graphic and the Bitmap! Very important
bmpgraphic.CompositingMode = CompositingMode.SourceCopy;
bmpgraphic.CompositingQuality = CompositingQuality.GammaCorrected;
now paint all your images on this bmpgraphic
something like this
for(i = 0; i < imagecount; i++)
{
//set actual image
img = new Bitmap("imagepath");
bmpgraphic.DrawImage(img,Point); //Point is where the picture is drawn in the box. You must change it wit every i
}
When everything is drawn virtual on your bitmap throught the bmpgraphic go into the
RunWorkerCompleted method
and make something like this
picturebox.Image = bitmap;
and then
bitmap.Dispose(); // Cleaning up the cache
So everytime you want to redraw this image (resize, picture changed etc.)
then you have to call this
if(!backgroundworker.IsBusy) // Just to get sure the backgroundworker is ready
backgroundworker.RunWorkerAsync(); // redraw everything
So now your image should or can be painted :D
Now you can check the xy coordinates with the mouse moove method throught your picturebox and check it with the positions from your little pictures.
(You have to save every point to every image so you can check them)
Hope I forgot nothing. And good luck!
Jay WebsterPosted Mar 22, 2011, 6:36 PM
Thanks Sascha :D
Sascha BPosted Mar 22, 2011, 4:46 PM
Got the exact same error when I first tried it :D
Jay WebsterPosted Mar 22, 2011, 4:42 PM
I have the following:
[CODE]
Bitmap bitmap, img;
Graphics bmpgraphic;
public frmEditor()
{
InitializeComponent();
}
private void frmEditor_Load(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
bitmap = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
bmpgraphic = Graphics.FromImage(bitmap);
bmpgraphic.CompositingMode = CompositingMode.SourceCopy;
bmpgraphic.CompositingQuality = CompositingQuality.GammaCorrected;
Point myPoint = new Point(1, 1);
img = new Bitmap("bush.png");
bmpgraphic.DrawImage(img, myPoint);
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
PictureBoxMap.Image = bitmap;
bitmap.Dispose();
}
[/CODE]
I wrote it to test showing one 32px image. Although when I run the program, I get a red X in a box where the picture box is.
Jay WebsterPosted Mar 22, 2011, 9:44 AM
Sascha BPosted Mar 22, 2011, 9:32 AM
But first I have to get home as I said before.
Than I can copy&paste something out of my project.
I think at 17:00 GMT+1
Jay WebsterPosted Mar 22, 2011, 9:30 AM
when you get a chance if you code post those code snippets it would help alot, would be appreciated :)
Jay WebsterPosted Mar 22, 2011, 9:01 AM
Sascha BPosted Mar 22, 2011, 8:55 AM
So I think it isn't that impossible :D
But 1000times better than redraw all of your little boxes everytime one thing changed.
btw: You idea sounds like a RPG Maker
Edit: My programm paints nearly 30k seperatet dots on my picturebox.
So I know what you mean with slow loading. But with painting them on a bitmap first my problems with freezes and laggs dissappeared
Jay WebsterPosted Mar 22, 2011, 8:50 AM
The only problem I have is that when it initially loads the grid of picture boxes it loads them slowly. I have used mouse events so that you can zoom/move the map around but when it zoom's it resizes the picture boxes and redraws the lot again, so takes along time, too long.
Sascha: I dont think I can use the method you stated as if it was just one big picture box I wouldnt be able to change the individual tiles?
Frogleg: Since it can zoom in and out, it could load 20 tiles, it could load all of them..
I hope I am being clear in what is happening.
Jay
Sascha BPosted Mar 22, 2011, 8:06 AM
If you have trouble with painting on bitmaps and assign them on the big picturebox I can post some code snippets when I'm home. (17:00 GMT + 1)
If you use mouse interaction you can use the xy coordinates of the picturebox + mouseClickEvent
FroglegPosted Mar 22, 2011, 8:06 AM
You might be better off having a set amount of pictureboxes, with a next and previous button - then load as required.