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

发布于 2024-10-21 06:13:36 字数 836 浏览 0 评论 0原文

如何将此 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 像素图像。

How can I convert this GDI code to WPF code?

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(); 

It seems I have to use the ToBitmap() methode but from then on I want to use WPF only.

In the end I want to display small 16x16 pixel Images in a WPF DataGrid`s Column via Binding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2024-10-28 06:13:36

要在 DataGrid 单元格中显示位图,您可以将 DataGridTemplateColumn 与 DataTemplate 结合使用,并使用 IValueConverter 在 DataGrid 单元格中显示图像。

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

以下是 XAML 中 DataGrid 的定义:
1-我在 DataGrid 中有三列,第一列是图像。
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>

下面是一次性查找 Word 文档关联图标的转换器。
如果要处理多个图标,请将 BitmapFrame 引用存储在字典中,并使用“value”输入参数来选择要显示的图像。

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace ContextMenuNotFiring.Helpers
{
  [ValueConversion(typeof(object), typeof(BitmapSource))]
  public sealed class ImageConverter : IValueConverter
  {
    private static BitmapFrame _bitmapFrame = null;

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
      try
      {
        if (_bitmapFrame == null)
        {
          using (Icon bigIcon32x32 = Icon.ExtractAssociatedIcon("c:\\temp\\test.docx"))
          {
            using (Bitmap bm = bigIcon32x32.ToBitmap())
            {
              MemoryStream finalStream = new MemoryStream();
              {
                bm.Save(finalStream, ImageFormat.Bmp);
                BmpBitmapDecoder bitmapDecoder = new BmpBitmapDecoder(finalStream,
                            BitmapCreateOptions.None, BitmapCacheOption.None);
                _bitmapFrame = bitmapDecoder.Frames[0];

              }
            }
          }
        }

        return _bitmapFrame;
      }
      catch
      {
        return Binding.DoNothing;
      }
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}

视图模型在我的视图模型构造函数中加载以下客户集合。

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

To show a bitmap in a DataGrid cell you can use a DataGridTemplateColumn with a DataTemplate using the IValueConverter to display an image in a DataGrid cell.

You can play with properties of BmpBitmapDecoder to achieve as good an image as possible.

Here is the definition for the DataGrid in XAML:
1- I have three columns in the DataGrid with the first one being the image.
2- I set Path=. because all I wanted to do was load the image from the converter.
3- The DataGrid binds to a Customers collection in a ViewModel, and I included the definitions of those for completeness at the end.

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

Here is the converter that does a onetime lookup of the Word Doc's associated icon.
If you want to handle multiple icons store the BitmapFrame references in a Dictionary and use the "value" input parameter to select which image to display.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace ContextMenuNotFiring.Helpers
{
  [ValueConversion(typeof(object), typeof(BitmapSource))]
  public sealed class ImageConverter : IValueConverter
  {
    private static BitmapFrame _bitmapFrame = null;

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
      try
      {
        if (_bitmapFrame == null)
        {
          using (Icon bigIcon32x32 = Icon.ExtractAssociatedIcon("c:\\temp\\test.docx"))
          {
            using (Bitmap bm = bigIcon32x32.ToBitmap())
            {
              MemoryStream finalStream = new MemoryStream();
              {
                bm.Save(finalStream, ImageFormat.Bmp);
                BmpBitmapDecoder bitmapDecoder = new BmpBitmapDecoder(finalStream,
                            BitmapCreateOptions.None, BitmapCacheOption.None);
                _bitmapFrame = bitmapDecoder.Frames[0];

              }
            }
          }
        }

        return _bitmapFrame;
      }
      catch
      {
        return Binding.DoNothing;
      }
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}

The View Model loads the following collection of Customers my View Model constructor.

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文