Image cropping C# without .net library

♀尐吖头ヾ 提交于 2019-12-10 14:21:05

问题


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 :(


回答1:


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

http://web.archive.org/web/20140304090029/http://code.google.com:80/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




回答2:


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.)




回答3:


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




回答4:


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..)



来源:https://stackoverflow.com/questions/37048/image-cropping-c-sharp-without-net-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!