Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [SOLVED] WPF Not Displaying Bitmaps
New Posts  All Forums:Forum Nav:

[SOLVED] WPF Not Displaying Bitmaps

post #1 of 2
Thread Starter 
OK, so this seems to be an age-old problem on Windows and unfortunately I've run into it. I've tried every tactic I can find and yet no joy. The application has a couple of windows; images load and display perfectly well in the main window, but the very same images refuse to show in the other windows.

For the record I'm developing this application in C# on Windows 8.1 Pro, using VS2013.

For one of the windows the XAML is: Warning: Spoiler! (Click to show)
Code:
<Window x:Class="Example.ImageWindow" 
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                WindowStyle="None" FontSize="16">
</Window>

and the C# code is: Warning: Spoiler! (Click to show)
Code:
// load the bitmap
BitmapImage bannerBitmap = new BitmapImage();
bannerBitmap.BeginInit();
bannerBitmap.UriSource = new Uri(@"/Resources/amur-leopard.jpg", UriKind.RelativeOrAbsolute);
bannerBitmap.EndInit();
                        
// attach it to the Image object
Image bannerImage = new Image();
bannerImage.Source = bannerBitmap;
bannerImage.Visibility = Visibility.Visible;
                        
// create a StackPanel to place the Image object in
StackPanel imagePanel = new StackPanel();
imagePanel.Background = Brushes.White;
imagePanel.Children.Add(bannerImage);
                        
// create the root panel
StackPanel visual = new StackPanel();
visual.Background = Brushes.White;
visual.HorizontalAlignment      = HorizontalAlignment.Center;
visual.VerticalAlignment        = VerticalAlignment.Center;
visual.Orientation                      = Orientation.Vertical;
visual.Children.Add(imagePanel);
                        
this.Content = visual;

I have made sure that the image files are known to the project, are actually in the Resources directory and have their Build Action set to Resource. I've also tried cleaning the project several times, to no avail.

If anyone can offer any insight into this, it would be appreciated.

Many thanks. smile.gif
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
post #2 of 2
Thread Starter 
OK, so it's solved. I have to use the pack:// URI and some reflection to get it to work correctly.
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
Ryzen
(12 items)
 
  
CPUMotherboardGraphicsRAM
Ryzen 7 1700 Gigabyte GA-AB350M Gaming 3 Palit GT-430 Corsair Vengeance LPX CMK16GX4M2B3000C15 
Hard DriveCoolingOSMonitor
Samsung 850 EVO AMD Wraith Spire Linux Mint 18.x Dell UltraSharp U2414H 
KeyboardPowerCaseMouse
Apple Basic Keyboard Thermaltake ToughPower 850W Lian-Li PC-A04B Logitech Trackman Wheel 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [SOLVED] WPF Not Displaying Bitmaps