ARTICLE

WPF Image Viewer in VB.NET

Posted by Dinesh Beniwal Articles | WPF using VB.NET October 20, 2009
The attached project with this article is a simple Image Viewer that allows users to browse an image and display it in WPF and XAML.
Download Files:
 
Reader Level:

The Image Class


The Image class is used to load and view an image in WPF. This class displays .bmp, .gif, .ico, .jpg, .png, .wdp, and .tiff files. If a file is a multiframe image, only the first frame is displayed. The frame animation is not supported by this class.

 

Image Class with source property :


<Image Name="ImageViewer1" Source="Creek.jpg" Height="400" Width="400" />

In the above code snippet, UriKind let you set if the image is relative to the application path or has an absolute path.

 

I create a WPF application with two labels, a button, and an Image control. The XAML code looks like following:

 

<StackPanel Orientation="Horizontal" Background="LightBlue" Height="40">

    <Label Margin="10,0,0,0" Height="23" Name="Label1">

        Current File:

    </Label>

    <Label Margin="5,0,0,0" Height="25" Name="FileNameLabel" Width="300" />

    <Button Margin="5,0,0,0" Height="23" Name="BrowseButton" Width="75" Click="BrowseButton_Click">

        Browse

    </Button>               

    </StackPanel>

<StackPanel >

    <Image Name="ImageViewer1" Height="400" Width="400" />      

</StackPanel>

 

The application UI looks like Figure 1.

 

imageviewer1.gif
 

Figure 1.

 

The Browse button click event handler looks like following:

 

        Private Sub BrowseButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Dim dlg As New OpenFileDialog()
            dlg.InitialDirectory = "c:\"
            dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*"
            dlg.RestoreDirectory = True

            If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim selectedFileName As String = dlg.FileName
                FileNameLabel.Content = selectedFileName
                Dim bitmap As New BitmapImage()
                bitmap.BeginInit()
                bitmap.UriSource = New Uri(selectedFileName)
                bitmap.EndInit()
                ImageViewer1.Source = bitmap
            End If
        End Sub
 

Click on the Browse button let you browse the files and selected file is displayed in the Viewer. See Figure 2.
 

imageviewer2.gif
 

Figure 2.

 

Summary

This article showed how to use the Image control of WPF to load and view the image files. I will be working with this Image Viewer for a while and will build a full-fledged image viewer.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor