hello, everyone i had a project which will have a function to resize the images in the picturebox..i've seen some code snippet in resizing the images. The code works fine but it automatically resizes the image when i run the program..what i need is that the user will be the one who will resize the pictures..does anyone knows how to do this? i'm using vb 2008. Any help is appreciated..

I did a resizeable picture box as an user control some time ago. The picture box (= user control) maintains it's size, but you can zoom in and out the image with Zoom property.

Option Explicit On
Option Strict On

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class CPictureBox

  Inherits ScrollableControl

  '--------------------------------------------------------
  ' CPictureBox.vb
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' CPictureBox Control
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' Copyright © 2007-2008 xxx
  '
  ' Language: Visual Basic 2005.NET
  '
  ' Version: 1.0.0
  '
  ' Created: 10.12.2007 xxx
  '
  ' Modified: 29.4.2008 xxx
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' Public data
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' Private data
  '--------------------------------------------------------

  Private m_Image As Image
  Private m_Zoom As Single = 1.0F
  Private m_InterpolationMode As InterpolationMode = InterpolationMode.High

  '--------------------------------------------------------
  ' Private procedures
  '--------------------------------------------------------

  Private Sub InitComponent()
    '
    ' Init component
    '

    Try
      Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
      Me.AutoScroll = True
      'Me.BackColor = Color.White
      m_InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
      m_Zoom = 1.0!
      m_Image = Nothing
    Catch ex As Exception
      
    End Try

  End Sub

  Private Sub UpdateScaleFactor()
    '
    ' Update scale factor
    '

    Try
      If m_Image Is Nothing Then
        Me.AutoScrollMargin = Me.Size
      Else
        Me.AutoScrollMinSize = New Size(CInt(Me.m_Image.Width * m_Zoom + 0.5F), CInt(Me.m_Image.Height * m_Zoom + 0.5F))
      End If
    Catch ex As Exception
      
    End Try

  End Sub

  '--------------------------------------------------------
  ' Event handlers
  '--------------------------------------------------------

  Public Sub New()
    '
    ' Initialize
    '

    Try
      InitComponent()
    Catch ex As Exception
      
    End Try

  End Sub

  Protected Overrides Sub Finalize()
    '
    ' Terminate control
    '

    Try
      MyBase.Finalize()
    Catch ex As Exception
      
    End Try

  End Sub

  Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
    '
    ' Do nothing
    '

    Try
      ' Do nothing
    Catch ex As Exception
      
    End Try

  End Sub

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    '
    ' If no image, don't bother
    '

    Try
      If m_Image Is Nothing Then
        MyBase.OnPaintBackground(e)
        Exit Sub
      End If
      ' Set up a zoom matrix
      Dim mx As New Matrix(m_Zoom, 0, 0, m_Zoom, 0, 0)
      mx.Translate(Me.AutoScrollPosition.X / m_Zoom, Me.AutoScrollPosition.Y / m_Zoom)
      e.Graphics.Transform = mx
      e.Graphics.InterpolationMode = m_InterpolationMode
      e.Graphics.DrawImage(m_Image, New Rectangle(0, 0, Me.m_Image.Width, Me.m_Image.Height), 0, 0, m_Image.Width, m_Image.Height, GraphicsUnit.Pixel)
      MyBase.OnPaint(e)
    Catch ex As Exception
      
    End Try

  End Sub

  '--------------------------------------------------------
  ' Public procedures
  '--------------------------------------------------------

  ''' <summary>
  ''' Sets or gets the image in CPictureBox
  ''' </summary>
  ''' <value>Image</value>
  ''' <returns>Image</returns>
  ''' <remarks></remarks>
  Public Property Image() As Image
    '
    Get
      Return m_Image
    End Get
    Set(ByVal value As Image)
      m_Image = value
      If m_Image IsNot Nothing Then
        If m_Image.Height > 0 Then
          m_Zoom = CSng(Me.Height / m_Image.Height)
        Else
          m_Zoom = 1.0!
        End If
      End If
      UpdateScaleFactor()
      Invalidate()
    End Set

  End Property

  ''' <summary>
  ''' Sets or gets the zoom factor
  ''' </summary>
  ''' <value>Zoom factor</value>
  ''' <returns>Zoom factor</returns>
  ''' <remarks>Zoom factor should be between 0-5</remarks>
  Public Property Zoom() As Single
    '
    Get
      Return m_Zoom
    End Get
    Set(ByVal value As Single)
      If value < 0 OrElse value < 0.00001 Then
        value = 0.00001F
      End If
      m_Zoom = value
      UpdateScaleFactor()
      Invalidate()
    End Set

  End Property

  ''' <summary>
  ''' Sets or gets the interpolation mode used in the drawing
  ''' </summary>
  ''' <value>System.Drawing.Drawing2D.InterpolationMode</value>
  ''' <returns>System.Drawing.Drawing2D.InterpolationMode</returns>
  ''' <remarks></remarks>
  Public Property InterpolationMode() As InterpolationMode
    ' The interpolation mode used to smooth the drawing
    Get
      Return m_InterpolationMode
    End Get
    Set(ByVal value As InterpolationMode)
      m_InterpolationMode = value
    End Set

  End Property

End Class

Haven't used the control for a while and it surely misses features :) But I hope that the zooming functionality helps you.

The code was written with VB 2005 and VB 2008 may have some of the functionality "built-in".

I did a resizeable picture box as an user control some time ago. The picture box (= user control) maintains it's size, but you can zoom in and out the image with Zoom property.

Option Explicit On
Option Strict On

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class CPictureBox

  Inherits ScrollableControl

  '--------------------------------------------------------
  ' CPictureBox.vb
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' CPictureBox Control
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' Copyright © 2007-2008 xxx
  '
  ' Language: Visual Basic 2005.NET
  '
  ' Version: 1.0.0
  '
  ' Created: 10.12.2007 xxx
  '
  ' Modified: 29.4.2008 xxx
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' Public data
  '--------------------------------------------------------

  '--------------------------------------------------------
  ' Private data
  '--------------------------------------------------------

  Private m_Image As Image
  Private m_Zoom As Single = 1.0F
  Private m_InterpolationMode As InterpolationMode = InterpolationMode.High

  '--------------------------------------------------------
  ' Private procedures
  '--------------------------------------------------------

  Private Sub InitComponent()
    '
    ' Init component
    '

    Try
      Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
      Me.AutoScroll = True
      'Me.BackColor = Color.White
      m_InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
      m_Zoom = 1.0!
      m_Image = Nothing
    Catch ex As Exception
      
    End Try

  End Sub

  Private Sub UpdateScaleFactor()
    '
    ' Update scale factor
    '

    Try
      If m_Image Is Nothing Then
        Me.AutoScrollMargin = Me.Size
      Else
        Me.AutoScrollMinSize = New Size(CInt(Me.m_Image.Width * m_Zoom + 0.5F), CInt(Me.m_Image.Height * m_Zoom + 0.5F))
      End If
    Catch ex As Exception
      
    End Try

  End Sub

  '--------------------------------------------------------
  ' Event handlers
  '--------------------------------------------------------

  Public Sub New()
    '
    ' Initialize
    '

    Try
      InitComponent()
    Catch ex As Exception
      
    End Try

  End Sub

  Protected Overrides Sub Finalize()
    '
    ' Terminate control
    '

    Try
      MyBase.Finalize()
    Catch ex As Exception
      
    End Try

  End Sub

  Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
    '
    ' Do nothing
    '

    Try
      ' Do nothing
    Catch ex As Exception
      
    End Try

  End Sub

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    '
    ' If no image, don't bother
    '

    Try
      If m_Image Is Nothing Then
        MyBase.OnPaintBackground(e)
        Exit Sub
      End If
      ' Set up a zoom matrix
      Dim mx As New Matrix(m_Zoom, 0, 0, m_Zoom, 0, 0)
      mx.Translate(Me.AutoScrollPosition.X / m_Zoom, Me.AutoScrollPosition.Y / m_Zoom)
      e.Graphics.Transform = mx
      e.Graphics.InterpolationMode = m_InterpolationMode
      e.Graphics.DrawImage(m_Image, New Rectangle(0, 0, Me.m_Image.Width, Me.m_Image.Height), 0, 0, m_Image.Width, m_Image.Height, GraphicsUnit.Pixel)
      MyBase.OnPaint(e)
    Catch ex As Exception
      
    End Try

  End Sub

  '--------------------------------------------------------
  ' Public procedures
  '--------------------------------------------------------

  ''' <summary>
  ''' Sets or gets the image in CPictureBox
  ''' </summary>
  ''' <value>Image</value>
  ''' <returns>Image</returns>
  ''' <remarks></remarks>
  Public Property Image() As Image
    '
    Get
      Return m_Image
    End Get
    Set(ByVal value As Image)
      m_Image = value
      If m_Image IsNot Nothing Then
        If m_Image.Height > 0 Then
          m_Zoom = CSng(Me.Height / m_Image.Height)
        Else
          m_Zoom = 1.0!
        End If
      End If
      UpdateScaleFactor()
      Invalidate()
    End Set

  End Property

  ''' <summary>
  ''' Sets or gets the zoom factor
  ''' </summary>
  ''' <value>Zoom factor</value>
  ''' <returns>Zoom factor</returns>
  ''' <remarks>Zoom factor should be between 0-5</remarks>
  Public Property Zoom() As Single
    '
    Get
      Return m_Zoom
    End Get
    Set(ByVal value As Single)
      If value < 0 OrElse value < 0.00001 Then
        value = 0.00001F
      End If
      m_Zoom = value
      UpdateScaleFactor()
      Invalidate()
    End Set

  End Property

  ''' <summary>
  ''' Sets or gets the interpolation mode used in the drawing
  ''' </summary>
  ''' <value>System.Drawing.Drawing2D.InterpolationMode</value>
  ''' <returns>System.Drawing.Drawing2D.InterpolationMode</returns>
  ''' <remarks></remarks>
  Public Property InterpolationMode() As InterpolationMode
    ' The interpolation mode used to smooth the drawing
    Get
      Return m_InterpolationMode
    End Get
    Set(ByVal value As InterpolationMode)
      m_InterpolationMode = value
    End Set

  End Property

End Class

Haven't used the control for a while and it surely misses features :) But I hope that the zooming functionality helps you.

The code was written with VB 2005 and VB 2008 may have some of the functionality "built-in".

i got this error message when i use the code...Base class 'System.Windows.Forms.ScrollableControl' specified for class 'CPictureBox' cannot be different from the base class 'System.Windows.Forms.Form' of one of its other partial types. what does it mean?

I'm not sure :( Like I said, I wrote it for 2005 and haven't tested it on 2008.

I would check first the references, it requires three of them:

  • System
  • System.Drawing
  • System.Windows.Forms

Also, the inherited ScrollableControl I used was .NET 2.0 version. It may be different in .NET 3.5.

And lastly here's a documentation for that error. It might help you solve the error you get.

hello..i'm sorry i could not understand the code would you mind clearing it to me..i'm new in vb.net thank you...

The code seems to be easy but i have no experience in coding so should i do the basics course for vb.net or not ?I want to do programming so guide me.

I had previous experience using VBA in MS Access so really going to VB.NET was what I felt comfortable with. I would suggest getting a couple of starter books, one on c# and one on vb.net. Then make the choice based on which language you prefer, before spending money on a course. Plus there are loads of great examples that people post on websites such as codeproject, and there are loads of great forums where people are very helpful with any queries you have.

This article has been dead for over six months. Start a new discussion instead.