Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# GDI+/System.Drawing.Graphics - creating a buffer and manually blitting?

I'm creating a cad viewer which deals with very large image files and I am trying to optimise it for as high a framerate and low a memory footprint as possible.

It uses GDI+ for rendering onto a panel.

It's current flaw is with image rendering. Some of the files I'm using reference images which are particularly big (8000x8000 pixels). I've optimised the memory usage by only loading them when they become visible and disposing of them when they're not. This reduces the chance of the program running out of memory but prevents the images from being loaded and unloaded too often; however rendering the images themselves (context.DrawImage) still carries a very large overhead.

I'm now exploring ways of blitting the images into a smaller buffer of some sort, rendering this (generally much smaller) buffer, and then refreshing/rebuilding it when the zoom level changes significantly.

The problem is, I can't find any provision for this in GDI whatsoever. Can anyone suggest how I could achieve it?

like image 441
Toby Wilson Avatar asked Mar 21 '11 11:03

Toby Wilson


2 Answers

I don't think GDI is designed for such high-speed updates of images. If you are trying to scroll the image, and tracking the mouse with each move, try to shift sections of the image and fill in the space opened up by the shift. Essentially reuse the tricks that programmers used when smoothly scrolling/panning graphics at a time when CPU's are slow and RAM is small.

like image 93
Stephen Chung Avatar answered Sep 28 '22 08:09

Stephen Chung


If you're creating a new graphics application that needs a high framerate and are looking for suggestions, then I suggest abandoning GDI+ and using WPF. WPF uses hardware acceleration and supports retained-mode graphics; this has much better performance for less work than GDI+.

If there is some limitation that forbids WPF, please explain it in your question. This is relevant because such limitations can also impact GDI+ drawing.

like image 33
Dour High Arch Avatar answered Sep 28 '22 08:09

Dour High Arch