0

How can I manipulate my photo to sepia after I click the button.

Below is my code:

private void button1_Click(object sender, RoutedEventArgs e)
{
    BitmapSource image = (BitmapSource)video.Source;
    image.Save(DateTime.Now.ToString("ddMMyyyy HHmmss") + ".jpg", ImageFormat.Jpeg);
    MessageBox.Show("Saved on bin/debug");
}
Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
  • You can do it pretty easily with Shaders: [Shader library](http://perspectivefx.codeplex.com/) – MyKuLLSKI Feb 09 '12 at 17:38
  • i want to use that button to capture the photo with sepia effect and save it to the location. what should i add? – mmmitchell Feb 16 '12 at 15:55
  • Apple the Sepia shadder from the library to the photo – MyKuLLSKI Feb 16 '12 at 16:56
  • i am using the kinect camera in capturing. i dont know what to put in the xaml.cs, where the photo will be in sepia when it saves or after i click the button. i can only use the shaders in xaml. – mmmitchell Feb 16 '12 at 18:29
  • Sorry typo. No you can do it in the code behind. Anything you can do in XAML you can do in the Code-Behind. Create the effect in code and apply the effect to the image – MyKuLLSKI Feb 16 '12 at 19:33
  • how can i do it? what should i put in the button? thanks for the help. badly needed. – mmmitchell Feb 16 '12 at 19:41
  • ShadderType shadder = new ShadderType(); ButtonName.Effect = shadder – MyKuLLSKI Feb 16 '12 at 19:43

1 Answers1

0

You can use the WriteableBitmap and manipulate it's pixels data in unsafe way, like here.

Then you can use

    public static void ToSepia(this WriteableBitmap wrb)
    {
        // ForEach...
        // (...ForEach(this WriteableBitmap bmp, Func<int, int, Color, Color> func)...)
        //
        wrb.ForEach((x, y, c) =>
        {
            // Convert color to grayscale.
            byte grayScale = (byte)((c.R * .3) + (c.G * .59) + (c.B * .11));
            // Init new color with taking same alpha.
            Color newColor = Color.FromArgb(c.A, grayScale, grayScale, grayScale);
            // Apply sepia and return new color.
            return new Color()
            {
                R = (byte)(newColor.R * 1),
                G = (byte)(newColor.G * 0.95),
                B = (byte)(newColor.B * 0.82),
            };
        });
    }

Here is a helper library for SL, but recently a wpf version is also made (Check in branches in source control). http://writeablebitmapex.codeplex.com/

Code0987
  • 2,598
  • 3
  • 33
  • 51