2011-03-11 40 views
0

如何將此GDI代碼轉換爲WPF代碼?以最佳質量縮小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像素圖像。

回答

1

要在DataGrid單元格中顯示位圖,可以使用DataGridTemplateColumn和DataTemplate使用IValueConverter在DataGrid單元格中顯示圖像。

您可以使用BmpBitmapDecoder的屬性來實現儘可能好的圖像。

以下是XAML中DataGrid的定義:
1-我在DataGrid中有三列,第一列是圖像。
2-我設置Path =。因爲我只想從轉換器加載圖像。
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 Doc的關聯圖標。 如果您想要處理多個圖標,則會在Dictionary中存儲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; } 
} 
+0

@贊博尼我現在有更少的時間來檢查代碼,但我在下個週末嘗試。當然,如果它的好代碼,我會將它標記爲解決方案;-) – msfanboy 2011-03-14 11:49:49

+0

@Zbonboni你會在哪裏創建字典,在那裏我緩存bitmapFrame?我正在使用MVVM,我不太確定我會使用Converter。 – msfanboy 2011-03-20 10:38:29

+0

如果代碼的多個部分需要使用這些圖像,則需要將字典移至更全局/共享的位置以使其可訪問;否則,您應該將字典放在使用BitmapFrame的代碼附近。 如果要控制ViewModel中圖像的顯示,請將此示例中的代碼複製到ViewModel中,並將_bitmapFrame分配給ViewModel中的ImageSource類型屬性,並從視圖中將其綁定到它。 如果轉換器是唯一使用圖像的地方,我可能會保留在轉換器中。 – Zamboni 2011-03-20 15:34:43