首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >操纵WriteableBitmap像素

操纵WriteableBitmap像素
EN

Stack Overflow用户
提问于 2013-02-14 16:51:33
回答 1查看 4K关注 0票数 1

我正在尝试合并两个图像作为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”

对于这些行

代码语言:javascript
运行
复制
using (Stream stream = bmp.PixelBuffer.AsStream())
{
   await stream.WriteAsync(pixels1, 0, pixels1.Length);
}

这可能是WriteableBitmapEx库破坏的东西吗?

此外,我还想知道如何将我的WriteableBitmaps放到BitmapDecoders中。我从一本Win8编码书中提取了这段代码。

到目前为止,这是我的完整代码:

代码语言:javascript
运行
复制
    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);
        }

   } 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-15 04:25:24

IBuffer.AsStream()是一种扩展方法。如果要使用它,则需要包含定义名称空间的名称空间:

代码语言:javascript
运行
复制
using System.Runtime.InteropServices.WindowsRuntime;

更新:正如我在下面的评论中提到的那样,我在可写位图方面做了很多工作。但是,您的代码没有设置透明度值。这至少可能是问题的一部分:

代码语言:javascript
运行
复制
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);
}

应:

代码语言:javascript
运行
复制
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.
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14879929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档