[英]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.