我正在尝试合并两个图像作为WriteableBitmaps。我想要将每个像素值平方,将其添加到另一个图像中,然后取第二个根(如果值大于255,请检查)。由于我没有成功地使用WriteableBitmapEx和ForEach() (Sobel operator & Convolution with WriteableBitmapEx) (这似乎也很慢),我尝试直接使用BitmapDecoder操作像素。不幸的是,我似乎无法将Pixelstream写回一个WriteableBitmap,因为我得到了错误:
'Windows.Storage.Streams.IBuffer‘不包含'AsStream’的定义,最好的扩展方法重载AsStream有一些无效的参数
和
实例参数:无法从“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);
}
}
发布于 2013-02-15 04:25:24
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.
}
https://stackoverflow.com/questions/14879929
复制相似问题