Click here to Skip to main content
15,742,145 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i made a grayscale filter effect in wp8 and now i want to save it but my code is save the original image not the image changed to grayscale
this is my grayscale code
private WriteableBitmap GrayScale(BitmapImage bp)
    WriteableBitmap wb = new WriteableBitmap(bp);
    int[] grayPixel = new int[wb.PixelWidth * wb.PixelHeight];
    for (int i = 0; i < wb.Pixels.Length; i++)
        int pixel = wb.Pixels[i];
        int red = (pixel & 0x00FF0000) >> 16;
        int blue = (pixel & 0x0000FF00) >> 8;
        int green = (pixel & 0x000000FF);
        int average = (byte)((red + green + blue) / 3);
            grayPixel[i] = (int)((pixel & 0xFF000000) | average << 16 | average << 8 | average);
    Buffer.BlockCopy(grayPixel, 0, wb.Pixels, 0, (grayPixel.Length * 4));
    return wb;

and this is my save method
private void SaveImage(WriteableBitmap wb)
    wb = new WriteableBitmap(bp);
    var fileStream = new MemoryStream();
    fileStream.Position = 0;
    wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
    fileStream.Seek(0, SeekOrigin.Begin);
    var m = new MediaLibrary();
    m.SavePictureToCameraRoll("text.jpg", fileStream);

what should i do to it save my image after effect not original image
With Respect
hypermellow 10-Apr-15 11:09am    
How are you calling the SaveImage routine? and what happens when you comment out the line:
wb = new WriteableBitmap(bp);

If you pass in your Grayscaled image, when calling SaveImage ... then it looks like you are creating (and then saving) a new WriteableBitmap instead.
Sergey Alexandrovich Kryukov 10-Apr-15 13:13pm    
I think you can add it as an answer. This is all bloody mess. I explained it in further detail in my answer, please see. Your comment is credited, but your suggestion would not be a complete fix, there is more to it.

1 solution

In addition to what hypermellow told you:

The whole problem is passing the object bp. It looks accidental, but it's worse. The real abuse of technology is having this member. Apparently, in the context of your second method, "bp" really means this.bp if this is an instance field or property or, even worse YourDeclaringClass.bp is you happen to declare it static (I would really hope not).

In all cases, the big abuse is passing references through more global context than it is really required. Your bp should really be a local variable which could be passed as a parameter. What's worse, you combine passing the same reference (or seemingly the same) as a parameter and via the class instance's context. No wonder you end up with some bloody mess.

This is not about this particular bug, this is about your understanding of the process of code development. This is what you really have to fix.

Share this answer
Avenger1 10-Apr-15 13:46pm    
bp is "BitmapImage" and its local and first i open the image with "bp" then in another method i try to save it and this is what i m doing in winform and its correct but in wp8 it doesn't
Sergey Alexandrovich Kryukov 10-Apr-15 14:59pm    
No! "bp" in the first line of you implementation SaveImage (if it even compiles) tells the tale. You may have another "bp", but this one is the member, period.
If you are using Visual Studio, activate the context menu on this name and use "Go to Definition".
Avenger1 10-Apr-15 16:59pm    
thanks for helped me
but after i tried a lot to find whats problem, i fixed the problem
With Respect
hypermellow 13-Apr-15 3:49am    
A very relevant and detailed explanation ... nail, head , hit & 5ed :-)
Sergey Alexandrovich Kryukov 13-Apr-15 9:57am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900