WPF C# Extrahieren, und Zeigen Sie Standard-Datei-Symbol
Ich bin das extrahieren von Standard-system-icons (16 * 16) nur für einige Erweiterungen wie XLS, XLSX, PDF etc. und versuchen zu zeigen, daß im Bild-Steuerelement. Ich bin zu bemerken, dass, wenn ich dump dieser Symbole auf der Festplatte, die Sie scharf sehen, aber die icons in meine ListBox sehen etwas verschwommen.
Irgendwelche Ideen, was könnte hier Los ? Ich habe versucht, eine Menge von Anregungen, die Beiträge, die ich Lesen, konnte aber nicht an das problem gehen
Code-Snippet:
Case1 - ich bin extrahieren das Symbol der Datei, die durch Win32 Pinvoke - Ergebnis -> blurrred Symbole
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-Symbol erstellte Objekte oben auf der Festplatte und betrachten Sie Sie im explorer -> die icons sind von ausgezeichneter Qualität
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>
Wie ich bin-converting-Symbol, um ImageSource-Objekt:
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;
}
Alle Hinweise werden sehr geschätzt!
Dank
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ich weiß, es ist schon eine Weile dieses Thema ist gelöst, aber ich habe gesehen, eine Coole Abkürzung, um win32 invoke :
Ich denke, es könnte besser sein, als alles, was Sie getan haben.
Quelle : Hier
Hier ist eine Lösung zu deiner Frage:
Holen Sie sich Registrierten Dateitypen und die Zugehörigen Symbole in C#
gemacht, indem Man Vuong.
Teilen, was geholfen hat in meinem Fall für die anderen, die vor ähnlichen problem.
Festlegen dieser Eigenschaft auf das Bild geholfen, eine Tonne bei der Beseitigung der Unschärfe:
Im anderen Fall, wir waren, zeigt das Bild in einem raster-Zelle, die mit 20 * 20, so landeten wir entfernen die Standard-Bild-Strecken mit niedriger Einstellung. Das machte eine Menge von diff zu.