Load Image at runtime from resource in WPF - wpf

Im trying to load a image at runtime in WPF using the following code
_image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(#"pack://application:,,,/images/tagimages/placeholder.png", UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
_image.Source = src;
_image.Stretch = Stretch.None;
In my project I have a folder called images and a sub folder of that folder called tagimages that contains the placeholder.png. When I run this code I get the following error
"Cannot locate resource 'images/tagimages/placeholder.png'"
What am I doing wrong?

It turns out that I should have used
Uri(#"pack://application:,,,/<MyProject>;component/images/tagimages/placeholder.png", UriKind.Absolute);

From procedural code you use: #"pack://application:,,,/putyourfilenamehere" for an embedded resource.
Or in other words
BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/Images/myimage.png"));

Related

WPF Image Caching

I have a WPF application that takes a snapshot image from a video file. The user can define the timestamp from which to take the image. The image is then saved to a temporary location on disk, and is then rendered into an <image> element.
The user should then be able to select a different timestamp, which then overwrites the temporary file on disk - this should then be displayed within the <image> element.
Using Image.Source = null;, I can clear the image file from the <image> element, so it displays a blank space instead. However, if the source image file is then overwritten with a new image (with the same name) and loaded into the <image> element, it still shows the old image.
I am using the following logic:
// Overwrite temporary file file here
// Clear out the reference to the temporary image
Image_Preview.Source = null;
// Load in new image (same source file name)
Image = new BitmapImage();
Image.BeginInit();
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.UriSource = new Uri(file);
Image.EndInit();
Image_Preview.Source = Image;
The image displayed in the <image> element does not change, even though the original file has been completely replaced. Is there an image caching issue here that I am not aware of?
By default, WPF caches BitmapImages that are loaded from URIs.
You can avoid that by setting the BitmapCreateOptions.IgnoreImageCache flag:
var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(file);
image.EndInit();
Image_Preview.Source = image;
Or you load the BitmapImage directly from a Stream:
var image = new BitmapImage();
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
Image_Preview.Source = image;

No image displayed when setting Image.Source to valid data

I'm receiving byte[] image-data (from a webcam source, transmitted over network) and want to display it in my WPF Image control.
If I set the resolution to 160x120, I see a nice tiny image being displayed.
For any other resolution though I see nada, nothing, zilch, squat.
If I write the raw bytes to disk I can see that the data is indeed a valid jpg-image.
Just to make sure it's not jpeg-incompatibility I've tested it with png- and bmp-encoding/decoding as well with the same results; still no image.
Anyone got a bright idea ?
private bool OnImage(byte[] inJpg)
{
this.Dispatch(() =>
{
//File.WriteAllBytes("h:\\tmp\\test" + sImageNum++ + ".jpg", inJpg);
using (MemoryStream ms = new MemoryStream(inJpg))
{
BitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.Default);
ImageRemoteVideo.Source = decoder.Frames[0];
}
}
);
}
Assuming ImageRemoteVideo is an instance of a WPF Image control, you may be getting bit by some lazy resolve issues. The Image control has "BeginInit" and "EndInit" methods that you should probably be using. Put the BeginInit method before you set the stream, the other after you set it. Also, if I remember right, the Image class supports a StreamSource. You could try setting your MemoryStream (without disposing of it and without using the JpegBitmapDecoder) to that Property. If that works, then see if you can dispose it after the EndInit call.
I have just found a way to make it work, but I'd love it if someone can explain WHY it works :)
First off, I'm using the raw bytestream to create a BitmapImage.
BUT - if I use a "using" clause around the MemoryStream it doesn't work, if I don't it works.
So this doesn't work :
using (MemoryStream byteStream = new MemoryStream(inJpg))
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = byteStream;
image.EndInit();
ImageRemoteVideo.Source = image;
}
But this does :
MemoryStream byteStream = new MemoryStream(inJpg);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = byteStream;
image.EndInit();
ImageRemoteVideo.Source = image;
Found it. It's like Brannon said a case of delayed loading.
Specifying "CacheOption = BitmapCacheOption.OnLoad;" fixes it !
Cheers !
Working version :
MemoryStream byteStream = new MemoryStream(inJpg);
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = byteStream;
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
ImageRemoteVideo.BeginInit();
ImageRemoteVideo.Source = image;
ImageRemoteVideo.EndInit();
}

Problems displaying an image in Silverlight

I am trying to generate an image in silverlight but for some reason it is not showing.
Here is the code that I am using
Image fireBall = new Image();
Uri imageUri = new Uri("/Yambushi;Images/Projectiles/Fireball.png", UriKind.Relative);
fireBall.Source = new BitmapImage(imageUri);
cnvGame.Children.Add(fireBall);
fireBall.SetValue(Canvas.LeftProperty, Convert.ToDouble(100)); //(Doll left property)
fireBall.SetValue(Canvas.TopProperty, Convert.ToDouble(100));
fireBall.Height = 30;
fireBall.Width = 30;
Yambushi being the solution name and cnvGame the canvas I want to display the image in. The png file build action is set to content
Ok solved the problem...after I debugged the sourceUri of an image i've implemented through xaml and the source had to be as follows:
Uri imageUri = new Uri("/Images/Projectiles/Fireball.png", UriKind.Relative);
fireBall.Source = new BitmapImage(imageUri);

Dynamic loading of images in WPF

I have a strange issue with WPF, I was loading images from the disk at runtime and adding them to a StackView container. However, the images were not displayed. After some debugging I found the trick, but it really doesn't make any sense. I've made a small demo app to identify the problem:
Create a new WPF project, and paste code as follows:
xaml:
<Window x:Class="wpfBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<StackPanel Name="sp">
</StackPanel>
</Window>
xaml.cs, paste below default usings:
namespace wpfBug
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("picture.jpg", UriKind.Relative);
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
sp.Children.Add(i);
}
}
}
Copy a image to the bin/Debug folder and call it 'picture.jpg'
This program doesn't display anything, unless the commented line is uncommented.
Can anyone explain what I'm doing wrong, or why this happens? If you remove the image and run the program it generates an exception on the 'int q= ...' line. If that line is commented the program runs without exceptions even if no image is present. Loading an image only if nessesary makes sense, but then the image should be loaded when I add the Image control to the StackPanel.
Any ides ?
Edit: By the way, if you add the image as a resource, the 'int q = ..' line is not needed.
It is because the Creation was delayed.
If you want the picture to be loaded immediately, you can simply add this code into the init phase.
src.CacheOption = BitmapCacheOption.OnLoad;
like this:
src.BeginInit();
src.UriSource = new Uri("picture.jpg", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
In code to load resource in the executing assembly where my image 'Freq.png' was in the folder "Icons" and defined as "Resource".
this.Icon = new BitmapImage(new Uri(#"pack://application:,,,/"
+ Assembly.GetExecutingAssembly().GetName().Name
+ ";component/"
+ "Icons/Freq.png", UriKind.Absolute));
I also made a function if anybody would like it...
/// <summary>
/// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'.
/// </summary>
/// <param name="pathInApplication">Path without starting slash</param>
/// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>
/// <returns></returns>
public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null)
{
if (assembly == null)
{
assembly = Assembly.GetCallingAssembly();
}
if (pathInApplication[0] == '/')
{
pathInApplication = pathInApplication.Substring(1);
}
return new BitmapImage(new Uri(#"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute));
}
Usage:
this.Icon = ResourceHelper.LoadBitmapFromResource("Icons/Freq.png");
This is strange behavior and although I am unable to say why this is occurring, I can recommend some options.
First, an observation. If you include the image as Content in VS and copy it to the output directory, your code works. If the image is marked as None in VS and you copy it over, it doesn't work.
Solution 1: FileStream
The BitmapImage object accepts a UriSource or StreamSource as a parameter. Let's use StreamSource instead.
FileStream stream = new FileStream("picture.png", FileMode.Open, FileAccess.Read);
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.StreamSource = stream;
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
panel.Children.Add(i);
The problem: stream stays open. If you close it at the end of this method, the image will not show up. This means that the file stays write-locked on the system.
Solution 2: MemoryStream
This is basically solution 1 but you read the file into a memory stream and pass that memory stream as the argument.
MemoryStream ms = new MemoryStream();
FileStream stream = new FileStream("picture.png", FileMode.Open, FileAccess.Read);
ms.SetLength(stream.Length);
stream.Read(ms.GetBuffer(), 0, (int)stream.Length);
ms.Flush();
stream.Close();
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.StreamSource = ms;
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
panel.Children.Add(i);
Now you are able to modify the file on the system, if that is something you require.
You could try attaching handlers to various events of BitmapImage:
DecodeFailed
DownloadFailed
DownloadCompleted
They might tell you a little about what's going on, as far as the image is concerned.
Here is the extension method to load an image from URI:
public static BitmapImage GetBitmapImage(
this Uri imageAbsolutePath,
BitmapCacheOption bitmapCacheOption = BitmapCacheOption.Default)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = bitmapCacheOption;
image.UriSource = imageAbsolutePath;
image.EndInit();
return image;
}
Sample of use:
Uri _imageUri = new Uri(imageAbsolutePath);
ImageXamlElement.Source = _imageUri.GetBitmapImage(BitmapCacheOption.OnLoad);
Simple as that!

How to render an <image> in a background WPF process?

I'm taking Silverlight XAML and sending it to a web service that renders the XAML in a STA thread and generates a PNG image. All the XAML renders correctly except the <image> entries which 'appear' to load correctly when their Source property is set in WPF but the PNG does not show the referenced image - what am I doing wrong ?
The core of the code that I am using is as below. The value object is a DTO from Silverlight that contains the XAML in the string that ends up as the sXAML local property and the Image URI is in the value.ImageURL property.
var canvas = (FrameworkElement)XamlReader.Load(new XmlTextReader(new StringReader(sXAML)));
var obj = canvas.FindName("BGImage");
Image img = null;
if (obj != null)
{
img = obj as Image;
img.ImageFailed += img_ImageFailed;
img.Source = new BitmapImage(new Uri(value.ImageURL, UriKind.Absolute));
}
canvas.Arrange(new Rect(new Size(463d, 381d)));
canvas.UpdateLayout();
var mem = new MemoryStream();
var bmp = new RenderTargetBitmap(463, 381, 96d, 96d, PixelFormats.Pbgra32);
bmp.Render(canvas);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(mem);
FileStream fs = new FileStream("D:\\out.png", FileMode.Create);
mem.WriteTo(fs);
fs.Close();
NB: The img_ImageFailed event handler is never invoked indicating that the img Source assignment was successful in some way.
Things I would try:
1) In your WPF app, if you 'render' your dynamically loaded Canvas to display in a Window, does it all work (images included)?
2) Have you tried attaching a Loaded handler to the img object to see when the image is actually loaded?
3) Assuming #1 works - where are the images located (are they on the internet/local web server)? If you put a breakpoint in the code on
bmp.Render(canvas);
and wait a while before stepping on - does the image then appear in the rendered output?
I suspect the image is being 'downloaded' asynchronously and you are rendering the Canvas too early, before the Image object has resolved its source.
[UPDATE 29-Jan-09]
possible solution
I copied you code down exactly and gave it a try. When I used a 'local' image location (eg. "c:\images\splash.jpg") the image was rendered fine in the output jpeg. When I used a URL (eg. "http://localhost/images/splash.jpg") it did NOT appear (as you describe).
So I modified your code as follows (to try and FORCE the image to be downloaded - will only work for http: image references) --
if (obj != null)
{
img = obj as Image;
// new stuff
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = getCachedURLStream(new Uri(ImageURL));
bi.EndInit();
img.BeginInit();
img.Source = bi;
img.EndInit();
//img.ImageFailed += img_ImageFailed;
//img.Loaded += new RoutedEventHandler(img_Loaded);
//img.Source = new BitmapImage(new Uri(ImageURL, UriKind.Absolute));
}
public static Stream getCachedURLStream(Uri url)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
WebResponse response;
webrequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
response = webrequest.GetResponse();
Stream s = response.GetResponseStream();
BufferedStream bs = new BufferedStream(s, 8192);
return bs;
}
(the getCachedURLStream method is from synchronous image loading - it's kinda extraneous but I just wanted a quick chunk of code to test) and it seemed to then work (image visible in JPEG). Maybe that will work in your scenario?

Resources