Determines how the control displays the image and the automatic adjustments of the display rectangles.
public enum ControlSizeMode
0 |
None |
(0) No special sizing
|
1 |
ActualSize |
(1) Use the image actual size
|
2 |
Fit |
(2) Fit the image into the viewing area while maintaining the aspect ratio. If the image size is smaller than the viewing area, no resizing is done.
|
3 |
FitAlways |
(3) Always fit the image into the viewing area while maintaining the aspect ratio even if the image size is smaller than the viewing area (in which case, the image will be scaled up).
|
4 |
FitWidth |
(4) Fit the image width to be the size of the width of the viewing area while maintaining the aspect ratio.
|
5 |
FitHeight |
(5) Fit the image height to be the size of the height of the viewing area while maintaining the aspect ratio.
|
6 |
Stretch |
(6) Fit the image to fill the viewing area. Aspect ratio might not be maintained.
|
ControlSizeMode is used by the ImageViewer.Zoom and ImageViewerItem.Zoom methods.
using Leadtools;
using Leadtools.Controls;
using Leadtools.Codecs;
using Leadtools.Drawing;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;
public void ImageViewerZoom_Example()
{
// Create the form that holds the ImageViewer
new MyForm2().ShowDialog();
}
class MyForm2 : Form
{
public MyForm2()
{
this.Size = new Size(800, 800);
}
// LEADTOOLS ImageViewer to be used with this example
private ImageViewer _imageViewer;
// Ratio to use when the user zooms in or out. Change this if another value is needed,
// for example, 2.0 will zoom the viewer in by 200% and out by 50% each time
private const double _zoomRatio = 1.2;
protected override void OnLoad(EventArgs e)
{
// Create a panel to the top
var panel = new Panel();
panel.Dock = DockStyle.Top;
panel.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(panel);
// Add a combo box for the zoom values
var zoomComboBox = new ComboBox();
zoomComboBox.Name = "zoomComboBox";
zoomComboBox.Location = new Point(8, 8);
zoomComboBox.DropDownStyle = ComboBoxStyle.DropDown;
// Add default zoom values
zoomComboBox.Items.AddRange(new string[]
{
"10%", "25%", "50%", "75%", "100%", "125%", "200%", "400%", "800%",
"1600%", "2400%", "3200%", "6400%", "Actual Size", "Fit Page", "Fit Width", "Fit Height"
});
panel.Controls.Add(zoomComboBox);
// Add zoom in and out buttons
var zoomInButton = new Button();
zoomInButton.Text = "+";
zoomInButton.Location = new Point(zoomComboBox.Right + 8, zoomComboBox.Top - 1);
panel.Controls.Add(zoomInButton);
var zoomOutButton = new Button();
zoomOutButton.Text = "-";
zoomOutButton.Location = new Point(zoomInButton.Right + 2, zoomComboBox.Top - 1);
panel.Controls.Add(zoomOutButton);
// Create the image viewer taking the rest of the form
_imageViewer = new ImageViewer();
_imageViewer.Dock = DockStyle.Fill;
_imageViewer.ViewBorderThickness = 1;
this.Controls.Add(_imageViewer);
_imageViewer.BringToFront();
// Set UseDpi to true to view the image at its original resolution
_imageViewer.UseDpi = true;
// Add Pan/Zoom interactive mode
// Click and drag to pan, CTRL-Click and drag to zoom in and out
_imageViewer.DefaultInteractiveMode = new ImageViewerPanZoomInteractiveMode();
// Initialize the zoom system
InitZoom(zoomComboBox, zoomInButton, zoomOutButton);
// Load an image
using (var codecs = new RasterCodecs())
_imageViewer.Image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"));
base.OnLoad(e);
}
private void UpdateZoomValueFromView()
{
// Get the current scale factor from the viewer and set it in the zoom combo box
var percentage = _imageViewer.ScaleFactor * 100.0;
var zoomComboBox = this.Controls.Find("zoomComboBox", true)[0] as ComboBox;
zoomComboBox.Text = percentage.ToString("F1") + "%";
}
private void InitZoom(ComboBox zoomComboBox, Button zoomInButton, Button zoomOutButton)
{
// Add support for user typing the zoom value in the combo box directly
zoomComboBox.LostFocus += (sender, e) =>
{
// When the user moves away from the combo box, make sure it has a valid value
UpdateZoomValueFromView();
};
zoomComboBox.KeyPress += (sender, e) =>
{
// Check if the finished editing by pressing Enter
if (e.KeyChar == (char)Keys.Return)
{
// Yes, get the new value
var value = zoomComboBox.Text.Trim();
if (!string.IsNullOrEmpty(value))
{
// Remove the % sign if present
if (value.EndsWith("%"))
value = value.Remove(value.Length - 1, 1).Trim();
// Try to parse the new zoom value
double percentage;
if (double.TryParse(value, out percentage))
{
// Valid value, zoom the viewer
_imageViewer.Zoom(ControlSizeMode.None, percentage / 100.0, _imageViewer.DefaultZoomOrigin);
}
// And update the combo box from this value (in case we set a value not in the min/max range)
UpdateZoomValueFromView();
}
}
};
zoomComboBox.SelectedIndexChanged += (sender, e) =>
{
// Get the value from the combo box
var value = zoomComboBox.Text.Trim();
switch (value)
{
case "Actual Size":
_imageViewer.Zoom(ControlSizeMode.ActualSize, 1, _imageViewer.DefaultZoomOrigin);
break;
case "Fit Page":
_imageViewer.Zoom(ControlSizeMode.FitAlways, 1, _imageViewer.DefaultZoomOrigin);
break;
case "Fit Width":
_imageViewer.Zoom(ControlSizeMode.FitWidth, 1, _imageViewer.DefaultZoomOrigin);
break;
case "Fit Height":
_imageViewer.Zoom(ControlSizeMode.FitHeight, 1, _imageViewer.DefaultZoomOrigin);
break;
default:
if (!string.IsNullOrEmpty(value))
{
// A percentage, use it
var percentage = double.Parse(value.Substring(0, value.Length - 1));
_imageViewer.Zoom(ControlSizeMode.None, percentage / 100.0, _imageViewer.DefaultZoomOrigin);
}
break;
}
};
// For zoom in button, use current ScaleFactor multiplied by the ratio
zoomInButton.Click += (sender, e) =>
{
_imageViewer.Zoom(ControlSizeMode.None, _imageViewer.ScaleFactor * _zoomRatio, _imageViewer.DefaultZoomOrigin);
};
// For zoom out button, use current ScaleFactor divided by the ratio
zoomOutButton.Click += (sender, e) =>
{
_imageViewer.Zoom(ControlSizeMode.None, _imageViewer.ScaleFactor / _zoomRatio, _imageViewer.DefaultZoomOrigin);
};
// Also update the value from the viewer when the transform changed, this happens if the user selects a fit mode,
// such as fit width or fit page. The viewer will change the scale factor accordingly and we need to update it
// in our combo box
// This also takes care of Pan/Zoom interactive mode updating the scale fatcor
_imageViewer.TransformChanged += (sender, e) =>
{
UpdateZoomValueFromView();
};
// Get the value from the viewer into the combo box
UpdateZoomValueFromView();
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}