943,083 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 5821
  • VB.NET RSS
Feb 17th, 2009
0

How to resize images at runtime using mouse...

Expand Post »
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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cutieann12 is offline Offline
7 posts
since Nov 2008
Feb 17th, 2009
0

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.
VB.NET Syntax (Toggle Plain Text)
  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".
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Feb 17th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Teme64 ...
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.
VB.NET Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cutieann12 is offline Offline
7 posts
since Nov 2008
Feb 18th, 2009
0

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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Feb 21st, 2009
0

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cutieann12 is offline Offline
7 posts
since Nov 2008
Apr 29th, 2010
0
Re: How to resize images at runtime using mouse...
This code will need to be placed in a class rather than a form with it being a control.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Manos_Arriba is offline Offline
2 posts
since Apr 2010
Apr 29th, 2010
0
Re: How to resize images at runtime using mouse...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
AckleyBarr is offline Offline
3 posts
since Apr 2010
Apr 29th, 2010
0
Re: How to resize images at runtime using mouse...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Manos_Arriba is offline Offline
2 posts
since Apr 2010
Apr 29th, 2010
0
Re: How to resize images at runtime using mouse...
Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Closed.
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: INSTER SQL but ID already exists
Next Thread in VB.NET Forum Timeline: vb.net code for sending e-mail





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC