问题描述
当我尝试修改已加载图像中的像素与来自空创建的WriteableBitmap的像素时,使用WriteableBitmap直接操作像素时,我看到了一些奇怪的效果。
I am seeing some strange effects when manipulating pixels directly using a WriteableBitmap when I am trying to modify pixels from a loaded image vs pixels from an empty created WriteableBitmap.
当我通过常用像素[(x + width * y)<< 2]创建一个空的WriteableBitmap设置像素时,方法可以正常工作,但是当我我试图修改加载图像的PixelBuffer而不是相同的尺寸相同的像素设置
方法确实设置了具有剪切偏移的像素,我必须手动调整步幅一个固定值(随每个加载而变化)图像文件)以获得相同的结果:像素[(x +(宽度+调整)* y)<< 2]。这个奇怪的
的原因是像素缓冲区的长度在两种情况下都是相同的。
When I create an empty WriteableBitmap setting pixels via the usual pixels[ (x + width * y)<<2] method works just as expected, but when I am trying to modify a PixelBuffer of a loaded image instead which has identical dimensions the same pixel setting method does set the pixels with a sheared offset and I have to manually adjust the stride by a fixed value (which varies with each loaded image file) in order to get the same result: pixels[ (x + (width + adjustment) * y)<<2]. What makes this so strange is that the length of the pixel buffer is identical in both cases.
所以我怀疑在某些情况下可能会出现某种转换或填充Stream.write()或Stream.read()方法,但我真的无法理解它。
So I suspect there might be some kind of conversion or padding going on in the Stream.write() or Stream.read() methods, but I cannot really make sense of it.
我使用以下方法来访问数据:
I am using the following methods to access the data:
// Read PixelBuffer from a WriteableBitmap:
Width = map.PixelHeight;
Height= map.PixelWidth;
Stream stream = map.PixelBuffer.AsStream();
byte[] pixels = new byte[stream.Length];
stream.Seek(0, 0);
stream.Read(pixels, 0, pixels.Length);
[...]
//Write PixelBuffer back
Stream stream = map.PixelBuffer.AsStream();
stream.Seek(0, 0);
stream.Write(pixels, 0, pixels.Length);
map.Invalidate();
[...]
//Create an empty WriteableBitmap
pixels = new byte[ Width * Height << 2];
WriteableBitmap map = new WriteableBitmap(Width,Height);
Stream stream = map.PixelBuffer.AsStream();
stream.Seek(0, 0);
stream.Write(pixels, 0, pixels.Length);
map.Invalidate();
[...]
//set pixel
public void setPixel(int x, int y, uint rgba) {
int stride = Width;
int index = (y * stride + x) << 2;
pixels[index] = (byte)((rgba >> 24) & 0xff);
pixels[index+1] = (byte)((rgba >> 16) & 0xff);
pixels[index+2] = (byte)((rgba >> 8) & 0xff);
pixels[index+3] = (byte)(rgba & 0xff);
}
推荐答案
ImageProperties props = await file.Properties.GetImagePropertiesAsync();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(await file.OpenAsync(FileAccessMode.Read));
BitmapTransform transform = new BitmapTransform();
transform.Bounds = new BitmapBounds() { Width = props.Width, Height = props.Height };
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, decoder.BitmapAlphaMode, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
byte[] pixels = flipRGBA( pixelData.DetachPixelData());
WriteableBitmap map = new WriteableBitmap(props.Width, props.Height);
Stream stream = map.PixelBuffer.AsStream();
stream.Seek(0, 0);
stream.Write(pixels, 0, pixels.Length);
map.Invalidate();
这篇关于WriteableBitmap.PixelBuffer.AsStream()的步幅问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!