簡體   English   中英

如何使用滑塊調整圖像的亮度和對比度?

[英]How to adjust Brightness and Contrast of an image using a slider?

我有一個圖像,我想通過xaml中的滑塊調整其亮度和對比度。 我不知道從哪里開始。任何幫助將不勝感激。 我正在努力實現這一點

public  WriteableBitmap ChangeBrightness(WriteableBitmap inputBitmap, double brightnessValue )
{
    double Value = brightnessValue;
    var inputPixels = inputBitmap.Pixels;
    for (int i = 0; i < inputPixels.Length; i++)
    {
        // Extract color components
        var c = inputBitmap.Pixels[i];
        var a = (byte)(c >> 24);
        var r = (byte)(c >> 16);
        var g = (byte)(c >> 8);
        var b = (byte)(c);

        int ri = r + (int)Value;
        int gi = g + (int)Value;
        int bi = b + (int)Value;

        // Clamp to byte boundaries
        r = (byte)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
        g = (byte)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
        b = (byte)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
        inputBitmap.Pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
    }
    return inputBitmap;          
}

但我認為這是沒有用的,我沒有找到任何有用的文章。

請參閱調整亮度的公式

使用添加公式,您在程序中缺少2個零件。

第1部分:創建一個新的可寫位圖而不是修改輸入位圖(返回此新位圖)
第2部分:您必須保存位圖的原始副本(亮度不可逆,如上面的鏈接所說)


一個非常慢的實現,讓你開始。 大多數這些東西都可以在MSDN上找到,如果你只是花一些時間閱讀(如果你想讓程序比Hello World更先進,你需要進入這個習慣)。

<StackPanel>
    <Image x:Name="myImage" Stretch="None" Source="Assets/bling.png"/>
    <Slider x:Name="mySilder" ValueChanged="mySilder_ValueChanged"></Slider>
</StackPanel>

// namespaces
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;

// our copy of the image
WriteableBitmap copy;

public MainPage()
{
    this.InitializeComponent();
}

private async void Page_Loaded(object sender, RoutedEventArgs e)
{

    // storage file for the image (load it)
    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/bling.png"));

    // create a bitmap image
    BitmapImage bi = new BitmapImage();

    using (
        // Open a stream for the selected file.
    Windows.Storage.Streams.IRandomAccessStream fileStream =
        await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
    {
        // load the image into the BitmapImage
        await bi.SetSourceAsync(fileStream);

        // create a copy of the image            
        copy = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);

        // load the image into writeablebitmap copy
        fileStream.Seek(0);
        await copy.SetSourceAsync(fileStream);
    }
}

private WriteableBitmap ChangeBrightness(WriteableBitmap source, byte change_value)
{
    WriteableBitmap dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight);        

    byte[] color = new byte[4];

    using (Stream s = source.PixelBuffer.AsStream())
    {
        using (Stream d = dest.PixelBuffer.AsStream())
        {
            // read the pixel color
            while (s.Read(color, 0, 4) > 0)
            {
                // color[0] = b
                // color[1] = g 
                // color[2] = r
                // color[3] = a

                // do the adding algo per byte (skip the alpha)
                for (int i = 0; i < 4; i++)
                {
                    if ((int)color[i] + change_value > 255) color[i] = 255; else color[i] = (byte)(color[i] + change_value);
                }

                // write the new pixel color
                d.Write(color, 0, 4);                        
            }
        }
    }

    // return the new bitmap
    return dest;
}

private void mySilder_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    if (this.mySilder != null)
    {
        // deterime the brightness to add
        byte value_to_add = (byte)((this.mySilder.Value / this.mySilder.Maximum) * 255);

        // get the new bitmap with the new brightness
        WriteableBitmap ret = ChangeBrightness(copy, value_to_add);

        // set it as the source so we can see the change
        this.myImage.Source = ret;
    }
}

代碼在行動

在此輸入圖像描述


如果您正在尋找“bling.png”,可以在這里找到: bling.png

順便說一句,提高預乘圖像亮度的正確功能是

    public static 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;
    }

暫無
暫無

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

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