1

I want to adjust brightness and contrast of an image with help of two sliders in my windows store app.I have already struggled a lot.I am very much new to c#.Any help would be appreciated. For Brightness I am doing this

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;
    }
}
default locale
  • 13,035
  • 13
  • 56
  • 62
Pranav Mahajan
  • 2,048
  • 2
  • 23
  • 36
  • 1
    Why not show us what you wrote while struggling ? – xlecoustillier Dec 08 '14 at 13:04
  • I'm afraid your question is too broad. Stackoverflow is not a freelance board to implement your requirements. Please, try to post specific programming problem you struggling with. – default locale Dec 08 '14 at 13:05
  • Prodviding some (code-)examples or approaches in general taken so far would be nice. – SSchuette Dec 08 '14 at 13:05
  • 1
    Have a look at this SO thread http://stackoverflow.com/questions/15408607/adjust-brightness-contrast-and-gamma-of-an-image – Alex Dec 08 '14 at 13:08
  • But on some dynamic uri it is giving "Value does not fall within the expected range" exception.Also this is extremely slow for larger image size. – Pranav Mahajan Dec 08 '14 at 13:09
  • @Alex:How to give the source of the image in the link that you suggested? – Pranav Mahajan Dec 08 '14 at 13:12
  • @PranavMahajan if this code is giving you an exception, you should post exception details (problematic line of code, stack trace, exception type and message). – default locale Dec 08 '14 at 14:01
  • Your question was too broad so I simply googled the title of your question and found that thread on SO. I have no idea what issues that code has...I guess it's up to you to figure it out ;) – Alex Dec 08 '14 at 14:42
  • To add even more to your struggle - this sort of code should be written in DirectX. Possibly in C++ too! – Filip Skakun Dec 08 '14 at 18:32
  • StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/download.jpg")); In this line,when i am fetching the image from some API then I am getting this exception "Value does not fall within the expected range". – Pranav Mahajan Dec 09 '14 at 04:41

0 Answers0