Wpf Image 展示方式 图片处理 显示
1、创建Bitmap
bash
复制代码
public Bitmap CreateBitmap(int width,int height,int step,IntPtr pdata)
{
return new Bitmap(
width,
height,
step,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed,
pdata);
}
2、Bitmap转BitmapSource显示
csharp
复制代码
public void ShowImg(System.Drawing.Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
Img = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
3、Bitmap转BitmapImage显示
csharp
复制代码
public void ShowImgEx(System.Drawing.Bitmap bitmap)
{
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以
ms.Close();
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(bytes);
image.EndInit();
Img = image;
bitmap.Save("111.bmp",ImageFormat.Bmp);
}
4、WriteableBitmap 显示
csharp
复制代码
public void ShowImgEx2(int width, int height, IntPtr pdata)
{
var (pixel, palette) = GetPixelFormat(1);
int bufferLen = width * height;
BitmapSource? bitmapSource =
WriteableBitmap.Create(width, height
, 96, 96, pixel, palette, pdata, bufferLen, width);
Img = bitmapSource;
}