Author Topic: Program freezes when creating waveform  (Read 534 times)

kafffee

  • Posts: 291
Program freezes when creating waveform
« on: 21 Sep '23 - 17:12 »
Hello  :)

I have a little problem:

I am creating my waveform like this:

Code: [Select]
 
Private Wait As Boolean
Public Sub UpdateImage(finished As Boolean)
      Wait = True
      WFPic = BitmapToImageSource(MyWaveForm.CreateBitmap(CInt(TransformToPixels(WFWidth) * Zoom * GetScaling), TransformToPixels(WFHeight), -1, -1, finished))
      Wait = False
     
  End Sub

I can zoom in and out with my mouse wheel like this:

 
Code: [Select]
Private Sub MouseWheelUp_Execute(obj As Object)
     If Zoom <> 1 Then Zoom = Zoom - 1
 End Sub

 Private Sub MouseWheelDown_Execute(obj As Object)
     If Zoom <> 50 Then Zoom = Zoom + 1
 End Sub

Public Property Zoom As Integer
    Get
        Return _Zoom
    End Get
    Set(value As Integer)
        _Zoom = value
        If CurrentMP3Info IsNot Nothing Then
            If Not Wait Then
                UpdateImage(True)
            End If
        End If
            RaisePropertyChanged()
    End Set
End Property

But when I scroll too fast with my mouse wheel, the program just freezes respectively even ends without an error. I tried to solve this with Wait As Boolean as you can see, but no success.

Any other ideas on  how to fix this?

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: Program freezes when creating waveform
« Reply #1 on: 21 Sep '23 - 17:48 »
To hopefully find out where it's stuck, try running your app under the debugger and "break" when it's frozen, and then have a look at the call stack. Make sure "Enable native code debugging" is enabled in your project first.

kafffee

  • Posts: 291
Re: Program freezes when creating waveform
« Reply #2 on: 22 Sep '23 - 08:34 »
Okay did that. See Screenshots. I will try to translate the error in callstack3.png:

The frames mentioned below are possibly not correct and/or missing. Native debugger tries to run managed callstack.

Something like that. In callsatck2.png you can also see the line of code that it is happening in (283).

Note: The error only happens on my test computer (Quad Core @ 2.8Ghz), on my main computer (Quad Core @ 3.3GHz) it works...
« Last Edit: 22 Sep '23 - 08:44 by kafffee »

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: Program freezes when creating waveform
« Reply #3 on: 22 Sep '23 - 14:47 »
To see more than "[External Code]" in the call stack, right-click there and enable the "Show External Code" option.

kafffee

  • Posts: 291
Re: Program freezes when creating waveform
« Reply #4 on: 22 Sep '23 - 15:25 »
Okay here it goes :)

Tell me if you need some translation

Ian @ un4seen

  • Administrator
  • Posts: 26177
Re: Program freezes when creating waveform
« Reply #5 on: 22 Sep '23 - 17:13 »
BASS doesn't appear in that call stack, so the problem doesn't appear to be something BASS-related. It seems to be getting stuck in a BitmapToImageSource function call, but the frame before it is UpdateVisualization (not UpdateImage like in your code above), so you should also check that function's code, eg. maybe add the "Wait" stuff there too? Apart from that, I'm afraid I can't really offer any suggestions as I'm not familiar with this .Net stuff.

kafffee

  • Posts: 291
Re: Program freezes when creating waveform
« Reply #6 on: 22 Sep '23 - 17:29 »
Okay cool, thanks anyways :)

radio42

  • Posts: 4840
Re: Program freezes when creating waveform
« Reply #7 on: 22 Sep '23 - 17:41 »
It looks, like your issues is neither with BASS nor within Bass.Net, but with the extensive use of the BitmapImage class and the .OnLoad option you are using.

I am not familiar with the inners of the BitmapImage class, but why are you using it - as in your example you create a load of new instances with every scroll, which might be very 'expensive'.
Why don't you use a single Bitmap instance, which you constantly reuse with each scroll.
Something like:
Code: [Select]
private Bitmap _waveImage = null;
...

// init, e.g.
_waveImage = new Bitmap(pictureEditWaveForm.Width, pictureEditWaveForm.Height);
...

// and (re)use it
using (Graphics g = Graphics.FromImage(_waveImage))
{
    if (_waveForm.CreateBitmap(g, pictureEditWaveForm.ClientRectangle, -1, -1, true))
    {
        try
        {
            // use it, e.g.
            pictureEditWaveForm.BackgroundImage = _waveImage;
            pictureEditWaveForm.Invalidate();
        }
        catch { }
    }
}

kafffee

  • Posts: 291
Re: Program freezes when creating waveform
« Reply #8 on: 23 Sep '23 - 15:42 »
@radio42

Thanks for the hint, you made me think a little:

As I have a WPF, and not a WinForms project, your code won't work because I cannot bind an Image control to a System.Drawing.Bitmap.

So I decided to go with a BitmapSource and now it works. For anyone who may run into the same issue here is my code:

Code: [Select]
Public Class Deck
    Public Function BitmapToBitmapSource(ByVal source As System.Drawing.Bitmap) As BitmapSource
        If source Is Nothing Then Return Nothing
        Dim bitSrc As BitmapSource = Nothing
        Dim hBitmap = source.GetHbitmap()

        Try
            bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
        Catch __unusedWin32Exception1__ As Win32Exception
            bitSrc = Nothing
        Finally
            NativeMethods.DeleteObject(hBitmap)
        End Try

        Return bitSrc
    End Function
End Class

Public Class NativeMethods
    Public Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As Long
End Class