URI path in WPF window background image - wpf

I'm trying to assign a background to my WPF window.
I have a .jpg in bin\debug\StoredData\wallpaper.jpg
(I want to obtain the .jpg from there).
I came to putting this code inside the .cs file (newly created file):
InitializeComponent();
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri("\\StoredData\\login_wallpaper.jpg", UriKind.Absolute));
this.Background = myBrush;
But I get a "Invalid URI: The format of the URI could not be determined" message.
What should I change?

\StoredData\login_wallpaper.jpg is a relative URI. You can either change the UriKind to UriKind.Relative, or enter an absolute URI, depending on what behavior you want.

Related

What iis wrong with this imagesource in WPF?

I try to set an imagesource in code behind in WPF. For some reason it doesn't work - am I missing something?
ImageBrush imgBrushBackground = new ImageBrush();
imgBrushBackground.ImageSource = new BitmapImage(new Uri(#"\Assets\img\bg_spaceFlare.jpg"));
The link to the file is correct (I checked it by dragging in an image from Solution Explorer).
The error message is this: System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'
Thanks!
Petter
You can fix this by passing UriKind.Relative to Uri's constructor:
new BitmapImage(new Uri(#"\Assets\img\bg_spaceFlare.jpg", UriKind.Relative));

change canvas image on button click in wpf

On button click I want to change the canvas image(background image) to another image which is in a folder(images is the folder name). This is what I have tried:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
drawingCanvas.Children.Clear();
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage((new Uri(#"../Images/canvas.png", UriKind.Relative)));
drawingCanvas.Background = imageBrush;
}
First I'm clearing the contents of the canvas, deleting the previous image. Now want to set the background to another image. error: URI not handled exception. Any solutions will be appreciated. Thanks.
An image resource is loaded by a Resource File Pack URI:
imageBrush.ImageSource = new BitmapImage(
new Uri("pack://application:,,,/Images/canvas.png"));
In addition, the Build Actionof the image file has to be set to Resource, as shown in this answer.

How can I set an Image to an Image control in WPF?

I'm trying to do this:
imgUser.Source = new Uri(user.Photo);
But I'm getting an error that I cannot convert a URI to an ImageSource.
BitmapImage is an ImageSource and can be created from a Uri:
imgUser.Source = new BitmapImage(new Uri(user.Photo));

How to set uri for local image in silverlight app?

In SL class library MyLib, I have a image say my.png. Then I want to use it in code behind I tried following way:
StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("/MyLib;component/my.png", UriKind.Relative));
BitmapImage image = new BitmapImage();
image.SetSource(resourceStream.Stream);
this.MyIcon.Source = image;
But it's not woking. I think it's the Uri not set correctly. Help please.
This works:-
BitmapImage image = new BitmapImage(new Uri("/MyLib;component/my.png", UriKind.Relative));
MyIcon.Source = image;
I can't see why you would want to use a Stream here. Having said that your Stream code should work. The build action on the png should be "Resource" and "MyLib" in your Uri should be the Assembly name of the library as found on the "Silverlight" tab of the project properties.
Do you have your image marked as "Resource" in the properties window, or "Content"?
You could always set a style as a resource in your application and then call it like:
Application.Current.Resources["myCoolStyle"] and apply that to the image.

Deleting a window's background image WPF

I'm having a problem in WPF where a window doesn't release it's file-lock on the background image file after closing, before another part of the application tries to write to the image.
So as an example; say I have a WPF app consisting of 3 windows, 1 "menu" selection window and 2 others. Both of the windows create an ImageBrush using a BitmapImage as the ImageSource (the same image).
Window A has a button that when pressed, cycles through the available background images by copying them each over the file used as the original ImageSource and creating a new ImageBrush and setting the Window.Background to the new brush.
Window B simply uses the ImageBrush to draw the Window.Background.
If Window A is launched, backgrounds switched, closed and then Window B launched, everything is fine.
If Window B is launched, closed, then Window A is launched and backgrounds switched it crashes. Trying to switch the backgrounds throws an IOException because:
"The process cannot access the file 'C:\Backgrounds\Background.png' because it is being used by another process."
So Window B must still be holding onto it somehow!? I have tried doing a GC.Collect(); GC.WaitForPendingFinalizers(); to see if that cures the problem but it doesn't.
The answer Thomas gave is correct, and works well if you have a file path, don't want to cache the bitmap, and don't want to use XAML.
However it should also be mentioned that BitmapImage has a built-in way to load the bitmap immediately by setting BitmapCacheOption:
BitmapImage img = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
img.BeginInit();
img.UriSource = imageUrl;
img.EndInit();
or
<BitmapImage CacheOption="OnLoad" UriSource="..." />
This will load the bitmap immediately and explicitly close the stream, just as using a FileStream would, but with several differences:
It will work with any Uri, such as a pack:// Uri.
It can be used directly from XAML
The bitmap is cached in the bitmap cache, so future use of the same Uri won't go to the disk. In your particular application this may be a bad thing, but for other uses it may be a desirable feature.
I assume you're loading the image directly from the file, like that ?
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = imageUrl;
img.EndInit();
Try to load it from a stream instead ; that way you can close the stream yourself after the image is loaded, so that the file isn't locked :
BitmapImage img = new BitmapImage();
using (FileStream fs = File.OpenRead(imageFilePath)
{
img.BeginInit();
img.StreamSource = fs;
img.EndInit();
}

Resources