How to use the external scroll bars with image viewer?
In This Topic
In order to use the external scroll bars in
ImageViewer class is necessary to:
- disable standard scroll bars in ImageViewer class by setting the value of ImageViewer.AutoScroll property to false
- subscribe to the ImageViewer.ImageLoaded event and initiale the parameters of external scroll bars when image is loaded
- subscribe to the ImageViewer.SizeChanged event and update the external scroll bars when size of ImageViewer is changed
- subscribe to the ImageViewer.ZoomChanged event and update the external scroll bars when image zoom in ImageViewer is changed
- subscribe to the System.Windows.Forms.ScrollBar.ValueChanged event of external scroll bars and scroll the image in viewer using the ImageViewer.ViewerState.AutoScrollPosition property.
ImageViewerState class provides the following abilities for manipulating the scroll bars of
ImageViewer:
- changing the scroll bars position
- obtaining size of horizontal and vertical scroll bar.
Here is C#/VB.NET code that demonstrates how to use external scroll bars with
ImageViewer class:
public partial class ImageViewer_ExternalScrollBars : System.Windows.Forms.Form
{
#region Constructors
public ImageViewer_ExternalScrollBars()
{
InitializeComponent();
// ...
imageViewer.ZoomChanged += new System.EventHandler<Vintasoft.Imaging.UI.ZoomChangedEventArgs>(imageViewer_ZoomChanged);
imageViewer.ImageLoaded += new System.EventHandler<Vintasoft.Imaging.ImageLoadedEventArgs>(imageViewer_ImageLoaded);
imageViewer.SizeChanged += new System.EventHandler(imageViewer_SizeChanged);
hScrollBar1.ValueChanged += new System.EventHandler(ScrollBar_ValueChanged);
vScrollBar1.ValueChanged += new System.EventHandler(ScrollBar_ValueChanged);
imageViewer.AutoScroll = false;
// ...
}
#endregion
#region Methods
/// <summary>
/// Image viewer size is changed.
/// </summary>
private void imageViewer_SizeChanged(object sender, System.EventArgs e)
{
SetScrollBarParams();
}
/// <summary>
/// Image is loaded in image viewer.
/// </summary>
private void imageViewer_ImageLoaded(object sender, Vintasoft.Imaging.ImageLoadedEventArgs e)
{
SetScrollBarParams();
}
/// <summary>
/// Image viewer zoom is changed.
/// </summary>
private void imageViewer_ZoomChanged(object sender, Vintasoft.Imaging.UI.ZoomChangedEventArgs e)
{
SetScrollBarParams();
}
/// <summary>
/// Sets parameters of scroll bars.
/// </summary>
private void SetScrollBarParams()
{
// initialize horizontal scroll bar
int width = imageViewer.ViewerState.AutoScrollSize.Width;
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = width + width / 5;
hScrollBar1.LargeChange = width / 5;
hScrollBar1.SmallChange = width / 20;
hScrollBar1.Enabled = width != 0;
// initialize vertical scroll bar
int height = imageViewer.ViewerState.AutoScrollSize.Height;
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum = height + height / 5;
vScrollBar1.LargeChange = height / 5;
vScrollBar1.SmallChange = height / 20;
vScrollBar1.Enabled = height != 0;
}
/// <summary>
/// Scrollbar value is changed.
/// </summary>
private void ScrollBar_ValueChanged(object sender, System.EventArgs e)
{
System.Drawing.PointF currentPosition =
new System.Drawing.PointF((float)hScrollBar1.Value, (float)vScrollBar1.Value);
imageViewer.ViewerState.AutoScrollPosition = currentPosition;
}
#endregion
}
Public Partial Class ImageViewer_ExternalScrollBars
Inherits System.Windows.Forms.Form
#Region "Constructors"
Public Sub New()
InitializeComponent()
' ...
AddHandler imageViewer.ZoomChanged, New System.EventHandler(Of Vintasoft.Imaging.UI.ZoomChangedEventArgs)(AddressOf imageViewer_ZoomChanged)
AddHandler imageViewer.ImageLoaded, New System.EventHandler(Of Vintasoft.Imaging.ImageLoadedEventArgs)(AddressOf imageViewer_ImageLoaded)
AddHandler imageViewer.SizeChanged, New System.EventHandler(AddressOf imageViewer_SizeChanged)
AddHandler hScrollBar1.ValueChanged, New System.EventHandler(AddressOf ScrollBar_ValueChanged)
AddHandler vScrollBar1.ValueChanged, New System.EventHandler(AddressOf ScrollBar_ValueChanged)
' ...
imageViewer.AutoScroll = False
End Sub
#End Region
#Region "Methods"
''' <summary>
''' Image viewer size is changed.
''' </summary>
Private Sub imageViewer_SizeChanged(sender As Object, e As System.EventArgs)
SetScrollBarParams()
End Sub
''' <summary>
''' Image is loaded in image viewer.
''' </summary>
Private Sub imageViewer_ImageLoaded(sender As Object, e As Vintasoft.Imaging.ImageLoadedEventArgs)
SetScrollBarParams()
End Sub
''' <summary>
''' Image viewer zoom is changed.
''' </summary>
Private Sub imageViewer_ZoomChanged(sender As Object, e As Vintasoft.Imaging.UI.ZoomChangedEventArgs)
SetScrollBarParams()
End Sub
''' <summary>
''' Sets parameters of scroll bars.
''' </summary>
Private Sub SetScrollBarParams()
' initialize horizontal scroll bar
Dim width As Integer = imageViewer.ViewerState.AutoScrollSize.Width
hScrollBar1.Minimum = 0
hScrollBar1.Maximum = width + width \ 5
hScrollBar1.LargeChange = width \ 5
hScrollBar1.SmallChange = width \ 20
hScrollBar1.Enabled = width <> 0
' initialize vertical scroll bar
Dim height As Integer = imageViewer.ViewerState.AutoScrollSize.Height
vScrollBar1.Minimum = 0
vScrollBar1.Maximum = height + height \ 5
vScrollBar1.LargeChange = height \ 5
vScrollBar1.SmallChange = height \ 20
vScrollBar1.Enabled = height <> 0
End Sub
''' <summary>
''' Scrollbar value is changed.
''' </summary>
Private Sub ScrollBar_ValueChanged(sender As Object, e As System.EventArgs)
Dim currentPosition As New System.Drawing.PointF(CSng(hScrollBar1.Value), CSng(vScrollBar1.Value))
imageViewer.ViewerState.AutoScrollPosition = currentPosition
End Sub
#End Region
End Class