操作可写入位图像素
本文关键字:位图 像素 操作 | 更新日期: 2023-09-27 17:59:38
我正在尝试将两个图像合并为WriteableBitmaps。我想对每个像素值进行平方,将其添加到另一个图像中,然后取2次根(如果值大于255,则进行+检查)。由于我在使用WriteableBitmapEx和ForEach()(Sobel运算符和WriteableBitmapEx的卷积)(这似乎也很慢)时运气不佳,我尝试直接使用BitmapDecoder操作像素。不幸的是,我似乎无法将Pixelstream写回WriteableBitmap,因为我得到了错误:
"Windows.Storage.Streams.IBuffer"不包含"AsStream"的定义和最佳扩展方法重载'System.IO.WindowsRuntimeStreamExtensions.AsStream(Windows.Storage.Streams.IRandomAccessStream)'有一些无效的参数
和
实例参数:无法从转换"Windows.Storage.Streams.IBuffer"到"Windows.Storage.Streams.IRandomAccessStream"
对于这些线路
using (Stream stream = bmp.PixelBuffer.AsStream())
{
await stream.WriteAsync(pixels1, 0, pixels1.Length);
}
这可能是WriteableBitmapEx库破坏的东西吗?
此外,我想知道如何将我的WriteableBitmaps放入BitmapDecoders。我从一本Win8编码的书中获得了这个代码。
到目前为止,这是我的完整代码:
async Task SobelCombine(BitmapDecoder decoder1, BitmapDecoder decoder2)
{
PixelDataProvider provider1 = await decoder1.GetPixelDataAsync(
BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(),
ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
byte[] pixels1 = provider1.DetachPixelData();
PixelDataProvider provider2 = await decoder1.GetPixelDataAsync(
BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(),
ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
byte[] pixels2 = provider1.DetachPixelData();
for (int i = 0; i < pixels1.Length; i += 4){
pixels1[i] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i], 2) + Math.Pow(pixels2[i], 2))) % byte.MaxValue);
pixels1[i + 1] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i + 1], 2) + Math.Pow(pixels2[i + 1], 2))) % byte.MaxValue);
pixels1[i + 2] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i + 2], 2) + Math.Pow(pixels2[i + 2], 2))) % byte.MaxValue);
}
WriteableBitmap bmp = new WriteableBitmap((int)decoder1.OrientedPixelWidth, (int)decoder1.OrientedPixelHeight);
using (Stream stream = bmp.PixelBuffer.AsStream())
{
await stream.WriteAsync(pixels1, 0, pixels1.Length);
}
}
IBuffer.AsStream()
是一个扩展方法。如果你想使用它,你需要包括定义它的名称空间:
using System.Runtime.InteropServices.WindowsRuntime;
更新:正如我在下面的评论中提到的,我在可写位图方面做得还不多。但是,您的代码不会设置透明度值。这可能至少是问题的一部分:
for (int i = 0; i < pixels1.Length; i += 4){
pixels1[i] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i], 2) + Math.Pow(pixels2[i], 2))) % byte.MaxValue);
pixels1[i + 1] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i + 1], 2) + Math.Pow(pixels2[i + 1], 2))) % byte.MaxValue);
pixels1[i + 2] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i + 2], 2) + Math.Pow(pixels2[i + 2], 2))) % byte.MaxValue);
}
应为:
for (int i = 0; i < pixels1.Length; i += 4){
pixels1[i] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i], 2) + Math.Pow(pixels2[i], 2))) % byte.MaxValue);
pixels1[i + 1] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i + 1], 2) + Math.Pow(pixels2[i + 1], 2))) % byte.MaxValue);
pixels1[i + 2] = (byte)((byte)Math.Sqrt((Math.Pow(pixels1[i + 2], 2) + Math.Pow(pixels2[i + 2], 2))) % byte.MaxValue);
pixels1[i + 3] = 255; // or some combination of the source data. 0 == completely transparent.
}