I'm creating a map editor for game in C# Windows Forms. What I need is a grid that will have to store even a few thousands of images.
My first approach was to create a Panel control and then add multiple PictureBoxes to it. Unfortunately 2000 images took about 3 seconds to draw.
Then I decided to try creating Rectangles and drawing Images on them in Panel's OnPaint() method in hope to get better results but it's still very slow.
Is there any better, efficient way to render so many images in Windows Forms?
Thanks in advance for any advices.
Use the Paint event as you have done but...
As part of the loading of the images, cache a zoomed out version where you merge 16 images into one, which is only 125 images, when you zoom out over a certain scale, switch to using the pre-rendered zoomed out version.
You can do that as often as you like for multiple zoom levels with the idea of keeping as few images as possible on screen at anyone time. So you could divide it by 4 again.
I do this for a project which has a map comprised of 65536 images (256 x 256). The cache is also writted to disk so each time you zoom out you see the same number of images. In my editior I can only view 16 images at any one time, even if I'm looking at the whole map.
You can further improve on the this by knowing the available options to the user (e.g. Pan and Zoom) this gives you a limited subset of images the user could potentially view next so you can preload these to improve performance.
You'll increased load time initially but I bet you already have a significant load time pulling 2000 images off disk
Related
I am making a lot of camera tracking applications. I want to open the selected cameras in a layout. But mostly one camera will be watched. But I also have to do something to watch multiple. I want the screen to be divided into 2 when there are 2 cameras and when there are 3 cameras, the screen will be divided into 4. I couldn't do it with FlowLayoutControl. Because it is necessary to give dimension to the video control that I added. How should I do such a thing. I'm adding a screenshot of the fixed size control insertion part. I did the thing in the image but gave 200x200 dimensions to the controls inside. This is not what I really want to do. I want controls that change dynamically in size according to the number of controls. How do i do this?
In my WinRT app I need to draw about 3000 objects on a canvas, where I can translate and zoom the view. Unfortunatley, after adding about 1500 lines to my canvas my Windows 8 App always crashes. What could be the best practice to achieve this?
One solution could be rendering everything on an image (how do I do this?). But then I loose comfort of easy access and editing of every element.
Also my scale and translate is very slow. But since I also need a big overview, it makes no sense to put only the objects of the visible area in the canvas, since on minimum zoom it's still everything and zoomed it's still very laggy cause of add and remove operations.
There are a couple of different things you should employ to have a smooth UX:
Use a Quadtree, whenever you add a shape to your canvas you also put it on your Quadtree. This will be helpful when you will zoom on a portion of the image: you will know what objects are in this portion of the image; you will render them again (against using a cached/pixellated version).
To overcome the potentially lengthy drawing process you could do the following:
display the portion of the cached image overview at the right scale
use a progress indicator to let know the user that the program is working render this portion
when the faint rendering is done, blit it on the screen
A concrete example: Google Maps does that.
this may seem like an odd question, and I don't know the formats of images so I'll just go ahead and ask...
I'm making a minesweeper game (relevant to different things too) which utilizes a grid of buttons, then I'm adding a sprite to the buttons using backgroundImage. If the grid is 9x9 it's fine. 15x15 it slows down and 30x30 you can visibly see each button being loaded.
That raises me to my question: Which image format would load fastest? Obviously, file size takes a part in the loading speed, however, I am enquiring as to if, say, a jpeg - with the same filesize as a gif - will load faster. or a bmp, png, etc.
I'm asking this as to see if I can get my grid to load faster using a different format.
Thanks!
You want an image format that paints faster. There's only one, the one whose pixel format directly matches the video adapter's setting so that the pixel data can be directly copied without needing format adjustments.
On most any modern machine, that's PixelFormat.Format32bppPArgb. It draws ten times faster than all the other ones.
You won't get that format when loading an image from a resource or a file, you have to create it.
Do note that this still won't give you stellar paint speed if every cell in the grid is a control. Particularly if it is a button, they are very expensive to draw since they support transparency effects. You'll only get ahead here if you draw the entire grid in one Paint event handler. Like the form's.
A completely different approach is to cover up the visible delays with this hack.
use the 8-bit PNG or GIF format and reduce the number of colors in the palette. Some image programs such as PhotoShop allow you to save the image for the web and fine-tune the image settings. By reducing the color palette from 256 to something like 32, you greatly reduce the size of the file. The less colors that the image has, the smaller the file size is going to be.
PNG has a similar feature called "Interlaced". You may want to turn this feature off so that the full image downloads quicker.
Because the 8-bit PNG and GIF formats have the potential to result in much smaller image files, try to keep this in mind when creating graphics and illustrations for your application. Try to keep the amount of colors to a minimum and use flat graphics instead of photographs. This way you can create images with palettes of 16 colors, keeping the file size extremely small and fast to load.
Best Regards
Are you reloading the image every time you paint a button? If so, there's your problem - solve it with a cache.
Are you painting the image at its native size? Runtime resampling can create a performance hit.
use PNG or GIF it's faster types of images
A winform control for displaying a list of images is currently implemented with a flowlayoutpanel and a collection of pictureboxes. But even at thumbnail scale (64x64) when we when start to approach 1000+ images we get OutOfMemory exceptions - our actual problem lies from the thumbnail generation part and the creation of the Image object.
I haven't been able to find any strategies from the existing image viewing examples on the net regarding a large number of images so does anybody have any links or strategies for solving this problem of displaying a list of 1000+ images?
As a starting point we really only need these image objects around when the thumbnail's picture box is in view. Then we would only have 10 image objects created but is there a smarter way of doing this other than loading and destroying image objects?
Thanks,
Edward
You should display only one screen of images at a time.
When the user drags the scrollbar, destroy those images and load new ones.
WPF handles this in a great way. If you have turned on Virtualization, the ListBox will create only the control that are visible to the screen.
In your case, keep reference to the images in a list. Put a pictureBox, and based on the Scrollbar, change the image on the pictureBox.
I have a winforms ImageList which contains 200 256x256 images.
When I add the images, 1 by one, half of the programs time spent on the Add method according to ANTS .NET profiler.
So the program takes 10 secs to launch and 5 is spent there. That is very slow in my opinion.
I implemented the same thing using ImageList.Images.AddRange. The result did not change.
Does anyone know any alternatives or optimizations to solve this? Is the WPF ImageList any faster? Is there a faster winforms ImageList?
EDIT:
foreach (string imageFile in images)
{
imageList.Images.Add(Image.FromFile(imageFile)); // takes pretty much all of program's execution time.
}
Have a look at the album and list views in PhotoSuru, one of Microsoft's WPF sample applications. They have a couple different thumbnail view screens that load pretty quickly (all the images are loaded asynchronously, and only the images necessary are loaded). And there's full source code to get you started if it's what you are looking for.
It not precisely like Picasa (they decided only to show full rows of photos instead of a partially covered row like Picasa), but by implementing your own virtualizing IScrollInfo panel (reasonably straightforward, and Ben Constable has a great series of posts on it), you should be able to get the performance you're looking for and the behaviour you want.
A final note: You might be doing this already (are your images stored as 256x256 pixel images?), but whenever you're trying to display image thumbnails, you'll want to avoid loading the full image. Reading 2 MB+ image files off of the disk just to render an entire screenful of 256x256 pixel thumbnails always has a bigger performance hit than reading an appropriately sized small thumbnail image instead. If you can, cache the thumbnails somewhere (like Windows and Picasa do), or at the very least, try to use the embedded thumbnails in any JPEG files you encounter.
Are you using a ListView for the UI part using that ImageList?
The general solution to these kind of problems is to only load some of the images, because only some of them are shown at a time, and then load the other as needed. ListView has a VirtualMode property for this kind of scenarios.
Loading and adding 200 images at once may be too much to do, you could load the first shown images and then use a background thread to load the others. Have a look at how Win7 display images, it shows some placeholder when you scroll and then, when the image is loaded it shows the real picture. Be warned that this is not a simple thing to do.
try my code here to scale-down your image and make a thumbnail out of it.
Yes it is. Load images to List<Image>. Then call ImageList.Images.AddRange(list).
List<Image> list = new List<Image>();
foreach (string imageFile in images)
{
list.Add(Image.FromFile(imageFile));
}
imageList.Images.AddRange(list.ToArray());