簡體   English   中英

如何有效地調整圖像的亮度

[英]How to adjust the brightness of an Image efficiently

有誰知道在 UWP 中在運行時調整圖像亮度的更有效方法?

我發現這個問題很好用,但運行速度非常慢。 但是,我在網上找不到任何表明有替代方法的文檔。

這是我的有問題的代碼。

// TODO Make Image Brightness Slider quicker and more intuitive. 
private WriteableBitmap ChangeBrightness(WriteableBitmap source, int increment)
{
    var dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight);

    byte[] color = new byte[4];

    using (var srcBuffer = source.PixelBuffer.AsStream())
    using (var dstBuffer = dest.PixelBuffer.AsStream())
    {
        while (srcBuffer.Read(color, 0, 4) > 0)
        {
            for (int i = 0; i < 3; i++)
            {
                var value = (float)color[i];
                var alpha = color[3] / (float)255;
                value /= alpha;
                value += increment;
                value *= alpha;

                if (value > 255)
                {
                    value = 255;
                }

                color[i] = (byte)value;
            }

            dstBuffer.Write(color, 0, 4);
        }
    }

    return dest;
}

這可能會奏效。 我沒有測試它:

    private async Task<WriteableBitmap> ChangeBrightness(WriteableBitmap source, float increment)
    {
        var canvasBitmap = CanvasBitmap.CreateFromBytes(CanvasDevice.GetSharedDevice(), source.PixelBuffer,
            source.PixelWidth, source.PixelHeight, DirectXPixelFormat.B8G8R8A8UIntNormalized);

        var brightnessFx = new BrightnessEffect
        {
            Source = canvasBitmap,
            BlackPoint = new Vector2(0, increment)
        };

        var crt = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), source.PixelWidth, source.PixelHeight, 96);

        using (var ds = crt.CreateDrawingSession())
        {
            ds.DrawImage(brightnessFx);
        }

        crt.GetPixelBytes(source.PixelBuffer);

        return source;
    }

你必須參考 win2d nuget

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM