| | |
How to resize images at runtime using mouse...
![]() |
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
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".
VB.NET Syntax (Toggle Plain Text)
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
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".
Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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.
Haven't used the control for a while and it surely misses featuresVB.NET Syntax (Toggle Plain Text)
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 ClassBut 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'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:
And lastly here's a documentation for that error. It might help you solve the error you get.
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
And lastly here's a documentation for that error. It might help you solve the error you get.
Teme64 @ Windows Developer Blog
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: Deserialize different types of objects from a NetworkStream
- Next Thread: Correctly displaying image in PictureBox?
Views: 2210 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net 2005 2008 access application arithmetic array arrays basic binary bing browser button buttons c# center checkbox client code combobox connection control convert crystal crystalreport dal data database datagrid datagridview date design designer dissertation dissertations editvb.net error excel fade file file-dialog image images input insert internet listview login loops memory mobile ms msaccess navigate-cell net path picturebox picturebox2 port print printing problem reports" save saving security serial server settings sms socket sorting sql statement studio subroutine syntax textbox time timer upload user validation vb vb.net vb2008 vbnet view visual visualbasic visualbasic.net visualstudio2008 vs2008 web webbrowser windows winforms wpf wrapingcode xml





