Hi All
I am trying to fit an outsized shape into a picture box, and then invert it by 180 degrees.
I managed to make it move with aid of trackbar up / down but I am unable to rotate it
using 'RotateFlip' commands, so I try to find a factor that I could use to make it work.
Could someone help me please ?
thanks in advance

Name:  invertVB.jpg
Views: 3938
Size:  21.0 KB


Code:
'
' 26 Jan 2014       ' fit outsized polygon inside picturebox and rotate by 180°
'
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
    Public limArr(5, 2) As String
    Public maxObj As Integer
    Public higherObjX, higherObjY As Double
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' draw
        Call DrawResizeInvert()
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' there is a form with a picturebox of defined dimensions and known location on screen.
        ' there is also an object(polygon) of defined dimensions that needs to be
        ' displayed FLIPPED on the picturebox and RESIZED to fit in the picturebox.
        '
        ' GATHER INITIAL PARAMETERS
        ' picturebox related data
        Dim picX, picY, picSizeW, picSizeH As Integer
        picX = PictureBox1.Location.X
        picY = PictureBox1.Location.Y
        picSizeW = PictureBox1.Size.Width
        picSizeH = PictureBox1.Size.Height
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        '-----------------------------------------------------------------
        ' object - POLYGON related data
        '
        Dim n As Integer
        n = 0
        higherObjX = 0
        higherObjY = 0
        limArr(1, 1) = "2" : limArr(1, 2) = "40"
        limArr(2, 1) = "500" : limArr(2, 2) = "10"  ' 2,1 = outside pic box height
        limArr(3, 1) = "500" : limArr(3, 2) = "180" ' 3,1 = outside pic box height
        limArr(4, 1) = "2" : limArr(4, 2) = "120"
        '------------------------------------------------------------------
        ' avoid exceeding the array limits (bounds)
        On Error GoTo 10
        ' find total array items
        maxObj = 0
        For n = 1 To 10
            If limArr(n, 1) = "" Then Exit For
            maxObj = maxObj + 1
            If Val(limArr(n, 1)) > Val(limArr(n - 1, 1)) Then higherObjX = Val(limArr(n, 1))
            If Val(limArr(n, 2)) > Val(limArr(n - 1, 2)) Then higherObjY = Val(limArr(n, 2))
        Next
        '----------- initial parameters gathered ------------------
        TrackBar1.Value = 0
        Label1.Text = TrackBar1.Value
        TrackBar1.Enabled = False
        '----------------------------------------------------------
        Exit Sub
10:
        MsgBox("Some error occured with the array index...")
    End Sub
    Sub DrawResizeInvert()
        On Error GoTo 10
        '---------------------------
        TrackBar1.Enabled = True
        TrackBar1.BackColor = Color.LightSteelBlue
        PictureBox1.Refresh()
        '---------------------------
        Dim G As Graphics
        G = PictureBox1.CreateGraphics
        G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        Dim P As New Pen(Color.Blue)
        Dim picAR   ' original picture box aspect ratio
        picAR = (PictureBox1.Size.Height / PictureBox1.Size.Width)
        '--------------------------------------------------------------
        ' image is out of screen so we must SCALE it down...
        ' reduce shape on one or more axis(es) until it fits the picture box
        ' so find the SF (scaling factor) that will reduce one side to fit inside the picture box.
        Dim SF, n, curHighX
        n = 0
        Do
            n = n + 0.05
            SF = n * picAR                  ' get the Scale Factor that will bring the object inside the picture box
            curHighX = (higherObjX / SF)
            If curHighX < (PictureBox1.Size.Height - 1) Then Exit Do
        Loop
        '--------------------------------------------------------------
        ' to maintain proportions must scale all items / both axes 
        Dim X(10), Y(10) As Integer
        For n = 1 To maxObj
            If Val(limArr(n, 1)) < higherObjY Then
                Y(n) = Int(Val(limArr(n, 1)) / 1)      ' no need to scale
            ElseIf Val(limArr(n, 1)) >= higherObjY Then
                Y(n) = Int(Val(limArr(n, 1)) / SF)     ' scale per SF value
            End If
            If Val(limArr(n, 2)) < higherObjX Then
                X(n) = Int(Val(limArr(n, 2)) / 1)
            ElseIf Val(limArr(n, 2)) >= higherObjY Then
                X(n) = Int(Val(limArr(n, 2)) / SF)
            End If
        Next
        '/////////////////////////////////////////////////////////
        ' the PROBLEM AREA
        ' need to display the shape inverting with the aid of trackbar
        ' which will display the actual INVERT FACTOR to achieve 180°
        ' creating a 'mirror' of the origial shape (polygon)
        '
        ' horizontal / vertical rotation ?
        '/////////////////////////////////////////////////////////
        For n = 1 To maxObj
            If Y(n) < higherObjY Then
                Y(n) = Y(n) - TrackBar1.Value
            ElseIf Y(n) >= higherObjY Then
                Y(n) = Y(n) - TrackBar1.Value
            End If
        Next
        '-------------------------------------------------------------
        ' draw and show !
        For n = 1 To maxObj + 1
            If n < maxObj Then G.DrawLine(P, X(n), Y(n), X(n + 1), Y(n + 1))
            If n = maxObj + 1 Then G.DrawLine(P, X(n - 1), Y(n - 1), X(1), Y(1)) ' close polygon with the first (1) point !
        Next n
        Exit Sub
10:     MsgBox("Some error occured with inverting the image...")
    End Sub
    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        Label1.Text = TrackBar1.Value
        Call DrawResizeInvert()
    End Sub
End Class