How to calculate Pixel position when zoom in or zoom out an
How to calculate Pixel position when zoom in or zoom out an image in c# . I have an application in c# that displayed image when I clicked with mouse at a point on the image it give the pixel value for that point (using e.x and e.y). now i zoom in or out the image. How to get the pixel of the same point in zoomed position on run time. (I don\'t want to click again at the same position after zoomed). zoom in by adding 0.2 to the current zoom and zoom out by subtracting 0.2 from the current zoom. Is there any procedure that give the correct pixel value on the original image?!
Solution
Imports System.ComponentModel
Imports System.Drawing
\'Public properties for the ZoomPictureBox
Partial Public Class ZoomPictureBox
<Category(\"_ZoomPictureBox\"), _
Description(\"Enable dragging. Set to False if you implement other means of image scrolling.\")> _
Public Property EnableMouseDragging As Boolean
Get
Return _EnableMouseWheelDragging
End Get
Set(value As Boolean)
_EnableMouseWheelDragging = value
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
Description(\"Enable mouse wheel zooming. Set to false e.g. if you control zooming with a TrackBar.\")> _
Public Property EnableMouseWheelZooming As Boolean
Get
Return _EnableMouseWheelZooming
End Get
Set(value As Boolean)
_EnableMouseWheelZooming = True
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
Description(\"Image to display in the ZoomPictureBox.\")> _
Public Property Image() As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
If value IsNot Nothing Then
InitializeImage()
Else
_imageInitialized = False
End If
End Set
End Property
<Browsable(False), _
Description(\"The bounding rectangle of the zoomed image relative to the control origin.\")> _
Public ReadOnly Property ImageBounds() As Rectangle
Get
Return _ImageBounds
End Get
End Property
<Browsable(False), _
Description(\"Location of the top left corner of the zoomed image relative to the control origin.\")> _
Public Property ImagePosition() As Point
Get
Return _ImageBounds.Location
End Get
Set(ByVal value As Point)
Me.Invalidate(_ImageBounds)
_ImageBounds.X = value.X
_ImageBounds.Y = value.Y
Me.Invalidate(_ImageBounds)
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
Description(\"The maximum zoom magnification.\")> _
Public Property MaximumZoomFactor As Double
Get
Return _MaximumZoomFactor
End Get
Set(value As Double)
_MaximumZoomFactor = value
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
Description(\"Minimum height of the zoomed image in pixels.\")> _
Public Property MinimumImageHeight As Integer
Get
Return _MinimumImageHeight
End Get
Set(value As Integer)
_MinimumImageHeight = value
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
Description(\"Minimum width of the zoomed image in pixels.\")> _
Public Property MinimumImageWidth As Integer
Get
Return _MinimumImageWidth
End Get
Set(value As Integer)
_MinimumImageWidth = value
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
Description(\"Sets the responsiveness of zooming to the mouse wheel. Choose a lower value for faster zooming.\")> _
Public Property MouseWheelDivisor As Integer
Get
Return _MouseWheelDivisor
End Get
Set(value As Integer)
_MouseWheelDivisor = value
End Set
End Property
<Browsable(False), _
Description(\"Linear size of the zoomed image as a fraction of that of the source Image.\")> _
Public Property ZoomFactor() As Double
Get
Return _ZoomFactor
End Get
Set(ByVal value As Double)
_ZoomFactor = ValidateZoomFactor(value)
If _imageInitialized Then
Me.Invalidate(_ImageBounds)
_ImageBounds = GetZoomedBounds()
Me.Invalidate(_ImageBounds)
End If
End Set
End Property
<Category(\"_ZoomPictureBox\"), _
DefaultValue(ZoomType.MousePosition), _
Description(\"Image zooming around the mouse position, image center or control center\")> _
Public Property ZoomMode() As ZoomType
Get
Return _ZoomMode
End Get
Set(ByVal value As ZoomType)
_ZoomMode = value
End Set
End Property
End Class