从 BitmapSource 复制到 WritableBitmap
我正在尝试将 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:值不在预期范围内”。在CopyPixels
行中。
我尝试将 row.PixelHeight * row.BackBufferStride
与 row.PixelHeight * row.PixelWidth
交换,但随后出现错误,指出该值太低。
我找不到使用 CopyPixels
重载的单个代码示例,因此我寻求帮助。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试图复制图像的哪一部分?更改目标 ctor 中的宽度和高度,以及 Int32Rect 中的宽度和高度以及前两个参数 (0,0),即 x &图像中的 y 偏移量。或者,如果您想复制整个内容,请离开。
What part of the image are trying to copy? change the width and height in the target ctor, and the width and height in Int32Rect as well as the first two params (0,0) which are x & y offsets into the image. Or just leave if you want to copy the whole thing.