首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何着色图像@ Windows-10地铁应用程序?

如何着色图像@ Windows-10地铁应用程序?
EN

Stack Overflow用户
提问于 2015-10-12 18:23:59
回答 2查看 115关注 0票数 0

我做了灰度图像,我想给它着色(例如改变HSL )。

在我在互联网上发现的例子中,我需要位图类,但这在Windows-10通用应用程序中并不存在。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-13 19:59:54

在经历了许多错误之后,这里是工作代码:

它看起来可能很糟糕,但它正在起作用,我很高兴:)

代码语言:javascript
运行
复制
private static async void changeImage(Molecule molecule)
{
    Molecule.Reaction++;    // Used as Working/Loading status here
    var atoms = molecule.Atoms;
    if (atoms > 5) atoms = 5;

    var stream = await GetStream("Assets/Images/" + atoms + ".png");
    var decoder = await BitmapDecoder.CreateAsync(stream); 
    var pixelData = await decoder.GetPixelDataAsync();
    var data = pixelData.DetachPixelData(); // Finally something where I can get my pixels

    var wb = new WriteableBitmap(16, 16);
    //wb.SetSource(stream);     // Get pixel not working so I dont need this

    for(int y = 0; y < decoder.PixelHeight; y++)
    {
        for(int x = 0; x < decoder.PixelWidth; x++)
        {
            // var rgba = wb.GetPixel(x, y);    // This is not working, only returnning empty pixels (A:0, R:0, G:0, B:0)

            // Get RGBA value of pixel
            var rgba = new Color {
                R = data[x * 4 + y * decoder.PixelWidth * 4 + 0],
                G = data[x * 4 + y * decoder.PixelWidth * 4 + 1],
                B = data[x * 4 + y * decoder.PixelWidth * 4 + 2],
                A = data[x * 4 + y * decoder.PixelWidth * 4 + 3]
            };

            // Array can't have index -1 and -1 is for gray what is default
            if (molecule.Owner != -1)
            {
                // Colorize pixel
                var z = ((rgba.R + rgba.G + rgba.B) / 3.0) / 256;
                rgba.R = Convert.ToByte(z * Settings.Colors[molecule.Owner].Item1.Color.R);
                rgba.G = Convert.ToByte(z * Settings.Colors[molecule.Owner].Item1.Color.G);
                rgba.B = Convert.ToByte(z * Settings.Colors[molecule.Owner].Item1.Color.B);
                data[x * 4 + y * decoder.PixelWidth * 4 + 0] = rgba.R;
                data[x * 4 + y * decoder.PixelWidth * 4 + 1] = rgba.G;
                data[x * 4 + y * decoder.PixelWidth * 4 + 2] = rgba.B;
            }

            wb.SetPixel(x, y, rgba); // I tried to connect byte[] and WriteAbleBitmap via stream but Failed so I'm using this
        }
    }

    if (atoms == molecule.Atoms)    // Because it's async method so If value is already different don't change it to old picture
    {
        molecule.parent.Content = new Image();
        (molecule.parent.Content as Image).Source = wb;
        (molecule.parent.Content as Image).Width = 24;
        (molecule.parent.Content as Image).Height = 24;
    }

    Molecule.Reaction--;    // done changing picture
}

internal static async Task<IRandomAccessStream> GetStream(string relativePath)
{
    var storageFile = await Package.Current.InstalledLocation.GetFileAsync(relativePath.Replace('/', '\\'));
    var stream = await storageFile.OpenReadAsync();
    return stream;
}
票数 0
EN

Stack Overflow用户

发布于 2015-10-13 09:17:49

WriteableBitmap类可以用来获取图像的像素缓冲区,并使用语言指定的方法修改像素内容。看一看“评论”和“例句”,更好地理解。

我想向您推荐WriteableBitmapEx,它包括对WriteableBitmap的许多扩展方法。您可以在项目中安装NuGet包来使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33087759

复制
相关文章

相似问题

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