how do you load a bitmapimage in wpf?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xarzu
    New Member
    • Apr 2007
    • 90

    how do you load a bitmapimage in wpf?

    In a silverlight app, I have a BitmapImage defined as System.Windows. Media.Imaging.B itmapImage and it as a method called "SetSource" where I can set the source like this:
    Code:
    	BitmapImage bitmap = new BitmapImage(); 
    	System.IO.Stream stream = _scene.GetStream(); 
    	if (stream == null) return; 
    	bitmap.SetSource(stream);
    In a WPF application I have also have a Bitmap image defined as System.Windows. Media.Imaging.B itmapImage but there is no SetSource method. How do I set the source in a WPF app like I do in a Silverlight app?

    Also, it is a stream, not a string. It is not a URI. so "UriSource" method does not work. I tried this:

    Code:
     	System.IO.Stream stream = _scene.GetStream(); 
            if (stream == null) return; 
            BitmapImage bitmap = new BitmapImage(); 
     
            bitmap.UriSource = new Uri(stream.ToString());
    And at runtime, it threw an error tha URI cannot be determined. Is the URI an identifier for the intranet? Are you sure that this is not a silverlight thing? I am doing a WPF application
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    In WPF, you set the source for the image using a BitmapSource.

    For example, say I have the following image control and button that loads the image file that the user selects defined in my XAML:

    (XAML)
    Code:
    <StackPanel>
      <Image x:Name="theImage" Height="500" Width="600"/>
      <Button x:Name="LoadPhoto" Content="Load Photo" Click="LoadPhoto_Click"/>
    </StackPanel>
    This would be the code in the button click event that loads the image and displays it:

    (VB.NET)
    Code:
    Private Sub LoadPhoto_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim dlg As New Microsoft.Win32.OpenFileDialog()
            dlg.FileName = "Image"
            dlg.DefaultExt = ".jpg"
            dlg.Filter = "All images|*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.ico;*.png;*.tif;*.tiff;*.hpd;*.jxr;*.wdp|" + _
                "JPEG image|*.jpg;*.jpeg;*.jpe|Windows BMP image|*.bmp|GIF image|*.gif|Microsoft Windows icon|*.ico|" + _
                "PNG image|*.png|TIFF image|*.tif;*.tiff|JPEG XR|*.hpd;*.jxr;*.wdp"
            Dim photo As New BitmapImage()
            If dlg.ShowDialog() Then
                If dlg.FileNames.Count > 0 Then
                    Using photoStream As System.IO.FileStream = New System.IO.FileStream(dlg.FileNames(0), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
                        photo.BeginInit()
                        photo.StreamSource = photoStream
                        photo.CacheOption = BitmapCacheOption.OnLoad
                        photo.EndInit()
                    End Using
                    theImage.Source = photo
                    theImage.Stretch = Stretch.Fill
                End If
            End If
        End Sub
    (C#)
    Code:
    private void LoadPhoto_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
    	Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    	dlg.FileName = "Image";
    	dlg.DefaultExt = ".jpg";
    	dlg.Filter = "All images|*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.ico;*.png;*.tif;*.tiff;*.hpd;*.jxr;*.wdp|" + "JPEG image|*.jpg;*.jpeg;*.jpe|Windows BMP image|*.bmp|GIF image|*.gif|Microsoft Windows icon|*.ico|" + "PNG image|*.png|TIFF image|*.tif;*.tiff|JPEG XR|*.hpd;*.jxr;*.wdp";
    	BitmapImage photo = new BitmapImage();
    	if (dlg.ShowDialog()) {
    		if (dlg.FileNames.Count > 0) {
    			using (System.IO.FileStream photoStream = new System.IO.FileStream(dlg.FileNames(0), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) {
    				photo.BeginInit();
    				photo.StreamSource = photoStream;
    				photo.CacheOption = BitmapCacheOption.OnLoad;
    				photo.EndInit();
    			}
    			theImage.Source = photo;
    			theImage.Stretch = Stretch.Fill;
    		}
    	}
    }
    -Frinny
    Last edited by Frinavale; Aug 3 '12, 01:45 PM.

    Comment

    Working...