Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
4.73/5 (3 votes)
See more:
Forgive my lack of knowledge, as I am just starting out with Visual Basic.
I am attempting to draw lines on a picturebox and will be using a trackbar to scale it.
What I have in code below will draw what I need, but seems to not be saving the original (unscaled) image to a bitmap. I need the trackbar, at every point, to grab the original image and scale it at the trackbar.value. I need to do it this way so it wont distort/blur the image because of scaling the same image too many times.
A background image is default, so removing that image upon drawing of lines.

This is the code to draw the lines using points)
VB
Imports System.Drawing.Drawing2D
Imports System.Math
Imports System.Drawing.Graphics

Public Module DrawVaCrossframe
    Public Bmp As New Bitmap(609, 330)
    Public g As Graphics = FromImage(Bmp)
    Public Property rotateAngle As Double

Public Sub drawVACF()
CrossframesMain.pbVACFDraw.BackgroundImage = Nothing
CrossframesMain.pbVACFDraw.BackColor = Color.Black
CrossframesMain.pbVACFDraw.Refresh()
g = CrossframesMain.pbVACFDraw.CreateGraphics


'Draw Centerline girders with drop WP's 1 thru 4
Dim pntWP1 As New Point
'left web/workline at bottom                                                                             
   If dblGdrDrop >= 0.0 Then
      pntWP1 = New Point(CInt((sngPnlWidth) - (dblBayHalfWidth)), CInt((sngPnlHeight) - (dblWebHalfDepth)))
   Else
      pntWP1 = New Point(CInt((sngPnlWidth) - (dblBayHalfWidth)), CInt(CInt(sngPnlHeight) - (dblWebHalfDepth) - dblGdrDrop))
        End If
'left web/workline at bottom
Dim pntWp2 As New Point(pntWP1.X, pntWP1.Y + CInt(dblWebDepth)) 
'right web/workline at top                                    
Dim pntWp3 As New Point(pntWP1.X + CInt(dblBayWidth), pntWP1.Y + CInt(dblGdrDrop))                  
'right web/workline at bottom
Dim pntWP4 As New Point(pntWp3.X, pntWp3.Y + CInt(dblWebDepth))

'draw left workline
g.DrawLine(whitePen, pntWP1, pntWp2)
'draw right workline                                                                
g.DrawLine(whitePen, pntWp3, pntWP4) 


That code gets my image, but is very small, so I need to scale it. When I try to send it to a Bitmap,
VB
CrossframesMain.pbVACFDraw.Image = Bmp

after `g.drawline`, the image comes on screen and then immediately off.

I am calling drawVACF() from the mainform with a button and then changing the trackbar on the same mainform.

VB
Imports System.Drawing.Drawing2D

Public Class CrossframesMain

Public g As Graphics
Private Bmp As New Bitmap(1218, 660)

Public Sub New()

        'This call is required by the designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.
        tbarVACF.Minimum = 1
        tbarVACF.Maximum = 9
        tbarVACF.SmallChange = 1
        tbarVACF.LargeChange = 2
        tbarVACF.Value = 1
        tbarVACF.TickFrequency = 2
        tbarVACF.TickStyle = TickStyle.Both

    End Sub



Private Sub tbarVACF_ValueChanged(sender As Object, e As EventArgs) Handles tbarVACF.ValueChanged

        Dim width As Integer
        Dim height As Integer

        Dim originalImage As Bitmap = CType(pbVACFDraw.Image, Bitmap)
        Dim g As Graphics = Graphics.FromImage(originalImage)

        Try
            If originalImage Is Nothing Then
                Return
            Else
            If Bmp Is Nothing Then
                    Return
                Else
                    lblTbarZoom.Text = "Zoom Level: " & tbarVACF.Value

                    If tbarVACF.Value > 0 Then
                        width = CInt(Bmp.Width * (0.25 * tbarVACF.Value))
                        height = CInt(Bmp.Height * (0.25 * tbarVACF.Value))
                    End If
                End If

               Dim _bmpImage = Bmp
               Dim scaledBitmap As New Bitmap(_bmpImage, width, height)
               Dim scaledBmp As Graphics = Graphics.FromImage(scaledBitmap)

               scaledBGBmp.InterpolationMode = InterpolationMode.HighQualityBicubic
               pbVACFDraw.Image = scaledBitmap
               g.DrawImage(scaledBitmap, width, height)
               End If
        Catch ex As Exception
            MsgBox(ex.ToString())

        End Try
End Sub



I would greatly appreciate any and all help and direction with this.

What I have tried:

Hard to say what all I have tried, because it has all run together over the last two weeks attempting to learn what I need to do. I am at my wits end.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900