我遇到了一个问题,我试图将一个WriteableBitmap的像素缓冲区复制到另一个WriteableBitmap上,实际上是给出了WriteableBitmap对象的一个副本。然而,当我尝试这样做时,我遇到了一个问题,第二个WriteableBitmap的流长度太短,无法容纳第一个WriteableBitmap的所有值。我在下面发布了我的代码。请记住,我是从网络摄像头捕获原始数据的。但是,当我将"ps“对象的流大小与wb1和wb2进行比较时,ps的大小比这两者都要小得多。我搞不懂的是为什么wb2流的大小比WB1的小。谢谢你的帮助。
private MemoryStream originalStream = new MemoryStream();
WriteableBitmap wb1 = new WriteableBitmap((int)photoBox.Width, (int)photoBox.Height);
WriteableBitmap wb2 = new WriteableBitmap((int)photoBox.Width, (int)photoBox.Height);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
var ps = new InMemoryRandomAccessStream();
await mc.CapturePhotoToStreamAsync(imageProperties, ps);
await ps.FlushAsync();
ps.Seek(0);
wb1.SetSource(ps);
(wb1.PixelBuffer.AsStream()).CopyTo(originalStream); // this works
originalStream.Position = 0;
originalStream.CopyTo(wb2.PixelBuffer.AsStream()); // this line gives me the error: "Unable to expand length of this stream beyond its capacity"
Image img = new Image();
img.Source = wb2; // my hope is to treat this as it's own entity and modify this image independently of wb1 or originalStream
photoBox.Source =wb1;发布于 2012-08-10 02:50:28
请注意,当您执行新的WriteableBitmap(w,h),然后对不同分辨率的图像调用SetSource()时,位图的大小将发生变化(它不会是传入构造函数的w x h)。您的photoBox.Width/Height很可能与您的CapturePhotoToStreamAsync()调用返回的内容不同(我假设图像是在默认或预先配置的相机设置下捕获的,而photoBox只是屏幕上的一个控件)。
不如就这样做点什么
ps.Seek(0);
wb1.SetSource(ps);
ps.Seek(0);
wb2.SetSource(ps);发布于 2012-08-09 22:44:52
我认为您应该从PixelBuffer创建一个写入器,并使用它来复制流。AsStream方法应该用于读取缓冲区,而不是写入缓冲区。
看一看http://social.msdn.microsoft.com/Forums/en-NZ/winappswithcsharp/thread/2b499ac5-8bc8-4259-a144-842bd756bfe2
获取一段代码
https://stackoverflow.com/questions/11885926
复制相似问题