Images in .ico as DynamicResource - wpf

Situation looks like that:
I have many icons in application and they are used in few different sizes.
I use icons as DynamicResource for example like that:
<igRibbon:MenuTool (...) LargeImage="{DynamicResource IconAdd}" />
<s:Item (...) Icon="{DynamicResource IconAdd}"/>
Some of icons are in .xaml and some in .png format
I add new icons for example like that:
<BitmapImage x:Key="IconAdd" UriSource="../Icons/IconAdd.png" />
The problem:
I would like to have icons in .ico format that I can use as DynamicResource.
I want images in .ico because this file format allows to have few different image sizes in one file. Icons in .xaml are fully resizable but they took to much time to load (because I have really a lot of them!).
Is it possible to add .ico file as DynamicResource and add x:key to it?
Even if I somehow add those .ico images, will they change size (depending on how much place they have)?

Yes, it is possible to add .ico files as a DynamicResource (and of course, because it is a resource, it must have an x:Key).
They will not automatically change size, however. Each size is extractable from the .ico like this, in which I make an Image for each of the icon's frames set to the exact size of the frame, and then add that Image to a StackPanel called imageStack:
var iconUri = new URI( "pack://application:,,,/MyIcon.ico", UriKind.RelativeOrAbsolute );
var iconDecoder = new IconBitmapDecoder( iconUri,
BitmapCreationOptions.None, BitmapCacheOption.Default );
foreach ( var frame in iconDecoder.Frames ) {
var img = new Image(){
Height = frame.PixelHeight,
Width = frame.PixelWidth,
Source = frame }
imageStack.Children.Add( img );
}
When you just use the .ico directly, it will choose whichever frame has the largest pixel resolution and resize that bitmap based on the size of the Image control, or whatever content alignment/sizing properties are in place for your Image, Button, or other control that is displaying it.
A few options for controlling which frame is added that aren't complete, but may serve as ideas towards a solution:
Programmatically split apart the icon into BitmapFrames and add them to the ResourceDictionary with keys like 'MyIcon16', 'MyIcon32', and so on.
Create a MarkupExtension or IValueConverter to extract a frame matching certain criteria such as index or size.
Since you are using DynamicResource, you can change which frame is associated with a particular resource key at any time.
You could control it by scope. You could have the 32x32 frame as a resource in the Window's ResourceDictionary with the key 'MyIcon', and have the 64x64 frame as a resource with the same key in a different scope, such as a Grid within that Window. Anything using {DynamicResource MyIcon} in the Grid will show the 64x64 frame, while everything else in the window shows the 32x32 frame.

Related

UserControl to display 16x16 or 32x32 images

I have a control that assembles a final image from multiple PNG files. The individual png files are chosen based on item states bound to the control. i.e. overlayed - but that all works fine.
Each image file is created in two sizes 16x16 and 32x32 (i.e. like an icon)
From the VS designer (or code) I want the user to be able to specify whether they want the 16x16 or 32x32 version. So I added a dependency property ImgSize. I've made it an int so I can set it to 16 or 32. So a user of this control simply XAMLs
<xyz:thisControl ImgSize="32"/>
and the DataContext supplies all the binding particulars.
I want the width and height of the user control to be automatically set to the corresponding image size. So binding like so makes sense:
<UserControl x:Class="...
Width="{Binding Path=ImgSize}"
Height="{Binding Path=ImgSize}"
When that didn't work, I tried every RelativeSource binding I could - no luck. Is there an additional layer of plumbing/binding/events I'm missing?
Thanks,
Dan
Why don't you just use the built-in width and height of the UserControl?
These properties are bindable by default

Is there any way to assign multiple images source at one time

Is there any way in silverlight to assign multiple images at time to selecting single image and set it's source it will assign to all the images in the wrappanel.
Thanks...!!
Maybe something like this?
foreach(Image image in imageWrappanel.Children) {
image.Source = selectedImage.source;
}
Assuming you have a wrappanel named imageWrappanel and the selected Image is called selectedImage.

WPF : Use different icon images as an ImageBrush?

An Icon can contain more than one image. I want to use an icon for an ImageBrush and I want to set which image inside the icon should be use. But somehow it seems I can't do that. The brush always picks the largest image from the icon.
Is there a way to choose the image manually?

WPF/C#: Images rotating from a listview?

I just want ask for your comments/suggestions on how to create a customized listview (if that's a good implementation) in WPF that displays images coming from a table from a database (more like a playlist) that rotates similar to a film (moving horizontally - on loop)
Any ideas?
If you have a list of Images, you can create an Image control for each one, put each Image control in a horizontal StackPanel, put the StackPanel inside a Canvas (of whatever size of the "film"), and animate the Left property of the Canvas to have the images roll.
Of course, if you need that the images wrap (the first one after the last one), you could forget about the StackPanel and move each Image separately.

How Can I Get Information About the Source Image of a WPF `<Image>` Element?

How can I get information about the source image of a WPF <Image> element?
My <Image> element has its Source bound to an ImageSource property, which changes frequently. In my code behind, I need to be able to access the actual width of the current source image file (in regular pixels), for mathematical purposes. My application will perform image operations on the image file, so this information is necessary.
Any help is appreciated.
I think this may work for you:
BitmapSource sourceData = image.Source as BitmapSource;
int width = sourceData.PixelWidth;
int height = sourceData.PixelHeight;

Resources