[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 787
  • Last Modified:

PictureBox access to ImageRectange properties

I have a PictureBox named Piccy with size 256,256

I have a BMP created with bump = New Bitmap(512, 512, Drawing.Imaging.PixelFormat.Format24bppRgb) which display data using the method                     bump.SetPixel(xpos, ypos, colour) as information is gathered


and have assigned bump to piccy with piccy.Image = bump
and used piccy.SizeMode = PictureBoxSizeMode.Normal

the image shown is the upper left quadrant of the bmp image with the private piccyimagerectangle properties being
x = 0
y = 0
width = 512
height = 512

using piccy.SizeMode = PictureBoxSizeMode.StretchImage gives the complete bmp with the private piccy.imagerectangle properties being
x = 0
y = 0
width = 252
height = 252

finally using piccy.SizeMode = PictureBoxSizeMode.CenterImage gives a centred bmp with the private piccy.imagerectangle properties being
x = -130
y = -130
width = 512
height = 512

I would like to perform panning and zooming in a similar way using the private piccy.imagerectangle properties

what methods and properties do i need to modify in order to affect these properties and thereby achieve a custom panning and zooming function?

i have tried using clientrectangles, clientsize all of which fail miserably

I do not want to create clones of bitmap which would represent the zoomed/panned image, just use the original single BMP
0
robertstuckey
Asked:
robertstuckey
  • 4
  • 3
2 Solutions
 
RonaldBiemansCommented:
This is an example on how to rotate and zoom in/out

    Dim myimage As Image
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox4.AllowDrop = True
        myimage = Image.FromFile("c:\chestnut.jpg")

        PictureBox1.Image = myimage

    End Sub

    Dim m_MouseIsDown As Boolean
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If Not PictureBox1.Image Is Nothing Then

            m_MouseIsDown = True
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If m_MouseIsDown Then

            PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy Or _
    DragDropEffects.Move)
        End If
        m_MouseIsDown = False
    End Sub

    Private Sub PictureBox2_DragEnter(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles PictureBox4.DragEnter
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then

            If e.KeyState = 9 Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub PictureBox2_DragDrop(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles PictureBox4.DragDrop

        PictureBox4.Image = e.Data.GetData(DataFormats.Bitmap)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Rotate left

        PictureBox4.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
        PictureBox4.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Rotate right
        PictureBox4.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
        PictureBox4.Refresh()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Zoom in

        PictureBox4.SizeMode = PictureBoxSizeMode.StretchImage
        Fit()
        PictureBox4.Width = CInt(PictureBox4.Width * 1.25)
        PictureBox4.Height = CInt(PictureBox4.Height * 1.25)
    End Sub

 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'Zoom out
        Fit()
        PictureBox4.SizeMode = PictureBoxSizeMode.StretchImage
        PictureBox4.Width = CInt(PictureBox4.Width / 1.25)
        PictureBox4.Height = CInt(PictureBox4.Height / 1.25)
    End Sub

    Private m_IsFitForZoomIn As Boolean = False
    Private Sub Fit()

        If PictureBox4.Image.Width < PictureBox4.Width And _
            PictureBox4.Image.Height < PictureBox4.Height Then
            If Not m_IsFitForZoomIn Then
                PictureBox4.SizeMode = PictureBoxSizeMode.CenterImage
            End If
        End If

        CalculateAspectRatioAndSetDimensions()
    End Sub

 Private Function CalculateAspectRatioAndSetDimensions() As Double

        Dim ratio As Double
        If PictureBox4.Image.Width > PictureBox4.Image.Height Then
            ratio = PictureBox4.Image.Width / _
                    PictureBox4.Image.Height
            PictureBox4.Height = CInt(CDbl(PictureBox4.Width) / ratio)
        Else
            ratio = PictureBox4.Image.Height / _
                    PictureBox4.Image.Width
            PictureBox4.Width = CInt(CDbl(PictureBox4.Height) / ratio)
        End If

        Return ratio
    End Function
0
 
RonaldBiemansCommented:
I see there is also drag and drop in there :-)
0
 
robertstuckeyAuthor Commented:
unfortunately cloning of bitmaps would only be my last resort although your clones do rotate and zoom fine

i'm sure microsoft in their infinite wisdom must have implemented some methods or properties to allow how the .image can be user configured other than the preset PictureBoxSizeMode.Normal, .StretchImage, and .CenterImage. They must have disposed of all the good VB6 people

when i said I would like to perform panning and zooming in a similar way using the private piccy.imagerectangle properties, i meant doing it as they do it by allowing the single picturebox to link to the original image and for the display of the image to be tweaked, effectively by modifying the imagerectangle properties and not modifying the actual image and relinking it to the picturebox

its a pity its only worth 500 points

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RonaldBiemansCommented:
Hi robertstuckey, call me stupid, but where is the cloning.
0
 
RonaldBiemansCommented:
this class handles will transform the actual bitmap

Imports System.Drawing.Image
Imports System.Drawing

Public Class MyBitmap

    Public _inputBitmap As Bitmap

    Public ReadOnly Property thebitmap() As Bitmap
        Get
            Return _inputBitmap
        End Get
    End Property

    ReadOnly Property Height() As String
        Get
            Return _inputBitmap.Height
        End Get
    End Property

    ReadOnly Property Width() As String
        Get
            Return _inputBitmap.Width
        End Get
    End Property

    Public Sub New(ByVal MyBitmap As Bitmap)
        Try
            _inputBitmap = MyBitmap
        Catch er As Exception
            Throw er
        End Try
    End Sub


    Public Function Crop(ByVal Height As String, ByVal Width As String) As Bitmap
        Dim recCrop As Rectangle
        Dim bmpCrop As Bitmap
        Dim gphCrop As Graphics
        Dim recDest As Rectangle
        Try
            recCrop = New Rectangle(0, 0, Width, Height)
            bmpCrop = New Bitmap(recCrop.Width, recCrop.Height, _inputBitmap.PixelFormat)
            gphCrop = Graphics.FromImage(bmpCrop)
            recDest = New Rectangle(0, 0, Width, Height)
            gphCrop.DrawImage(_inputBitmap, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
                recCrop.Height, GraphicsUnit.Pixel)
            Return _inputBitmap
        Catch er As Exception
            Throw er
        Finally
            gphCrop.Dispose()
        End Try
    End Function

    Public Function Rotate(ByVal Flip As String) As Bitmap


        Try
            Select Case Flip
                Case "90"
                    _inputBitmap.RotateFlip(RotateFlipType.Rotate90FlipXY)
                Case "180"
                    _inputBitmap.RotateFlip(RotateFlipType.Rotate180FlipXY)
                Case "270"
                    _inputBitmap.RotateFlip(RotateFlipType.Rotate270FlipXY)
            End Select
            Return _inputBitmap

        Catch er As Exception
            Throw
        End Try

    End Function

    Public Function Resize(ByVal Height As String, ByVal Width As String)
        Dim inp As New IntPtr

        Try
            If Height > 0 And Width > 0 Then
                _inputBitmap = _inputBitmap.GetThumbnailImage(Height, Width, Nothing, inp)
            Else
                _inputBitmap = _inputBitmap
            End If
            Return _inputBitmap

        Catch er As Exception
            Throw er
        Finally
        End Try
    End Function

End Class
0
 
robertstuckeyAuthor Commented:
cloning was probably the wrong word to use

while your original routine was able to zoom and rotate the original image without cloning, it would need clones of the original bitmap in order to pan, in a similar way to how your second routines work

i was thinking of the bitmap function clone, the example given:

Dim Bump As New Bitmap(512, 512, Drawing.Imaging.PixelFormat.Format24bppRgb)
' Clone a portion of the Bitmap object.
Dim cloneRect As New RectangleF(128, 128, 256, 256)
Dim cloneBitmap As Bitmap = Bump.Clone(cloneRect, .PixelFormat.Format24bppRgb)

which would be similar to your gphCrop.DrawImage(_inputBitmap, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
                recCrop.Height, GraphicsUnit.Pixel)

this in conjunction with PictureBoxSizeMode.StretchImage would give the effect i wanted with the cloneRect X and Y performing the panning and the cloneRect width and height effecting the zoom with 512,512 as 1:1, 256,256 2:1 128,128 4:1 etc

I would still ideally want to wait for a solution to modifying the picturebox properties/methods rather than the methods so far covered
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Another example...

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents ZoomIn As System.Windows.Forms.Button
    Friend WithEvents ZoomOut As System.Windows.Forms.Button
    Friend WithEvents SelectImage As System.Windows.Forms.Button
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.ZoomIn = New System.Windows.Forms.Button
        Me.ZoomOut = New System.Windows.Forms.Button
        Me.SelectImage = New System.Windows.Forms.Button
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Controls.Add(Me.PictureBox1)
        Me.Panel1.Location = New System.Drawing.Point(8, 8)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(256, 256)
        Me.Panel1.TabIndex = 0
        '
        'ZoomIn
        '
        Me.ZoomIn.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ZoomIn.Location = New System.Drawing.Point(280, 40)
        Me.ZoomIn.Name = "ZoomIn"
        Me.ZoomIn.Size = New System.Drawing.Size(72, 24)
        Me.ZoomIn.TabIndex = 1
        Me.ZoomIn.Text = "Zoom In"
        '
        'ZoomOut
        '
        Me.ZoomOut.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ZoomOut.Location = New System.Drawing.Point(280, 72)
        Me.ZoomOut.Name = "ZoomOut"
        Me.ZoomOut.Size = New System.Drawing.Size(72, 24)
        Me.ZoomOut.TabIndex = 2
        Me.ZoomOut.Text = "Zoom Out"
        '
        'SelectImage
        '
        Me.SelectImage.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.SelectImage.Location = New System.Drawing.Point(280, 8)
        Me.SelectImage.Name = "SelectImage"
        Me.SelectImage.Size = New System.Drawing.Size(72, 24)
        Me.SelectImage.TabIndex = 3
        Me.SelectImage.Text = "Image"
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(256, 256)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(360, 270)
        Me.Controls.Add(Me.SelectImage)
        Me.Controls.Add(Me.ZoomOut)
        Me.Controls.Add(Me.ZoomIn)
        Me.Controls.Add(Me.Panel1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private startX As Single
    Private startY As Single

    Private Sub SelectImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectImage.Click
        OpenFileDialog1.Title = "Select an Image File"
        OpenFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Try
                PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbCrLf & "File: " & OpenFileDialog1.FileName, "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub ZoomIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomIn.Click
        If Not (PictureBox1.Image Is Nothing) Then
            Dim p As Single = PictureBox1.Width * 0.1
            PictureBox1.Visible = False
            PictureBox1.Location = New Point(PictureBox1.Location.X - p, PictureBox1.Location.Y - p)
            PictureBox1.Size = New Size(PictureBox1.Size.Width + (2 * p), PictureBox1.Size.Height + (2 * p))
            PictureBox1.Visible = True
        End If
    End Sub

    Private Sub ZoomOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ZoomOut.Click
        If Not (PictureBox1.Image Is Nothing) Then
            Dim p As Single = PictureBox1.Width * 0.1
            PictureBox1.Visible = False
            PictureBox1.Location = New Point(PictureBox1.Location.X + p, PictureBox1.Location.Y + p)
            PictureBox1.Size = New Size(PictureBox1.Size.Width - (2 * p), PictureBox1.Size.Height - (2 * p))
            PictureBox1.Visible = True
        End If
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = MouseButtons.Left Then
            startX = e.X
            startY = e.Y
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then
            If Not (PictureBox1.Image Is Nothing) Then
                PictureBox1.Location = New Point(PictureBox1.Left + e.X - startX, PictureBox1.Top + e.Y - startY)
            End If
        End If
    End Sub

End Class
0
 
robertstuckeyAuthor Commented:
I think what i was wanted is probably impossible so split points for a helpful answer

the good is only because it was not the solution i wanted
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now