在c#中使用滑块的亮度和对比度?

时间:2014-12-08 13:02:47

标签: c# windows-store-apps winrt-xaml winrt-async

我想在我的windows商店应用程序中使用两个滑块来调整图像的亮度和对比度。我已经挣扎了很多。我对c#非常陌生。任何帮助都会受到赞赏。 对于亮度我正在做这个

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/download.jpg"));

    // 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 slider1_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    if (this.slider1 != null)
    {
        // deterime the brightness to add
        byte value_to_add = (byte)((this.slider1.Value / this.slider1.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;
    }
}

0 个答案:

没有答案