How to resize images at runtime using mouse...

Reply

Join Date: Nov 2008
Posts: 7
Reputation: cutieann12 is an unknown quantity at this point 
Solved Threads: 0
cutieann12 cutieann12 is offline Offline
Newbie Poster

How to resize images at runtime using mouse...

 
0
  #1
Feb 17th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: How to resize images at runtime using mouse...

 
0
  #2
Feb 17th, 2009
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.
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Imports System
  5. Imports System.ComponentModel
  6. Imports System.Drawing
  7. Imports System.Drawing.Drawing2D
  8. Imports System.Windows.Forms
  9.  
  10. Public Class CPictureBox
  11.  
  12. Inherits ScrollableControl
  13.  
  14. '--------------------------------------------------------
  15. ' CPictureBox.vb
  16. '--------------------------------------------------------
  17.  
  18. '--------------------------------------------------------
  19. ' CPictureBox Control
  20. '--------------------------------------------------------
  21.  
  22. '--------------------------------------------------------
  23. ' Copyright © 2007-2008 xxx
  24. '
  25. ' Language: Visual Basic 2005.NET
  26. '
  27. ' Version: 1.0.0
  28. '
  29. ' Created: 10.12.2007 xxx
  30. '
  31. ' Modified: 29.4.2008 xxx
  32. '--------------------------------------------------------
  33.  
  34. '--------------------------------------------------------
  35. ' Public data
  36. '--------------------------------------------------------
  37.  
  38. '--------------------------------------------------------
  39. ' Private data
  40. '--------------------------------------------------------
  41.  
  42. Private m_Image As Image
  43. Private m_Zoom As Single = 1.0F
  44. Private m_InterpolationMode As InterpolationMode = InterpolationMode.High
  45.  
  46. '--------------------------------------------------------
  47. ' Private procedures
  48. '--------------------------------------------------------
  49.  
  50. Private Sub InitComponent()
  51. '
  52. ' Init component
  53. '
  54.  
  55. Try
  56. Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
  57. Me.AutoScroll = True
  58. 'Me.BackColor = Color.White
  59. m_InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
  60. m_Zoom = 1.0!
  61. m_Image = Nothing
  62. Catch ex As Exception
  63.  
  64. End Try
  65.  
  66. End Sub
  67.  
  68. Private Sub UpdateScaleFactor()
  69. '
  70. ' Update scale factor
  71. '
  72.  
  73. Try
  74. If m_Image Is Nothing Then
  75. Me.AutoScrollMargin = Me.Size
  76. Else
  77. Me.AutoScrollMinSize = New Size(CInt(Me.m_Image.Width * m_Zoom + 0.5F), CInt(Me.m_Image.Height * m_Zoom + 0.5F))
  78. End If
  79. Catch ex As Exception
  80.  
  81. End Try
  82.  
  83. End Sub
  84.  
  85. '--------------------------------------------------------
  86. ' Event handlers
  87. '--------------------------------------------------------
  88.  
  89. Public Sub New()
  90. '
  91. ' Initialize
  92. '
  93.  
  94. Try
  95. InitComponent()
  96. Catch ex As Exception
  97.  
  98. End Try
  99.  
  100. End Sub
  101.  
  102. Protected Overrides Sub Finalize()
  103. '
  104. ' Terminate control
  105. '
  106.  
  107. Try
  108. MyBase.Finalize()
  109. Catch ex As Exception
  110.  
  111. End Try
  112.  
  113. End Sub
  114.  
  115. Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
  116. '
  117. ' Do nothing
  118. '
  119.  
  120. Try
  121. ' Do nothing
  122. Catch ex As Exception
  123.  
  124. End Try
  125.  
  126. End Sub
  127.  
  128. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  129. '
  130. ' If no image, don't bother
  131. '
  132.  
  133. Try
  134. If m_Image Is Nothing Then
  135. MyBase.OnPaintBackground(e)
  136. Exit Sub
  137. End If
  138. ' Set up a zoom matrix
  139. Dim mx As New Matrix(m_Zoom, 0, 0, m_Zoom, 0, 0)
  140. mx.Translate(Me.AutoScrollPosition.X / m_Zoom, Me.AutoScrollPosition.Y / m_Zoom)
  141. e.Graphics.Transform = mx
  142. e.Graphics.InterpolationMode = m_InterpolationMode
  143. 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)
  144. MyBase.OnPaint(e)
  145. Catch ex As Exception
  146.  
  147. End Try
  148.  
  149. End Sub
  150.  
  151. '--------------------------------------------------------
  152. ' Public procedures
  153. '--------------------------------------------------------
  154.  
  155. ''' <summary>
  156. ''' Sets or gets the image in CPictureBox
  157. ''' </summary>
  158. ''' <value>Image</value>
  159. ''' <returns>Image</returns>
  160. ''' <remarks></remarks>
  161. Public Property Image() As Image
  162. '
  163. Get
  164. Return m_Image
  165. End Get
  166. Set(ByVal value As Image)
  167. m_Image = value
  168. If m_Image IsNot Nothing Then
  169. If m_Image.Height > 0 Then
  170. m_Zoom = CSng(Me.Height / m_Image.Height)
  171. Else
  172. m_Zoom = 1.0!
  173. End If
  174. End If
  175. UpdateScaleFactor()
  176. Invalidate()
  177. End Set
  178.  
  179. End Property
  180.  
  181. ''' <summary>
  182. ''' Sets or gets the zoom factor
  183. ''' </summary>
  184. ''' <value>Zoom factor</value>
  185. ''' <returns>Zoom factor</returns>
  186. ''' <remarks>Zoom factor should be between 0-5</remarks>
  187. Public Property Zoom() As Single
  188. '
  189. Get
  190. Return m_Zoom
  191. End Get
  192. Set(ByVal value As Single)
  193. If value < 0 OrElse value < 0.00001 Then
  194. value = 0.00001F
  195. End If
  196. m_Zoom = value
  197. UpdateScaleFactor()
  198. Invalidate()
  199. End Set
  200.  
  201. End Property
  202.  
  203. ''' <summary>
  204. ''' Sets or gets the interpolation mode used in the drawing
  205. ''' </summary>
  206. ''' <value>System.Drawing.Drawing2D.InterpolationMode</value>
  207. ''' <returns>System.Drawing.Drawing2D.InterpolationMode</returns>
  208. ''' <remarks></remarks>
  209. Public Property InterpolationMode() As InterpolationMode
  210. ' The interpolation mode used to smooth the drawing
  211. Get
  212. Return m_InterpolationMode
  213. End Get
  214. Set(ByVal value As InterpolationMode)
  215. m_InterpolationMode = value
  216. End Set
  217.  
  218. End Property
  219.  
  220. 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".
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: cutieann12 is an unknown quantity at this point 
Solved Threads: 0
cutieann12 cutieann12 is offline Offline
Newbie Poster

Re: How to resize images at runtime using mouse...

 
0
  #3
Feb 17th, 2009
Originally Posted by Teme64 View Post
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.
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Imports System
  5. Imports System.ComponentModel
  6. Imports System.Drawing
  7. Imports System.Drawing.Drawing2D
  8. Imports System.Windows.Forms
  9.  
  10. Public Class CPictureBox
  11.  
  12. Inherits ScrollableControl
  13.  
  14. '--------------------------------------------------------
  15. ' CPictureBox.vb
  16. '--------------------------------------------------------
  17.  
  18. '--------------------------------------------------------
  19. ' CPictureBox Control
  20. '--------------------------------------------------------
  21.  
  22. '--------------------------------------------------------
  23. ' Copyright © 2007-2008 xxx
  24. '
  25. ' Language: Visual Basic 2005.NET
  26. '
  27. ' Version: 1.0.0
  28. '
  29. ' Created: 10.12.2007 xxx
  30. '
  31. ' Modified: 29.4.2008 xxx
  32. '--------------------------------------------------------
  33.  
  34. '--------------------------------------------------------
  35. ' Public data
  36. '--------------------------------------------------------
  37.  
  38. '--------------------------------------------------------
  39. ' Private data
  40. '--------------------------------------------------------
  41.  
  42. Private m_Image As Image
  43. Private m_Zoom As Single = 1.0F
  44. Private m_InterpolationMode As InterpolationMode = InterpolationMode.High
  45.  
  46. '--------------------------------------------------------
  47. ' Private procedures
  48. '--------------------------------------------------------
  49.  
  50. Private Sub InitComponent()
  51. '
  52. ' Init component
  53. '
  54.  
  55. Try
  56. Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
  57. Me.AutoScroll = True
  58. 'Me.BackColor = Color.White
  59. m_InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
  60. m_Zoom = 1.0!
  61. m_Image = Nothing
  62. Catch ex As Exception
  63.  
  64. End Try
  65.  
  66. End Sub
  67.  
  68. Private Sub UpdateScaleFactor()
  69. '
  70. ' Update scale factor
  71. '
  72.  
  73. Try
  74. If m_Image Is Nothing Then
  75. Me.AutoScrollMargin = Me.Size
  76. Else
  77. Me.AutoScrollMinSize = New Size(CInt(Me.m_Image.Width * m_Zoom + 0.5F), CInt(Me.m_Image.Height * m_Zoom + 0.5F))
  78. End If
  79. Catch ex As Exception
  80.  
  81. End Try
  82.  
  83. End Sub
  84.  
  85. '--------------------------------------------------------
  86. ' Event handlers
  87. '--------------------------------------------------------
  88.  
  89. Public Sub New()
  90. '
  91. ' Initialize
  92. '
  93.  
  94. Try
  95. InitComponent()
  96. Catch ex As Exception
  97.  
  98. End Try
  99.  
  100. End Sub
  101.  
  102. Protected Overrides Sub Finalize()
  103. '
  104. ' Terminate control
  105. '
  106.  
  107. Try
  108. MyBase.Finalize()
  109. Catch ex As Exception
  110.  
  111. End Try
  112.  
  113. End Sub
  114.  
  115. Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
  116. '
  117. ' Do nothing
  118. '
  119.  
  120. Try
  121. ' Do nothing
  122. Catch ex As Exception
  123.  
  124. End Try
  125.  
  126. End Sub
  127.  
  128. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  129. '
  130. ' If no image, don't bother
  131. '
  132.  
  133. Try
  134. If m_Image Is Nothing Then
  135. MyBase.OnPaintBackground(e)
  136. Exit Sub
  137. End If
  138. ' Set up a zoom matrix
  139. Dim mx As New Matrix(m_Zoom, 0, 0, m_Zoom, 0, 0)
  140. mx.Translate(Me.AutoScrollPosition.X / m_Zoom, Me.AutoScrollPosition.Y / m_Zoom)
  141. e.Graphics.Transform = mx
  142. e.Graphics.InterpolationMode = m_InterpolationMode
  143. 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)
  144. MyBase.OnPaint(e)
  145. Catch ex As Exception
  146.  
  147. End Try
  148.  
  149. End Sub
  150.  
  151. '--------------------------------------------------------
  152. ' Public procedures
  153. '--------------------------------------------------------
  154.  
  155. ''' <summary>
  156. ''' Sets or gets the image in CPictureBox
  157. ''' </summary>
  158. ''' <value>Image</value>
  159. ''' <returns>Image</returns>
  160. ''' <remarks></remarks>
  161. Public Property Image() As Image
  162. '
  163. Get
  164. Return m_Image
  165. End Get
  166. Set(ByVal value As Image)
  167. m_Image = value
  168. If m_Image IsNot Nothing Then
  169. If m_Image.Height > 0 Then
  170. m_Zoom = CSng(Me.Height / m_Image.Height)
  171. Else
  172. m_Zoom = 1.0!
  173. End If
  174. End If
  175. UpdateScaleFactor()
  176. Invalidate()
  177. End Set
  178.  
  179. End Property
  180.  
  181. ''' <summary>
  182. ''' Sets or gets the zoom factor
  183. ''' </summary>
  184. ''' <value>Zoom factor</value>
  185. ''' <returns>Zoom factor</returns>
  186. ''' <remarks>Zoom factor should be between 0-5</remarks>
  187. Public Property Zoom() As Single
  188. '
  189. Get
  190. Return m_Zoom
  191. End Get
  192. Set(ByVal value As Single)
  193. If value < 0 OrElse value < 0.00001 Then
  194. value = 0.00001F
  195. End If
  196. m_Zoom = value
  197. UpdateScaleFactor()
  198. Invalidate()
  199. End Set
  200.  
  201. End Property
  202.  
  203. ''' <summary>
  204. ''' Sets or gets the interpolation mode used in the drawing
  205. ''' </summary>
  206. ''' <value>System.Drawing.Drawing2D.InterpolationMode</value>
  207. ''' <returns>System.Drawing.Drawing2D.InterpolationMode</returns>
  208. ''' <remarks></remarks>
  209. Public Property InterpolationMode() As InterpolationMode
  210. ' The interpolation mode used to smooth the drawing
  211. Get
  212. Return m_InterpolationMode
  213. End Get
  214. Set(ByVal value As InterpolationMode)
  215. m_InterpolationMode = value
  216. End Set
  217.  
  218. End Property
  219.  
  220. 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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: How to resize images at runtime using mouse...

 
0
  #4
Feb 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: cutieann12 is an unknown quantity at this point 
Solved Threads: 0
cutieann12 cutieann12 is offline Offline
Newbie Poster

Re: How to resize images at runtime using mouse...

 
0
  #5
Feb 21st, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2210 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC