[英]How do you get a Bitmap Image into a wpf ListView
這就是我所擁有的
<GridViewColumn Header="Status" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Width="22" Height="22"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
圖像是從Namespace.Resources獲取的System.Drawing.Bitmap圖像。
無論我如何嘗試,都無法在列中顯示任何圖像。
您需要從System.Drawing.Bitmap
轉換為ImageSource,這是WPF用於圖像的。 您可以通過Imaging.CreateBitmapSourceFromHBitmap來執行此操作 :
// Include, in your class or elsewhere:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
Bitmap image = LoadYourBitmap();
IntPtr hbitmap = image.GetHbitmap();
try
{
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
hbitmap, IntPtr.Zero, Int32Rect.Empty,
Imaging.BitmapSizeOptions.FromEmptyOptions());
}
finally
{
// Clean up the bitmap data
DeleteObject(hbitmap);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.