在WPF中以最佳质量缩小位图

在WPF中以最佳质量缩小位图,wpf,image,bitmap,scale,Wpf,Image,Bitmap,Scale,如何将此GDI代码转换为WPF代码 Icon bigIcon32x32 = null; bigIcon32x32 = Icon.ExtractAssociatedIcon("c:\\test.docx"); Bitmap bm = bigIcon32x32.ToBitmap(); Bitmap thumb16x16 = new Bit

如何将此GDI代码转换为WPF代码

Icon bigIcon32x32 = null;
                    bigIcon32x32 = Icon.ExtractAssociatedIcon("c:\\test.docx");                    

                    Bitmap bm = bigIcon32x32.ToBitmap();

                    Bitmap thumb16x16 = new Bitmap(16, 16);
                    Graphics graphics = Graphics.FromImage(thumb16x16);
                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.DrawImage(bm, new Rectangle(0, 0, 16, 16), new Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel);

                    graphics.Dispose();
                    bm.Dispose(); 
                    thumb16x16.Dispose(); 
似乎我必须使用ToBitmap()方法,但从那时起,我只想使用WPF


最后,我想通过绑定在WPF DataGrid的列中显示16x16像素的小图像。

要在DataGrid单元格中显示位图,可以使用DataGridTemplateColumn和DataTemplate,并使用IValueConverter在DataGrid单元格中显示图像

您可以使用BmpBitmapDecoder的属性来获得尽可能好的图像

以下是数据网格在XAML中的定义:
1-我在数据网格中有三列,第一列是图像。
2-我设置路径=。因为我只想从转换器加载图像。
3-DataGrid绑定到ViewModel中的Customers集合,为了完整性,我在最后包含了这些集合的定义

<Window x:Class="ContextMenuNotFiring.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Helpers="clr-namespace:ContextMenuNotFiring.Helpers" 
  Title="Main Window" Height="400" Width="800">
  <Window.Resources>
    <Helpers:ImageConverter  x:Key="imgConv"/>
  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <DataGrid
       Grid.Row="0"
       IsSynchronizedWithCurrentItem="True"
       Background="Transparent" 
       AutoGenerateColumns="False"
       ItemsSource="{Binding Customers}">
     <DataGrid.Columns>
        <DataGridTemplateColumn
           Header="Icon" 
           Width="SizeToHeader">
           <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <Image Source="{Binding Path=., Converter={StaticResource imgConv}}" />
              </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn 
           Header="First Name" 
           Width="SizeToHeader"
           Binding="{Binding FirstName}" />
        <DataGridTextColumn 
           Header="Last Name" 
           Width="SizeToCells"
           Binding="{Binding LastName}" />
       </DataGrid.Columns>
    </DataGrid>
  </Grid>
</Window>
视图模型加载以下客户集合“我的视图模型构造函数”

private List<Customer> _customers = new List<Customer>():
public List<Customer> Customers
{
   get
   {
      return _customers;
   }
}

public class Customer
{
  public String FirstName { get; set; }
  public String LastName { get; set; }
}
private List\u customers=new List():
公开名单客户
{
得到
{
退回客户;
}
}
公共类客户
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}

@Zamboni我现在检查代码的时间少了,但我会在下个周末试试。当然,如果它的代码很好,我会将其标记为解决方案;-)@Zamboni在哪里创建字典,在哪里缓存位图帧?我使用的是MVVM,我不确定我是否会使用转换器。如果您的代码的多个部分需要使用这些图像,那么您需要将字典移动到一个更全局/共享的位置,以使其可访问;否则,应将字典放在使用位图框的代码附近。如果要控制ViewModel中图像的显示,请将本例中的代码复制到ViewModel,并将_bitmapFrame指定给ViewModel中的ImageSource类型属性,并从视图绑定到该属性。如果转换器是唯一使用图像的地方,我可能会将其保存在转换器中。转换器的目的是转换一个值并返回该值。但是在你的转换器里你甚至不用这个值???代码对我来说没有意义。它看起来像是我在SO上找到的一个代码片段,它在没有上下文的情况下被放入转换器,你明白我的担心吗?我已经包含了DataGrid的完整XAML定义;我希望这有帮助。
private List<Customer> _customers = new List<Customer>():
public List<Customer> Customers
{
   get
   {
      return _customers;
   }
}

public class Customer
{
  public String FirstName { get; set; }
  public String LastName { get; set; }
}