By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,504 Members | 2,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,504 IT Pros & Developers. It's quick & easy.

how do you load a bitmapimage in wpf?

xarzu
P: 65
In a silverlight app, I have a BitmapImage defined as System.Windows.Media.Imaging.BitmapImage and it as a method called "SetSource" where I can set the source like this:
Expand|Select|Wrap|Line Numbers
  1.     BitmapImage bitmap = new BitmapImage(); 
  2.     System.IO.Stream stream = _scene.GetStream(); 
  3.     if (stream == null) return; 
  4.     bitmap.SetSource(stream); 
In a WPF application I have also have a Bitmap image defined as System.Windows.Media.Imaging.BitmapImage 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:

Expand|Select|Wrap|Line Numbers
  1.      System.IO.Stream stream = _scene.GetStream(); 
  2.         if (stream == null) return; 
  3.         BitmapImage bitmap = new BitmapImage(); 
  4.  
  5.         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
Aug 2 '12 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
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)
Expand|Select|Wrap|Line Numbers
  1. <StackPanel>
  2.   <Image x:Name="theImage" Height="500" Width="600"/>
  3.   <Button x:Name="LoadPhoto" Content="Load Photo" Click="LoadPhoto_Click"/>
  4. </StackPanel>
This would be the code in the button click event that loads the image and displays it:

(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Private Sub LoadPhoto_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
  2.         Dim dlg As New Microsoft.Win32.OpenFileDialog()
  3.         dlg.FileName = "Image"
  4.         dlg.DefaultExt = ".jpg"
  5.         dlg.Filter = "All images|*.jpg;*.jpeg;*.jpe;*.bmp;*.gif;*.ico;*.png;*.tif;*.tiff;*.hpd;*.jxr;*.wdp|" + _
  6.             "JPEG image|*.jpg;*.jpeg;*.jpe|Windows BMP image|*.bmp|GIF image|*.gif|Microsoft Windows icon|*.ico|" + _
  7.             "PNG image|*.png|TIFF image|*.tif;*.tiff|JPEG XR|*.hpd;*.jxr;*.wdp"
  8.         Dim photo As New BitmapImage()
  9.         If dlg.ShowDialog() Then
  10.             If dlg.FileNames.Count > 0 Then
  11.                 Using photoStream As System.IO.FileStream = New System.IO.FileStream(dlg.FileNames(0), IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
  12.                     photo.BeginInit()
  13.                     photo.StreamSource = photoStream
  14.                     photo.CacheOption = BitmapCacheOption.OnLoad
  15.                     photo.EndInit()
  16.                 End Using
  17.                 theImage.Source = photo
  18.                 theImage.Stretch = Stretch.Fill
  19.             End If
  20.         End If
  21.     End Sub
  22.  
(C#)
Expand|Select|Wrap|Line Numbers
  1. private void LoadPhoto_Click(System.Object sender, System.Windows.RoutedEventArgs e)
  2. {
  3.     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  4.     dlg.FileName = "Image";
  5.     dlg.DefaultExt = ".jpg";
  6.     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";
  7.     BitmapImage photo = new BitmapImage();
  8.     if (dlg.ShowDialog()) {
  9.         if (dlg.FileNames.Count > 0) {
  10.             using (System.IO.FileStream photoStream = new System.IO.FileStream(dlg.FileNames(0), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) {
  11.                 photo.BeginInit();
  12.                 photo.StreamSource = photoStream;
  13.                 photo.CacheOption = BitmapCacheOption.OnLoad;
  14.                 photo.EndInit();
  15.             }
  16.             theImage.Source = photo;
  17.             theImage.Stretch = Stretch.Fill;
  18.         }
  19.     }
  20. }
-Frinny
Aug 3 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.