Sizing and working with ICO files in WPF/C# Sizing and working with ICO files in WPF/C# wpf wpf

Sizing and working with ICO files in WPF/C#


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.


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

Image displayImage = new Image();// Create the sourceBitmapImage sourceImage = new BitmapImage();sourceImage.BeginInit();sourceImage.UriSource = new Uri("pack://application:,,,/Resources/Images/Application.ico");sourceImage.EndInit();// Set the sourcedisplayImage.Source = sourceImage;// Set the size you wantdisplayImage.Width = 96;displayImage.Stretch = Stretch.Uniform;


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