Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real Time Graph Drawing (Waveform)

I'd like to do some projects with audio in C# .NET 4.0 in the future so I've gathered some code samples for audio recording, working with WAVE files etc. But what I haven't been able to find is:

How can I draw audio waveforms / spectrograms in realtime(ish)? Obviously, creating an in-memory bitmap and loading it into a picture box would be very slow, right? So, what's the easiest way?

Thanks!

like image 603
John Avatar asked Feb 28 '12 17:02

John


2 Answers

You can use the GDI+ based Graphics and Drawing in Windows Forms to draw your content directly. This will be far faster than rendering to a bitmap and displaying in a picture box for constantly changing content.

like image 146
Reed Copsey Avatar answered Oct 23 '22 02:10

Reed Copsey


Just paint directly on a suitable control surface, i.e. a Panel. Get the graphics context of the control and update it in the Paint-event using the Invalidate() function. Every time you invalidate, Paint is called automagically. This is where you want to put your drawing logic.

The best way to update it is to use DrawImageUnscaled(). The image itself is fastest made with the LockBits method, explained excellently here:

https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx

like image 1
Pedery Avatar answered Oct 23 '22 01:10

Pedery