views:

4585

answers:

4

I am trying to find out how to use FJCore to encode a WriteableBitmap to a jpeg. I understand that WriteableBitmap provides the raw pixels but I am not sure how to convert it to the format that FJCore expects for its JpegEncoder method. JpegEncoder has two overloads, one takes a FluxJpeg.Core.Image and the other takes in a DecodedJpeg.

I was trying to create a FluxJpeg.Core.Image but it expects a byte[][,] for the image data. byte[n][x,y] where x is width and y is height but I don't know what n should be.

I thought that n should be 4 since that would correspond to the argb info encoded in each pixel but when I tried that FJCore throws an argument out of range exception. Here is what I tried. Raster is my byte[4][x,y] array.

raster[0][x, y] = (byte)((pixel >> 24) & 0xFF);
raster[1][x, y] = (byte)((pixel >> 16) & 0xFF);
raster[2][x, y] = (byte)((pixel >> 8) & 0xFF);
raster[3][x, y] = (byte)(pixel & 0xFF);
A: 

Sounds like [n] should be the byte-array of the image, I have been looking into encoding WriteableBitmap into a JPEG and found the same library but have not looked into it in detail, but assume this would be the case, will add more later to this answer to see if this works, as I have not have the chance to try it yet. There will be some method to get the bytes of a WritableBitmap in Silverlight I guess as it is possible to save to other types.

RoguePlanetoid
+4  A: 

Figured it out! I downloaded FJCore from code.google.com and went through the image class. It only expects the RGB bytes. Here is the function that I wrote. I need the base64 version of the image so that's what my function returns.

    private static string GetBase64Jpg(WriteableBitmap bitmap)
    {
        int width = bitmap.PixelWidth;
        int height = bitmap.PixelHeight;
        int bands = 3;
        byte[][,] raster = new byte[bands][,];

        for (int i = 0; i < bands; i++)
        {
            raster[i] = new byte[width, height];    
        }

        for (int row = 0; row < height; row++)
        {
            for (int column = 0; column < width; column++)
            {
                int pixel = bitmap.Pixels[width * row + column];
                raster[0][column, row] = (byte)(pixel >> 16);
                raster[1][column, row] = (byte)(pixel >> 8);
                raster[2][column, row] = (byte)pixel;
            }
        }

        ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
        FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
        MemoryStream stream = new MemoryStream();
        JpegEncoder encoder = new JpegEncoder(img, 90, stream);
        encoder.Encode();

        stream.Seek(0, SeekOrigin.Begin);
        byte[] binaryData = new Byte[stream.Length];
        long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);

        string base64String =
                System.Convert.ToBase64String(binaryData,
                                              0,
                                              binaryData.Length);

        return base64String;
    }
RHLopez
Hope you don't mind but I have used your code sample and extend it in to a full article see: http://blog.blueboxes.co.uk/2009/07/21/rendering-xaml-to-a-jpeg-using-silverlight-3/ and attributed you correctly as under the CC licence
John
No problem. Glad someone is benefiting from this.
RHLopez
A: 

This Code does not work!

Roger
This response doesn't contribute anything. Please explain what exactly doesn't work and how it fails.
Sheeo
A: 

This code is fine and it should work. I am using same code to send image stream to server via web service and than regenerate image using these bytes...you can save these bytes to Db also

 [WebMethod]
 public string SaveImage(string data, string fileName)
 {
    byte[] imageBytes = System.Convert.FromBase64String(data);

    MemoryStream mem = new MemoryStream();
    mem.Write(imageBytes, 0, imageBytes.Length);

    System.Drawing.Image img = System.Drawing.Image.FromStream(mem);
    img.Save("D:\\FinalTest.jpg");      
    return "Saved !";
 }
Waqar Ahmad