views:

3043

answers:

4

Can anyone advise on how to crop an image, let's say jpeg, without using any .NET framework constructs, just raw bytes? Since this is the only* way in Silverlight...

Or point to a library?

I'm not concerned with rendering i'm wanting to manipulate a jpg before uploading.

*There are no GDI+(System.Drawing) or WPF(System.Windows.Media.Imaging) libraries available in Silverlight.

Lockbits requires GDI+, clarified question

Using fjcore: http://code.google.com/p/fjcore/ to resize but no way to crop :(

+2  A: 

ImageMagick does a pretty good job. If you're ok with handing off editing tasks to your server...

(Seriously? The recommended way of manipulating images in Silverlight is to work with raw bytes? That's... incredibly lame.)

Shog9
A: 

Hello boys.. where is silverlight executed? Is there any reason at all to send an complete picture to the client to make the client crop it? Do it on the server... (if you are not creating an image editor that is..)

neslekkiM
+2  A: 

I'm taking a look at : http://code.google.com/p/fjcore/source/checkout A dependency free image processing library.

Brian Leahy
+2  A: 

You could easily write crop yourself in fjcore. Start with the code for Resizer

http://code.google.com/p/fjcore/source/browse/trunk/FJCore/Resize/ImageResizer.cs

and FilterNNResize -- you can see how the image data is stored -- it's just simple arrays of pixels.

The important part is:

    for (int y = 0; y < _newHeight; y++)
    {
        i_sY = (int)sY; sX = 0;

        UpdateProgress((double)y / _newHeight);

        for (int x = 0; x < _newWidth; x++)
        {
            i_sX = (int)sX;

            _destinationData[0][x, y] = _sourceData[0][i_sX, i_sY];

            if (_color) {

                _destinationData[1][x, y] = _sourceData[1][i_sX, i_sY];
                _destinationData[2][x, y] = _sourceData[2][i_sX, i_sY];
            }

            sX += xStep;
        }
        sY += yStep;
    }

shows you that the data is stored in an array of color planes (1 element for 8bpp gray, 3 elements for color) and each element has a 2-D array of bytes (x, y) for the image.

You just need to loop through the destination pixels, copying then from the appropriate place in the source.

edit: don't forget to provide the patch to the author of fjcore

Lou Franco