简体   繁体   English

如何将位图图像放入wpf ListView

[英]How do you get a Bitmap Image into a wpf ListView

This is what I have 这就是我所拥有的

<GridViewColumn Header="Status" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Image}" Width="22" Height="22"/>
            </StackPanel>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

Image is a System.Drawing.Bitmap image that I am getting from a Namespace.Resources. 图像是从Namespace.Resources获取的System.Drawing.Bitmap图像。

I cannot get any image to show up in column no matter what I try. 无论我如何尝试,都无法在列中显示任何图像。

You need to convert from the System.Drawing.Bitmap into an ImageSource, which is what WPF uses for images. 您需要从System.Drawing.Bitmap转换为ImageSource,这是WPF用于图像的。 You can do this via Imaging.CreateBitmapSourceFromHBitmap : 您可以通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM