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.
Related
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.
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));
I ham developing an application for WP7 and i am having a strange problem with Image control, it does not show the image it is binded to.
I have tryed it in so many ways but event this way (the most naive one) does not work:
In the xaml I have an Image control named img and this is what I have done in the code behind
public MainPage()
{
InitializeComponent();
img.Source = new BitmapImage(new Uri (#"http://img11.imageshack.us/img11/5365/photovnj.jpg", UriKind.Absolute));
}
</code>
It seems to simple not to work...
Please help!
This code works fine for me presuming you have img declared as an Image in your xaml.
If not, I'd suggest you have a connection problem from your app running in the emulator or device to the internet.
This is the code I wrote.
image1.Source = new BitmapImage(new Uri(#"http://img11.imageshack.us/img11/5365/photovnj.jpg", UriKind.Absolute));
I have an image (embedded resource) that i can get access to and form an Image object from. I actually can get the Image object or the Stream of bits the represent the image. However I want to sue that image programaticly to be a background image.
So how do I set the ImageSource on the ImageBrush to an AcutalImage (PNG)?
I think the MSDN documentation says it all:
http://msdn.microsoft.com/en-us/library/system.windows.media.imagebrush.imagesource%28VS.95%29.aspx
You can either set the source as a URI in XAML, or use code behind to set it to an ImageSource object created from a stream or a Uri, e.g.
_imageBrush.ImageSource = new BitmapImage(new Uri("http://someurl.com/images/myimage.png"));
Cheers, Alex
EDIT: If your image is a ressource, you can use the ressource url syntax:
"/{AssemblyName};component/{RelativePath}"
For example:
<ImageBrush ImageSource="/MyApplication.Resources;component/Images/image1.png" />
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();
}