<p>I am writing a C# control that wraps DirectX 9 and provides a simplified interface to perform 2D pixel level drawing. .NET requires that I wrap this code in an unsafe code block and compile with the allow unsafe code option.</p>

<p>I'm locking the entire surface which then returns a pointer to the locked area of memory. I can then write pixel data directly using "simple" pointer arithmetic. I have performance tested this and found a substantial speed improvement over other "safe" methods I know of.</p>

<p>Is this the fastest way to manipulate individual pixels in a C# .NET application? Is there a better, safer way? If there was an equally fast approach that does not require pointer manipulation it would be my preference to use that.</p>

<p>(I know this is 2008 and we should all be using DirectX 3D, <a href="http://en.wikipedia.org/wiki/OpenGL" rel="nofollow noreferrer">OpenGL</a>, etc., however this control is to be used exclusively for 2D pixel rendering and simply does not require 3D rendering.)</p>

[{'answer_id': 55972, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': 1, 'selected': False, 'text': "<p>I recently was tasked with creating a simple histogram control for one of our thin client apps (C#).  The images that I was analyzing were about 1200x1200 and I had to go the same route.  I could make the thing draw itself once with no problem, but the control needed to be re-sizable.  I tried to avoid it, but I had to get at the raw memory itself.</p>\n\n<p>I'm not saying it is impossible using the standard .NET classes, but I couldn't get it too work in the end.</p>\n"}, {'answer_id': 60855, 'author': 'Corey Ross', 'author_id': 5927, 'author_profile': 'https://Stackoverflow.com/users/5927', 'pm_score': 4, 'selected': True, 'text': "<p>Using unsafe pointers is the fastest way to do direct memory manipulation in C# (definitely faster than using the Marshal wrapper functions).</p>\n\n<p>Just out of curiosity, what sort of 2D drawing operations are you trying to perform?</p>\n\n<p>I ask because locking a DirectX surface to do pixel level manipulations will defeat most of the hardware acceleration benefits that you would hope to gain from using DirectX. Also, the DirectX device will fail to initialize when used over terminal services (remote desktop), so the control will be unusable in that scenario (this may not matter to you).</p>\n\n<p>DirectX will be a big win when drawing large triangles and transforming images (texture mapped onto a quad), but it won't really perform that great with single pixel manipulation.</p>\n\n<p>Staying in .NET land, one alternative is to keep around a Bitmap object to act as your surface, using LockBits and directly accessing the pixels through the unsafe pointer in the returned BitmapData object.</p>\n"}, {'answer_id': 131476, 'author': 'Steven A. Lowe', 'author_id': 9345, 'author_profile': 'https://Stackoverflow.com/users/9345', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, that is probably the fastest way.</p>\n\n<p>A few years ago I had to compare two 1024x1024 images at the pixel level; the get-pixel methods took 2 minutes, and the unsafe scan took 0.01 seconds.</p>\n'}, {'answer_id': 377913, 'author': 'Guge', 'author_id': 37771, 'author_profile': 'https://Stackoverflow.com/users/37771', 'pm_score': 2, 'selected': False, 'text': '<p>I have also used unsafe to speed up things of that nature. The performance improvements are dramatic, to say the least. The point here is that unsafe turns off a bunch of things that you might not need as long as you know what you\'re doing.</p>\n\n<p>Also, check out <a href="https://en.wikipedia.org/wiki/DirectDraw" rel="nofollow noreferrer">DirectDraw</a>. It is the 2D graphics component of DirectX. It is really fast.</p>\n'}]
['https://Stackoverflow.com/questions/55963', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5023/']