Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# fast pixel rendering

I'm developing depth processing (Xbox Kinect, Asus Xtion, etc) applications using OpenNI.

I need a really simple and fast way of drawing on a Windows form when new depth data is available from the sensor (30 or 60 fps depending on resolution).

Currently I'm invalidating a double-buffered panel from a seperate thread when the data becomes available, and then setting pixels of a bitmap in the panel's paint method, yielding a predictably awful 5fps.

System.Drawing.Graphics seems to lack a fast way to set individual pixels, unless anyone can instruct otherwise.

I literally just need to set pixel colours, so would like to avoid using third party high speed rendering APIs if possible, and ideally use something as native as possible.

Does anyone have any suggestions?

like image 321
Toby Wilson Avatar asked Jun 14 '12 09:06

Toby Wilson


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

If you're using Bitmaps, then you should use LockBits and UnlockBits to access the data directly in memory. In C#, you can get some extra performance by using unsafe code blocks and pointers.

See this link for more information: http://web.archive.org/web/20150227183132/http://bobpowell.net/lockingbits.aspx

like image 76
Chris Dunaway Avatar answered Oct 01 '22 13:10

Chris Dunaway


image.SetPixel() is very slow when you are replacing many pixels per frame and you need many frames per second.

It will be a lot faster when you use a WriteableBitmap and call CopyPixels

This way you can fill the array with pixel data using multiple threads and simply blit the array to the image in a single call.

EDIT

Note that WriteableBitmap is a WPF class. If you are bound to WinForms you might need to create your own implementation. WPF/WinForms/GDI interop: converting a WriteableBitmap to a System.Drawing.Image?

like image 36
Emond Avatar answered Oct 01 '22 13:10

Emond