Access icon resources through URI VB.NET - wpf

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))}

Related

how can I set a background image in code?

If I want to set a image as background on a textBox I can use this code in the axml:
<Grid>
<Grid.Background>
<ImageBrush ImageSource="MyImage.jpg" />
</Grid.Background>
<TextBlock Text="Some Text" />
</Grid>
However, I am creating a TextBlock in code, I amtrying this:
TextBox myTextBox = new TextBox();
But in this way I don't know how to access to the ImageBrush property.
Which is the way to add a background in code?
Thank so much.
Provided that MyImage.jpg is a file in the application's current folder, you could write
myTextBox.Background = new ImageBrush(new BitmapImage(new Uri("MyImage.jpg")));
If it's a Resource File, you would have to use a Resource File Pack URI:
myTextBox.Background =
new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/MyImage.jpg")));

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

How to set ImageSource in a codebehindfile

I have in a UserControl the Property ImageSource. How can I set the ImageSource in my code behind to a Image in my Resources directory?
I want to bind the Image Source to the property ImageSource.
<Image Source="{Binding Path=ImageSource}" />
In order to create a BitmapImage (which is derived from ImageSource) from a resource file in code, you would do the following, provided that the file MyImage.jpg is in a folder named Images of your Visual Studio project, and that its Build Action is set to Resource:
var uri = new Uri("pack://application:,,,/Images/MyImage.jpg");
ImageSource = new BitmapImage(uri); // set the ImageSource property
See also this answer.
You could also directly use the image resource without binding:
<Image Source="/Images/MyImage.jpg" />

Using a DrawingImage as Icon for Multiple MenuItems

I have some icon resources as DrawingImages that is made up of many GeometryDrawings. I have File MenuItems and ToolBar buttons that use these images via resource bindings to MenuItem.Icon. Unfortunately, only one of the MenuItems show the icon.
I am sure you can't assign a single DrawingImage resource to many MenuItem.Icon (or anything else for that matter), but I don't know of an alternative. I would prefer not to duplicate the DrawingImage resource, but if I have too I guess I will.
You assign an Image control to the Icon Property and set the DrawingImage into the Image.Source property.
In XAML:
<MenuItem>
<MenuItem.Icon>
<Image Source="{StaticResource myDrawingImage}"/>
</MenuItem.Icon>
<!-- everyhting else -->
</MenuItem>
In C#:
menuItem.Icon = new Image() {Source = (ImageSource)Resources["myDrawingImage"]};

How to display an image in 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" />

Resources