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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With