2013-07-17 107 views
0

我的任務是獲取透明.png圖像(當前位於ImageList中)的現有列表,並將它們顯示在基於ImageID列的WPF DataGrid中。在WPF DataGrid中顯示透明圖像

我已成立的DataGridColumn如下:

_dataTemplateColumn = new DataGridTemplateColumn(); 
    _dataTemplateColumn.Header = ""; 
    FrameworkElementFactory _factory = new FrameworkElementFactory(typeof(Image)); 
    Binding _binding = new Binding("Image"); 
    _binding.Mode = BindingMode.TwoWay; 
    _factory.SetValue(Image.SourceProperty, _binding); 
    DataTemplate _cellTemplate = new DataTemplate(); 
    _cellTemplate.VisualTree = _factory; 
    _dataTemplateColumn.CellTemplate = _cellTemplate; 

    Style _style = new Style(); 
    _style.Setters.Add(new Setter(BackgroundProperty, Brushes.Transparent)); 
    _dataTemplateColumn.CellStyle = _style; 

我然後在運行時創建自定義對象,其包括圖像爲我和運行在圖像下面2種方法,首先要調整其大小和第二把它轉換成一個位圖,而不是一個BitmapImage的(這是我設法得到它在WPF的工作與迄今唯一格式):

public static Bitmap ResizeImage(this Bitmap Bitmap, Size size) 
    { 
     try 
     { 
      Bitmap _bitmap = new Bitmap(size.Width, size.Height); 
      using (Graphics _graphic = Graphics.FromImage((Image)_bitmap)) 
      { 
       _graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       _graphic.DrawImage(Bitmap, 0, 0, size.Width, size.Height); 
      } 
      _bitmap.MakeTransparent(Color.Magenta); 

      return _bitmap; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

    public static Bitmap ToBitmap(this BitmapImage BitmapImage) 
    { 
     using (MemoryStream _stream = new MemoryStream()) 
     { 
      BitmapEncoder _encoder = new BmpBitmapEncoder(); 
      _encoder.Frames.Add(BitmapFrame.Create(BitmapImage)); 
      _encoder.Save(_stream); 
      System.Drawing.Bitmap _bitmap = new System.Drawing.Bitmap(_stream); 
      _bitmap.MakeTransparent(Color.Magenta); 
      return new Bitmap(_bitmap); 
     } 
    } 

圖像被顯示在正確的大小和位置在DataGrid中,但透明度不是pres來自.png格式。如果有人知道對我來說更好的方法(也許將圖像首先帶入資源文件更正確?)或者在當前代碼中獲得透明度的方法,那將是非常感謝!

+0

丟棄所有的'System.Drawing.Bitmap'相關東東。這在WPF中並不需要。只需將'Image.Source'屬性綁定到引用原始圖像文件的'Uri'即可。調整大小已經由'Image'控件完成了。 – Clemens

+0

雖然我沒有使用圖像控件,但圖像需要在DataGridColumn中顯示,並且使用DataGridTemplateColumn完成。 – SkankyMuppet

+0

那麼這是什麼:'FrameworkElementFactory(typeof(Image));'? 'Image'是[Image control](http://msdn.microsoft.com/en-us/library/System.Windows.Controls.Image.aspx)。 – Clemens

回答

0

下面的例子給你怎麼可能看起來像一個想法:

XAML:

<Window ...> 
    <Window.Resources> 
     <DataTemplate x:Key="ImageCellTemplate"> 
      <Image Source="{Binding Image}" Width="100"/> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <DataGrid x:Name="dataGrid" AutoGenerateColumns="False"/> 
    </Grid> 
</Window> 

代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     var col = new DataGridTemplateColumn(); 
     col.CellTemplate = (DataTemplate)Resources["ImageCellTemplate"]; 

     dataGrid.Columns.Add(col); 

     foreach (var file in Directory.EnumerateFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg")) 
     { 
      dataGrid.Items.Add(new DataItem { Image = file }); 
     } 
    } 
} 

public class DataItem 
{ 
    public string Image { get; set; } 
} 
+0

謝謝,那工作:) – SkankyMuppet