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.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".
Like I said, I wrote it for 2005 and haven't tested it on 2008.| DaniWeb Message | |
| Cancel Changes | |