Csharp/C#教程:从BitmapSource复制到WritableBitmap分享


从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.PixelWidth交换row.PixelHeight * row.BackBufferStride ,但后来我得到一个错误,说该值太低了。

我找不到使用CopyPixels这个重载的单个代码示例,所以我正在寻求帮助。

谢谢!

图像的哪个部分正在尝试复制? 更改目标ctor中的宽度和高度,以及Int32Rect中的宽度和高度以及x&y偏移到图像中的前两个参数(0,0)。 或者如果你想复制整件事就离开。

上述就是C#学习教程:从BitmapSource复制到WritableBitmap分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 BitmapSource source = sourceImage.Source as BitmapSource; // Calculate stride of source int stride = source.PixelWidth * (source.Format.BitsPerPixel + 7) / 8; // Create data array to hold source pixel data byte[] data = new byte[stride * source.PixelHeight]; // Copy source image pixels to the data array source.CopyPixels(data, stride, 0); // Create WriteableBitmap to copy the pixel data to. WriteableBitmap target = new WriteableBitmap( source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY, source.Format, null); // Write the pixel data to the WriteableBitmap. target.WritePixels( new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), data, stride, 0); // Set the WriteableBitmap as the source for the  element // in XAML so you can see the result of the copy targetImage.Source = target; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/997945.html

(0)
上一篇 2021年12月26日
下一篇 2021年12月26日

精彩推荐