DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   How to resize images at runtime using mouse... (http://www.daniweb.com/forums/thread176182.html)

cutieann12 Feb 17th, 2009 3:49 am
How to resize images at runtime using mouse...
 
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..

Teme64 Feb 17th, 2009 6:31 am
Re: How to resize images at runtime using mouse...
 
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".

cutieann12 Feb 17th, 2009 11:19 am
Re: How to resize images at runtime using mouse...
 
Quote:

Originally Posted by Teme64 (Post 805617)
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?

Teme64 Feb 18th, 2009 2:00 am
Re: How to resize images at runtime using mouse...
 
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.

cutieann12 Feb 21st, 2009 9:48 am
Re: How to resize images at runtime using mouse...
 
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...


All times are GMT -4. The time now is 6:16 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC