Tutorial: Display Images in an Image Viewer

Posted on 2020-05-22 Nick Villalobos

The LEADTOOLS ImageViewer is a cross-platform viewer that provides image display controls for desktop, web, tablet, and phone applications. Not only can this viewer display more than 150 different file formats, it also has a numerous amount of interactive modes that can be incorporated to your company's workflow. These modes provide an efficient and friendly user experience.

Once you get the ImageViewer up and running, there is so much more that you can do besides viewing images. You can add options to annotate and mark up images, OCR images, and improve the image quality of document images with the LEADTOOLS Document Cleanup Image processing functions. The LEADTOOLS ImageViewer library can be added to just about any workspace and help you out within minutes, so try it out!

The below code shows the basics of loading an image in the image viewer using both Winforms and WPF. If you want a complete step-by-step tutorial, check out our tutorials:


Winforms

private _viewer imageViewer; 

private void Form1_Load(object sender, EventArgs e) 
{ 
   _viewer = new ImageViewer(); 
   _viewer.Dock = DockStyle.Fill; 
   _viewer.BackColor = Color.DarkGray; 
   Controls.Add(_viewer); 
   _viewer.BringToFront(); 
} 


private void openToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
        { 
            OpenFileDialog dlg = new OpenFileDialog(); 
            dlg.InitialDirectory = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
            if (dlg.ShowDialog(this) == DialogResult.OK) 
            { 
               _viewer.Image = codecs.Load(dlg.FileName); 
            } 
        }
}

WPF

private ImageViewer imageViewer; 
private RasterCodecs codecs; 

// Initialize Image Viewer 
private void InitViewer() 
{ 
    // Create new instance of ImageViewer 
    imageViewer = new ImageViewer 
    { 
        Background = SystemColors.AppWorkspaceBrush, 
        UseDpi = true, 
        ViewPadding = new ControlPadding(), 
        ViewHorizontalAlignment = ControlAlignment.Center, 
        ViewVerticalAlignment = ControlAlignment.Center, 
    }; 
    // Add viewer to the imageViewerGrid 
    imageViewerGrid.Children.Add(imageViewer); 
    // Add pan and zoom functionality 
    var panZoom = new ImageViewerPanZoomInteractiveMode(); 
    imageViewer.InteractiveModes.Add(panZoom); 
    // Instantiate RasterCodecs 
    codecs = new RasterCodecs(); 
} 

private void _fileOpen_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog dlg = new OpenFileDialog(); 
        dlg.InitialDirectory = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
        if (dlg.ShowDialog() == true) 
        { 
            imageViewer.Image = codecs.Load(dlg.FileName); 
        } 
}

Try it out!

To test this for yourself, make sure to get the latest LEADTOOLS SDK evaluation for free from our site, if you have not already. This trial is good for 60 days and comes with unlimited chat and email support.

Support

Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.


Stay tuned because, as promised in our previous post, "Convert Images to Searchable PDF with OCR ", we'll be featuring a lot more tutorials that programmers can use to develop applications that directly impact data capture, recognition, exchange, and other pressing business needs.

LEADTOOLS Blog

LEADTOOLS Powered by Apryse,the Market Leading PDF SDK,All Rights Reserved