473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how do you load a bitmapimage in wpf?

xarzu
88 64KB
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
1 19257
Frinavale
9,735 Expert Mod 8TB
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

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

Similar topics

6
by: JS | last post by:
EE instance DB2 v7.2.0 fixpack 3 on WIN2K. I select some data from a table A and write it to a file using the COALESCE function and whitespace as the null character: eg coalesce(col1, ' '). This...
2
by: **Developer** | last post by:
I have a Form (FV&C) containing a userconrtrol (CF&E) I do a ShowDialog for the form and the form's Load calls a method of the UserControl. The first time I do this the usercontrol appears on...
3
by: db2udbgirl | last post by:
Env: DB2 UDB 8.2, AIX 5.3 While trying to load data (73 Million rows, Medium size table uses 4K tablespace) into a table using cursor it fails with "SQL0964C The transaction log for the database...
1
by: dbagirltx | last post by:
We have done some testing with mixed and forgotten results. So I'm hoping that asking here can clarify some issues for us. Right now we do one weekly warm backup. Throughout the week there are...
1
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the...
2
by: contractsup | last post by:
Environment: $ uname -a AIX <withheld2 5 000100614C00 $ db2level DB21085I Instance "<withheld>" uses "32" bits and DB2 code release "SQL08024" with level identifier "03050106"....
5
by: danfan46 | last post by:
Hi. I'm on DB2 9.5.0.1 on Linux AMD 64 and the database has four partitions where catalog is on partion 0 and user data on 1 thru 3. After a load failure (path to load file was wrong) I...
2
by: David Thielen | last post by:
So we have moved our app from .NET version 2.X in IIS6 to a Windows 2008 Server running IIS7. We have copied all files to the Windwardreports\apps directory and that apps directory has been...
13
by: rdudejr | last post by:
Hi all, I hardly ever make a post unless I am having a very purplexing issue, so this one should be good... I am trying to do a load against a database on an AIX server into a DB2 v9.1...
8
by: martinsmith160 | last post by:
Hi everyone I am trying to create a simple wpf program that allows a user to select an image from a combo box and then where they click on th screen draw that image at the mouse co-ordinates. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.