Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:05:27+00:00 2026-06-14T18:05:27+00:00

I am extracting system default icons (16 * 16 only) for some extensions like

  • 0

I am extracting system default icons (16 * 16 only) for some extensions like XLS, XLSX, PDF etc and trying to show that in Image Control. I am noticing that if I take dump of those icons on disk, they look sharp but the icons in my ListBox look somewhat blurred.

Any ideas what could be going on here ? I have tried a lot of suggestions from the posts I read from but couldn’t make the problem go

Code Snippet:

Case1 – I am extracting the icon of file through Win32 Pinvoke – result -> blurrred icons

private const uint SHGFI_ICON = 0x100;
    private const uint SHGFI_LARGEICON = 0x0;
    private const uint SHGFI_SMALLICON = 0x1;
    private const int FILE_ATTRIBUTE_NORMAL = 0x80;
    private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010;
    [StructLayout(LayoutKind.Sequential)]
    private struct SHFILEINFO
    {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };
    [DllImport("shell32.dll")]
    private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,   
   ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

private static Icon Extract(string fileName)
{
var shinfo = new SHFILEINFO();

IntPtr hIcon = SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
//The icon is returned in the hIcon member of the shinfo struct
var icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
DestroyIcon(shinfo.hIcon);
return icon;
}
[DllImport("User32.dll")]
public static extern int DestroyIcon(IntPtr hIcon);

Case2 – Dumping Icon objects created above to disk and viewing them in explorer -> the icons are of excellent quality

string filePath = string.Format("C:\\temp\\Icons\\{0}.ico", iconName.Substring(1));
var stream = new FileStream(filePath, FileMode.OpenOrCreate);
iconObject.ExtensionSource.ToBitmap().Save(stream, ImageFormat.Bmp);
stream.Close();

ListBox xaml –

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal"
                    Grid.Row="0"
                    Margin="10">
            <TextBox Text="{Binding ExtensionNames, Mode=TwoWay}"
                     ToolTip="Comma Separated List of Extensions, Tab out to persist changes.."
                     Margin="5"/>
            <Button Content="Dump All Icons" 
                    Click="Button_DumpAllIconsClick" 
                    Width="100"
                    ToolTip="Dump Path: C:\temp\Icons"
                    Margin="5"/>

        </StackPanel>
        <ListBox ItemsSource="{Binding Icons}"
                 Grid.Row="1"
                 Margin="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal"
                                HorizontalAlignment="Stretch"
                                Margin="5">
                        <TextBlock Text="{Binding ExtensionName}"
                                       FontWeight="Bold"
                                       FontSize="20"/>
                        <Image Source="{Binding Path=ExtensionSource,
                                                Converter={ExtractIconWPF:IconToImageSourceConverter}}"
                               Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
</Grid>

How I am converting Icon to ImageSource object:

var stream = GetIconStream(iconObject);
ImageSource source = GetBitMapImage(stream);

public static Stream GetIconStream(Icon icon)
{
var iconStream = new MemoryStream();

var bitmap = icon.ToBitmap();
bitmap.Save(iconStream, ImageFormat.Png);

return iconStream;
}

public static ImageSource GetBitMapImage(Stream iconStream)
{
var bi = new BitmapImage();

bi.BeginInit();
bi.StreamSource = iconStream;
bi.EndInit();

return bi;
}

Any pointers will be highly appreciated!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:05:28+00:00Added an answer on June 14, 2026 at 6:05 pm

    Sharing what helped in my case for others who face similar problem.

    Setting this property on Image helped a ton in removing the blur:

    RenderOptions.BitmapScalingMode="NearestNeighbor"
    

    In other case, we were showing the image in a grid cell which was 20 * 20, so we ended up removing the default Image stretch with below setting. That made a lot of diff too.

    Stretch = Stretch.None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Installing Sparks assumes that you are abiding by the default CodeIgniter installation pattern; extracting
I was trying to implement an extension to an exception collecting system. After extracting
On extracting some html from a web page, I have some elements containing text
I need some help extracting the following bits of information using regular expressions. Here
I'm trying to parse through fixed-width formatted file extracting x,y values of points from
TI'm trying to update Ruby on my system using RVM. I currently use Ruby-1.9.3-p194
I'm trying to write an extension (plug-in) for Eclipse BIRT reporting. It involves extracting
I am trying to install MySQL5 on Snow Leopard, but I am having some
I'm working to develop a small system for extracting content from web pages (I
I am extracting r8g8b8 palette from file, looks like its very easy to convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.