Author Topic: Bass.NET drawing spectrum with a grid  (Read 264 times)

Alf162

  • Posts: 2
Bass.NET drawing spectrum with a grid
« on: 10 Jan '19 - 08:04 »
Hi.
I'm trying to draw a signal spectrum in Windows Form using
Code: [Select]
visuals.CreateSpectrum. It returns a bitmap, which i can display in picturebox.
I also want to draw a grid, but i can't using DrawLine function, because grid will flicker.
Code is below.
Code: [Select]
if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Handle))
{
_handle = Bass.BASS_StreamCreateFile(file_, 0, 0, BASSFlag.BASS_SAMPLE_FLOAT);
if (Bass.BASS_ChannelPlay(_handle, false))
{
_visuals = new Visuals();
_timer = new BASSTimer((int)(1.0d / 10 * 1000));
_timer.Tick += timer_Tick;
_timer.Start();
}
}
Code: [Select]
private void timer_Tick(object sender, EventArgs e)
{
Bitmap bp_1 = _visuals.CreateSpectrum(_handle, pictureBox1.Width, pictureBox1.Height, Color.Red, Color.Blue, Color.LightGreen, false, false, false);
Graphics g = pictureBox1.CreateGraphics();
pictureBox1.Image = bp_1;
int numOfCells = 200;
int cellSize = 50;
Pen p = new Pen(Color.Black);

for (int y = 0; y < numOfCells; ++y)
{
g.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
}

for (int x = 0; x < numOfCells; ++x)
{
g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
}
}
P.S. If i create grid once time, spectrum is drawing ower it

radio42

  • Posts: 4643
Re: Bass.NET drawing spectrum with a grid
« Reply #1 on: 10 Jan '19 - 11:35 »
The flickering is because you are first assigning the Spectrum image to your pictureBox and then you are drawing on top of that pictureBox (as you are creating the Graphics object from it directly).
As such, with each DrawLine you are invalidating the pictureBox and it has to draw itself first, which causes the flickering.

To avoid this, note that each Visuals method also contains a 2nd overload, which takes a Graphics object as a parameter directly.
So what you do is, you create your own (empty) bitmap, create a Graphics context from it and let the Spectrum draw on it and later in addition add your own drawing on top of it and after all that you assign finished image to the pictureBox.
Something like this (make sure to ALWAYS release any drawing resources!):
Code: [Select]
Bitmap bmp = null;
Graphics g = null;
try
{
    bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    g = Graphics.FromImage(bmp);

    // first do the spectrum drawing
    _visuals.CreateSpectrum(_handle, g, pictureBox1.ClientRectangle, Color.Red, Color.Blue, Color.LightGreen, false, false, false);

    using (Pen p = new Pen(Color.Black))
    {
        // do your own drawing here...
    }
}
catch
{
    // on error reset the bitmap
    bmp = null;
}
finally
{
    // clean up graphics resources
    if (g != null)
        g.Dispose();
}

// use your bitmap
pictureBox1.Image = bmp;
...

Alf162

  • Posts: 2
Re: Bass.NET drawing spectrum with a grid
« Reply #2 on: 11 Jan '19 - 05:50 »
Thank you very much, it works!
And another small question. Function CreateSpectrum3DVoicePrint draw spectrogram from left to right (horizontal=time, vertical=frequency). Can i change it? For example, (horizontal=frequency, vertical=time)
« Last Edit: 11 Jan '19 - 06:08 by Alf162 »

radio42

  • Posts: 4643
Re: Bass.NET drawing spectrum with a grid
« Reply #3 on: 11 Jan '19 - 12:43 »
And another small question. Function CreateSpectrum3DVoicePrint draw spectrogram from left to right (horizontal=time, vertical=frequency). Can i change it? For example, (horizontal=frequency, vertical=time)
Currently not, so I guess you would need to implement this yourself.