I am trying to copy a part of a BitmapSource to a WritableBitmap.
This is my code so far:
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();
I get "ArgumentException: Value does not fall within the expected range." in the line of CopyPixels
.
I tried swapping row.PixelHeight * row.BackBufferStride
with row.PixelHeight * row.PixelWidth
, but then I get an error saying the value is too low.
I couldn't find a single code example using this overload of CopyPixels
, so I'm asking for help.
Thanks!