Graphics Draw on Bitmap/PictureBox - highdpiaware app

StewartBW 1,905 Reputation points
2025-08-30T23:47:04.7933333+00:00

Hi experts,

Using Bass.dll and Basscd.dll to play audio Cds and show a simple equalizer-type visualizer on PictureBox Size(572, 93)

The code works great on dpi unaware app, when I set the app to highdpiaware, the visualizer bars will not fit the PictureBox when app not run in dpi96/100

Since the code is simple, I think there's a fix to calculate the drawings correctly using the correct dpi.

I guess should calculate the correct values of EQPictureBox.Width and EQPictureBox.Height

Using VB.net WinForms .Net Framework 4.8

Thanks for helping :)

BASS_ChannelPlay(MyPlayer, False)

    Private Sub EQPictureBox_Paint(sender As Object, e As PaintEventArgs) Handles EQPictureBox.Paint
        MyGO = e.Graphics
    End Sub
    Private Function SquareRoot(ByVal InputNum As Double) As Double
        SquareRoot = InputNum ^ 0.5
    End Function
    Private Sub Visualizer()
        Dim MyBitmap As Bitmap = New Bitmap(EQPictureBox.Width, EQPictureBox.Height)
        Dim MyBuffer(1023) As Single
        Dim MyX As Integer
        Dim MyY As Integer
        If BASS_ChannelIsActive(MyPlayer) = 0 Then Exit Sub
        BASS_ChannelGetData(MyPlayer, MyBuffer, &H80000002) 'BASSData.BASS_DATA_FFT1024
        Select Case DarkTheme()
            Case False
                TimeLabel.ForeColor = Color.Lime
                For MyX = 0 To EQPictureBox.Width Step 8
                    MyY = CInt(SquareRoot(MyBuffer(MyX + 1)) * 3 * EQPictureBox.Height)    ' - 4
                    If MyY > EQPictureBox.Height Then MyY = EQPictureBox.Height
                    Using MyGraphs As Graphics = Graphics.FromImage(MyBitmap)
                        MyGraphs.SmoothingMode = SmoothingMode.AntiAlias
                        Using GreenPen As New Pen(Color.Green, 5)
                            MyGraphs.DrawLine(GreenPen, MyX + 4, EQPictureBox.Height, MyX + 4, EQPictureBox.Height - MyY)
                        End Using
                        'MyGraphs.DrawArc(RedPen, MyX + 2, EQPictureBox.Height, EQPictureBox.Width, EQPictureBox.Height - MyY, MyY, MyY)
                        Using LemonPen As New Pen(Color.Yellow, 5)
                            MyGraphs.DrawLine(LemonPen, MyX + 4, EQPictureBox.Height - MyY, MyX + 4, EQPictureBox.Height - MyY - 2)
                        End Using
                    End Using
                Next
            Case True
                TimeLabel.ForeColor = Color.Magenta
                For MyX = 0 To EQPictureBox.Width Step 4
                    MyY = CInt(SquareRoot(MyBuffer(MyX + 1)) * 3 * EQPictureBox.Height)    ' - 4
                    If MyY > EQPictureBox.Height Then MyY = EQPictureBox.Height
                    If MyY < 0 Then MyY = 0
                    Using MyGraphs As Graphics = Graphics.FromImage(MyBitmap)
                        MyGraphs.SmoothingMode = SmoothingMode.AntiAlias
                        Using WhitePen As New Pen(Color.Azure, 2)
                            MyGraphs.DrawLine(WhitePen, MyX + 2, EQPictureBox.Height \ 2, MyX + 2, EQPictureBox.Height \ 2 - MyY)    ' \ 2)
                        End Using
                        Using GrapePen As New Pen(Color.BlueViolet, 2)
                            MyGraphs.DrawLine(GrapePen, MyX + 2, EQPictureBox.Height \ 2 + MyY, MyX + 2, EQPictureBox.Height \ 2)     '- MyY \ 2)
                        End Using
                    End Using
                Next
        End Select
        EQPictureBox.Image = MyBitmap 'Draw Visual 2 PictureBox
        MyBitmap = Nothing
    End Sub
Developer technologies | Windows Forms
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.