/// <summary>
/// WriteableBitmap转Bitmap图像
/// </summary>
/// <param name="wBitmap"></param>
/// <returns></returns>
public static Bitmap WriteableBitmapToBitmap(WriteableBitmap wBitmap)
{
Bitmap bmp = new Bitmap(wBitmap.PixelWidth, wBitmap.PixelHeight);
int rPixelBytes = wBitmap.BackBufferStride * wBitmap.PixelHeight; //字节数,计算方式是幅宽乘以高度像素
//注意,像素格式根据实际情况
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, wBitmap.PixelWidth, wBitmap.PixelHeight), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
wBitmap.Lock();
unsafe
{
Buffer.MemoryCopy(wBitmap.BackBuffer.ToPointer(), data.Scan0.ToPointer(), rPixelBytes, rPixelBytes);
}
//Buffer.MemoryCopy需要在.net 4.6版本或更高版本上才可以使用,.net4.5不存在该方法。
wBitmap.AddDirtyRect(new Int32Rect(0, 0, (int)wBitmap.Width, (int)wBitmap.Height));
wBitmap.Unlock();
bmp.UnlockBits(data);
return bmp;
}