11

In an application I'm working on there are Images (part of System.Windows.Media.Imaging) being created dynamically in the code-behind of a XAML page and getting added to the window. I'm using ICO files, which contain various sizes and bit depths. The size I want is 96x96 @ 32 bit. The code automatically selects the largest size (256x256 @ 32 bit).

I'm setting the Image.Source property as follows:

image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/Application.ico"));

Now, by searching around I've found the Icon (part of System.Drawing) which will allow me to set a string path and size, but this is a part of an entirely different library.

new Icon("pack://application:,,,/Resources/Images/Inquiry.ico",96,96);

Any idea how I can make my Image's size property a bit more manageable? Thank you!

EDIT: Convert System.Drawing.Icon to System.Media.ImageSource
This post gets the image to appear smaller, however, it doesn't matter if I set the Icon to 96x96 or 128x128, it doesn't actually change the displayed image's size. I'm assuming it's going to some default value that is different that the System.Windows.Media.Imaging default.

EDIT to the EDIT: Weird keeps getting weirder. When I display through the original method, it's definitely displaying the image as 256x256. However, when I use the conversion method described in the link, while the sizes change, they are significantly scaled down (i.e. 256x256 looks closer to 48x48).

Community
  • 1
  • 1
Wally Young
  • 198
  • 1
  • 1
  • 10
  • 1
    not sure, if i got the question, if you want to set a fixed size for the image, why not using Image.Height and Image.Width? Else there are a lot of tuturials about reszing like http://rongchaua.net/blog/c-wpf-fast-image-resize/ – Jan K. Oct 13 '11 at 12:36
  • ICOs are such a pain. It really doesn't look like Microsoft considers them first-class objects anymore... Anyways, system.drawing.icon.tobitmap could be used to resize an icon. Can you use the BitmapImage.DecodePixelWidth property inside a BitmapImage.BeginInit - EndInit block? – djdanlib Dec 12 '11 at 18:39
  • I read another post where the solution was to create the ICO a specific size but I can't find it now. Are you using this as the icon for an applicaiton? – paparazzo Dec 19 '11 at 21:39
  • Blam: I was using it as an icon within the program. Sort of a pseudo-tablet-esque interface. It seemed to be touch-and-go for whatever size the actual system used, and then re-sized itself. Just ended up biting the bullet and made vectors instead. – Wally Young Mar 14 '12 at 14:33

3 Answers3

1

Try the markup extension that is posted in this answer to another SO question. Note the use of BitmapDecoder to get the required frame from the Ico file.

Community
  • 1
  • 1
Olly
  • 5,966
  • 31
  • 60
  • It looks like this might help me out, but unfortunately this was with my last job, and I do not have the source, so I don't have the solution to test it against. – Wally Young Aug 03 '12 at 07:08
0

You could try with the following code, it should work for ICO files:

Image displayImage = new Image();
// Create the source
BitmapImage sourceImage = new BitmapImage();
sourceImage.BeginInit();
sourceImage.UriSource = new Uri("pack://application:,,,/Resources/Images/Application.ico");
sourceImage.EndInit();
// Set the source
displayImage.Source = sourceImage;
// Set the size you want
displayImage.Width = 96;
displayImage.Stretch = Stretch.Uniform;
Nemanja Banda
  • 796
  • 2
  • 14
  • 23
-1

I havent tried with ico files, I think could be useful here.

    /// <summary>
    /// Resizes image with high quality
    /// </summary>
    /// <param name="imgToResize">image to be resized</param>
    /// <param name="size">new size</param>
    /// <returns>new resized image</returns>
    public Image GetResizedImage(Image imgToResize, Size size) 
    {
      try
      {
       if (imgToResize != null && size != null)
       {
         if (imgToResize.Height == size.Height && imgToResize.Width == size.Width)
         {
            Image newImage = (Image)imgToResize.Clone();
            imgToResize.Dispose();
            return newImage;
         }
         else
         {
            Image newImage = (Image)imgToResize.Clone();
            imgToResize.Dispose();
            Bitmap b = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(newImage, 0, 0, size.Width, size.Height);
            g.Dispose();
            return (Image)b;
        }
      }
       return null;
     }
     catch (Exception e)
     {
       log.Error("Exception in Resizing an image ", e);
       return null;
     }
    }
DotNetUser
  • 6,494
  • 1
  • 25
  • 27