Unsafe C # and pointers for 2D rendering, good or bad?

I am writing a C # control that wraps DirectX 9 and provides a simplified interface for performing the 2D level of the image level .. NET requires me to wrap this code in an insecure code block and compile it using the allow unsafe code option.

I lock the entire surface, which then returns a pointer to the locked memory area. Then I can write pixel data directly using the β€œsimple” pointer arithmetic. I tested this performance and found a significant speed improvement over the other "safe" methods that I know of.

Is this the fastest way to manage individual pixels in a C # .NET application? Is there a better, safer way? If there was a fast approach that does not require pointer manipulation, I would prefer to use it.

(I know this is 2008 and we should all use DirectX 3D, OpenGL , etc., however this control is used exclusively for 2D pixel rendering and just does not require 3D rendering.)

+4
pointers c # graphics
Sep 11 '08 at 7:47
source share
4 answers

Using unsafe pointers is the fastest way to directly manipulate memory in C # (definitely faster than using marshal's wrapper functions).

Just out of curiosity, what 2D drawing operations are you trying to perform?

I ask because blocking the DirectX surface to process pixel levels will defeat the hardware acceleration benefits you hoped to gain from using DirectX. In addition, the DirectX device will not initialize when used in terminal services (Remote Desktop), so the control will not be suitable for this scenario (this may not matter to you).

DirectX will be a big win when drawing large triangles and transforming images (the texture displayed on the quadrant), but in reality it will not do very well with one-pixel manipulations.

Staying in a .NET environment is one alternative - to support a Bitmap object to act like your surface using LockBits and directly accessing pixels through an insecure pointer in the returned BitmapData object.

+4
Sep 13 '08 at 20:41
source share

I also used unsafe ones to speed things up. At the very least, performance improvements are at least serious. The fact is that unsafe disables a bunch of things that you might not need until you know what you are doing.

Also check out DirectDraw . This is a 2D graphic component of DirectX. It is really fast.

+3
Dec 18 '08 at 13:57
source share

Yes, this is probably the fastest way.

A few years ago I had to compare two 1024x1024 images at the pixel level; get-pixel methods took 2 minutes, and unsafe scanning took 0.01 seconds.

+2
Sep 25 '08 at 4:27
source share

Recently, I was tasked with creating a simple histogram control for one of our thin client applications (C #). The images I analyzed were about 1200x1200, and I had to go the same way. I could do this thing on my own, without problems, but the control had to be overestimated. I tried to avoid this, but I had to figure out the raw memory myself.

I'm not saying that it is impossible to use standard .NET classes, but I could not get it to work at the end.

+1
Sep 11 '08 at 7:51
source share



All Articles