How to display an image in Silverlight? - silverlight

This should be simple, but...
I created a folder in my solution called Images. I dragged an image into it. How do I now display this image on a Page or View?

Make sure the image is set as a Resource. It can be in any folder in any of your projects in your solution.
You can then reference this as [assembly];component/[path]/[imagename.extension]
For example:
<Image Source="/mynamespace.myassembly;component/ResourcesFolder/image.png" Width="16" Height="16" />

There are a couple of ways to get at it--here's the way that involves setting the image as a Resource in the Visual Studio file properties:
using (var stream = Application.GetResourceStream(
new Uri("SilverlightAssemblyName;component/Images/myImage.png",
UriKind.Relative)))
{
// read from stream
}
Where SilverlightAssemblyName is replaced by the Assembly Name you specified in the Silverlight tab of your Silverlight project.
If you want to use the image in code:
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
myImageControl.ImageSource = bitmap;
Or, if you want to use the resource in XAML, you don't need any of the code:
<Image Source="/Images/myImage.png" Width="16" Height="16" />

Related

Access icon resources through URI VB.NET

I have a WPF VB.NET application and I want to use an icon embedded in the applications resources as a menu icon. So far I have this code (in the window's initialized event):
MenuItem.Icon = New Image() With {.Source = New BitmapImage(New
Uri("Resources\Icon.ico", UriKind.Relative))}
And the icon is still not displayed, any ideas?
The problem is your URI. If you set it in code behind, you must write the full WPF Pack URI. You must also set the Build Action
of the icon file to Resource (the default value for icons is None).
MenuItem.Icon = New Image() With
{
.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/Icon.ico"))
}
When you specify the URI in XAML, the default ImageSource TypeConverter will add the pack://application:,,, part, and you could simply write
<Image Source="/Resources/Icon.ico"/>
Better option is building menu in XAML:
Create folder Images in your solution
Add image as Resources to Images directory (in my sample code: "Icon.ico")
In XAML you can use following code:
...
<MenuItem Header="Item1">
<MenuItem.Icon>
<Image Source="/Images/Icon.ico" Width="20" Height="20" />
</MenuItem.Icon>
</MenuItem>
Or if you want to do this in code-behind you can use following code instead of step 3:
MenuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("/Images/Icon.ico", UriKind.RelativeOrAbsolute))}

Loading external images in WPF

I am building an application using WPF for Windows 8 in VB.NET and I need to load several images from the disk.
I don't want to add the images as a reference to the project, I just want to load them as is.
I already studied the "Imaging Overview" at MSDN and several other articles over the internet but nothing solved my problem.
I used both XAML and code but nothing worked. I can't get the image presented.
Dim myimage As New Image
myimage.Width = 200
Dim bitmapimage As New BitmapImage()
bitmapimage.UriSource = New Uri("C:\Users\MyName\Documents\Database\image.jpg")
myimage.Source = bitmapimage
XAML code:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660">
<Image.Source>
<BitmapImage UriSource="C:\Users\MyName\Documents\Database\image.jpg"/>
</Image.Source>
</Image>
</Grid>
When I load a referenced image (ie. Assets/Logo.png) it loads it correctly.
I also added all the necessary capabilities and declerations in the application manifest.
Your XAML looks correct. Maybe the image path is invalid. However it could be written simpler like this:
<Image HorizontalAlignment="Left" Height="265" Margin="191,145,0,0" VerticalAlignment="Top" Width="660"
Source="C:\Users\MyName\Documents\Database\image.jpg" />
The path string is automatically converted to an ImageSource instance.
In code you should use the BitmapImage constructor with Uri parameter. Otherwise you would have to call BeginInit and EndInit.
Dim uri As New Uri("C:\Users\MyName\Documents\Database\image.jpg")
Dim bitmapimage As New BitmapImage(uri)
myimage.Source = bitmapimage

Load image from file to dynamic resource programmatically

I need to load an image file to WPF's dynamic resource programmatically because the directory which contains the image files can be moved.
How can I load an image to WPF's dynamic resource which is used like this: Source="{DynamicResource ...}" in XAML?
In XAML:
<Image Source="{DynamicResource MyDynamicImage}" />
In code:
var myimg = new BitmapImage(new Uri("SomeUriHere"));
Resources["MyDynamicImage"] = myimg;
(The Resources collection should be one which is in the scope of the Image of course, if you have a direct reference to the Image control you can also use the immediate Image.Resources)
Whenever you set a new object to that resource key the DynamicResource will update.

Why does WPF make working with images so much more difficult?

I used to be able to do so very much with just the Bitmap and Graphics objects. Now that I've been using WPF the only thing I seem to be able to do is load an image and show it and make it dance around the stupid screen. Why did they get rid of these very useful tools. Are they trying to stupefy .Net?
All I want to do is load an image from a file and cut it into two parts. This was easy with .Net 2.0 and System.Drawing. But with WPF, I'm hitting a brick wall without using some very low level code. I've tried working with WriteableBitmap. But it doesn't seem to really be what I'm wanting. Is there no way to wrap a DrawingContext around a BitmapImage or something?
Please tell me that WPF is more than HTML for applications. I am REALLY frustrated!!
Edits:
Also, how on earth does one save an image to a file?
If you want to cut the image in two parts, why not use the CroppedBitmap class?
Consider the following XAML. One source BitmapImage shared by two CroppedBitmaps, each showing different parts of the source.
<Window.Resources>
<BitmapImage x:Key="bmp" UriSource="SomeBitmap.jpg" />
</Window.Resources>
<StackPanel>
<Image>
<Image.Source>
<CroppedBitmap Source="{StaticResource ResourceKey=bmp}">
<CroppedBitmap.SourceRect>
<Int32Rect X="0" Y="0" Width="100" Height="100" />
</CroppedBitmap.SourceRect>
</CroppedBitmap>
</Image.Source>
</Image>
<Image>
<Image.Source>
<CroppedBitmap Source="{StaticResource ResourceKey=bmp}">
<CroppedBitmap.SourceRect>
<Int32Rect X="100" Y="150" Width="50" Height="50" />
</CroppedBitmap.SourceRect>
</CroppedBitmap>
</Image.Source>
</Image>
</StackPanel>
Update: to do something similar in code:
var bitmapImage = new BitmapImage(new Uri(...));
var sourceRect = new Int32Rect(10, 10, 50, 50);
var croppedBitmap = new CroppedBitmap(bitmapImage, sourceRect);
Well there is this http://www.nerdparadise.com/tech/csharp/wpfimageediting/
or perhaps you could add a reference to System.Drawing to your project and then do the editing the way you are comfortable with.
You are probably best off using TransformedBitmap. Load your Bitmap as a BitmapSource, then set the Transform property to however you want the image to transform. You have several different transformation options here. This allows you to rotate, screw, matrix, etc. transformations. If you want to apply more than one, use a TransformGroup and apply several transformations at once.
You can also use BitmapFrame.Create(...) to work with the transformed image more.
Some Pseudo code:
var image = new BitmapSource(...); //Your image
var transformBitmap = new TransformedBitmap(image);
var transformBitmap.Transform = ..//Set your transform;
//optionally:
var frame = BitmapFrame.Create(transformBitmap);

Cannot find the image by providing the relative path in Silverlight

I'm working on a Silverlight application and I have a hard time setting the relative path of an image in my application.
This is a brief picture of my project directory:
MyProject
L images
L mountain1.jpg
L SpriteObjects
L MountainFactory.cs
L GameScreen.xaml
Originally, I painted an Rectangle using an image brush from the GameScreen.xaml.
<Rectangle Name="rec_bg" HorizontalAlignment="Left" VerticalAlignment="Top" Width="800" Height="600">
<Rectangle.Fill>
<ImageBrush x:Name="ib_bg" ImageSource="./images/mountain1.jpg" ></ImageBrush>
</Rectangle.Fill>
</Rectangle>
This xaml code works, that is it can find the image in the images folder without any problem.
I want to change the image dynamically, and I created a class called MountainFactory.cs. In this class, I have a method with this code:
ImageBrush brush = new ImageBrush();
BitmapImage image = new BitmapImage(new Uri("./images/mountain" + level + ".jpg", UriKind.Relative));
brush.ImageSource = image;
This method will return an image brush which is applied to the rec_bg Rectangle object.
However, this code cannot find the specified image.
Does anyone who know how to fix this?
Thanks.
It depends on how you are embedded the image. I'm assuming you have it set as "Content" so it's not embedded in the DLL but does embed in the XAP. In that case, you simply access it relative to the project root, so you would be using:
"images/mountain..."
(Note, you don't use the ./ in front of it, you are automatically relative to the root of the application). If you have it set as an embedded resource, you'll need to extract it out of the DLL using a stream - I don't recommend that.
Short story boring: try removing the leading ./ and make sure the properties of the Image have it as Content.

Resources