我正在尝试将BitmapSource的一部分复制到WritableBitmap中。
这是我目前的代码:
var bmp = image.Source as BitmapSource;
var row = new WriteableBitmap(bmp.PixelWidth, bottom - top, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette);
row.Lock();
bmp.CopyPixels(new Int32Rect(top, 0, bmp.PixelWidth, bottom - top), row.BackBuffer, row.PixelHeight * row.BackBufferStride, row.BackBufferStride);
row.AddDirtyRect(new Int32Rect(0, 0, row.PixelWidth, row.PixelHeight));
row.Unlock();
我遇到了"ArgumentException: Value does not fall within the expected range."的问题,出现在CopyPixels
这一行。
我尝试将row.PixelHeight * row.BackBufferStride
和row.PixelHeight * row.PixelWidth
交换,但是我得到了一个错误,说这个值太低了。
我找不到任何使用这个重载版本的CopyPixels
的代码示例,所以想请教一下。
谢谢!
width * (bitsPerPixel + 7) / 8
。它不应该是width * ((bitsPerPixel + 7) / 8)
吗? - mafu(width * pf.BitsPerPixel + 7) / 8
。 - mafu