1

I have 3 multi-resolution icon files (.ico). Each one contains sizes 16x16, 24x24, 32x32, 48x48, 64x64 and 96x96

The icon is being dynamically picked by using binding.

<Image Source="{Binding ImageSource}" />

and following is the code where imageSource is being set:

_imageSource = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

the column in which the image is being placed has Width="Auto"

I know that having a single .ico file helps Windows pick the best size. However, I am facing a problem that the icon size is different depending on which icon is being picked

e.g.: For one image 32x32 size is being picked and for the other 48x48 size is being picked.

Why? Is there a problem with the code or is it with the icons?

Please note that width is not being set anywhere in the code.

Another thing that is confusing me is that all examples I have seen for CreateBitmapSourceFromHIcon are passing Int32Rect.Empty as the second argument (i.e.: sourceRect). However, I haven't found some good explanation on how it affects the image size. Some explanation regarding this will be highly appreciated

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
Maria
  • 123
  • 8
  • 1
    Haven't you tried different values for sourceRect? Maybe you can force picking a certain resolution. – Clemens Apr 05 '18 at 12:22
  • 1
    the problem is that I don't want to force a certain resolution. It should be picked automatically. However, for a single image, same size should be picked (e.g.: in the case I mentioned, the size for both images for be same i.e.: either 32 or 48) because it is affecting the layout of remaining items – Maria Apr 05 '18 at 12:26
  • Understood, but apparently it doesn't work as you expect. So you could at least try something else. – Clemens Apr 05 '18 at 12:27
  • You could also try what IconBitmapDecoder gives you. You would also not have to deal with icon handles. – Clemens Apr 05 '18 at 12:30
  • `BitmapDecoder.Create(new Uri(...), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);` (where Uri could be a Resource File Pack Uri like `pack://application:,,,/Some.ico`) will create a BitmapDecoder. You can iterate over the BitmapFrames in its `Frames` property and check their sizes. Maybe that gives you a hint. – Clemens Apr 05 '18 at 12:41
  • It's my understanding that the different sized icons within an ico file can be in any order. I guess your approach picks the first one. There's no guarantee an ico file will have a specific size in it. The designer doing the ones for our game initially added one for every size, giving a massive file. – Andy Apr 05 '18 at 13:17
  • The code here looks like it might be what Clemens suggests https://stackoverflow.com/questions/952080/how-do-you-select-the-right-size-icon-from-a-multi-resolution-ico-file-in-wpf – Andy Apr 05 '18 at 13:18

0 Answers0