raptor00

raptor00
- Name: [not set]
- Favorite Languages: [not set]
- Website: [not set]
- Location: [not set]
- About Me: [not set]
-
C# Tutorial - Image Editing: Saving, Cropping, and Resizing
06/01/2011 - 10:04
First off, just wanna say great article.
I've implemented a different cropping method using unmanaged code that is much faster than Bitmap.Clone.
It's about 2x faster for cropping large areas >1000x1000px and 100x faster for small areas <200x200px.[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
private static unsafe extern void MoveMemory(void* dest, void* src, int size);
public static Bitmap FastCrop(Bitmap image, Rectangle area)
{
//Create the new bitmap
Bitmap result = new Bitmap(area.Width,
area.Height, image.PixelFormat);
unsafe
{
//lock the bits for both bitmaps to
//allow direct access by pointer
BitmapData bmd = image.LockBits(area,
ImageLockMode.ReadOnly, image.PixelFormat);
BitmapData bmdResult = result.LockBits(
new Rectangle(0, 0, area.Width, area.Height),
ImageLockMode.WriteOnly, result.PixelFormat);
int pixelSize =
Image.GetPixelFormatSize(image.PixelFormat) / 8;
//Copy the memory from each row in
//the first bitmap to second
for (int y = 0; y < bmd.Height; y++)
{
byte* to = (byte*)bmdResult.Scan0
+ (y * bmdResult.Stride);
byte* from = (byte*)bmd.Scan0 + (y * bmd.Stride);
MoveMemory(to, from, area.Width * pixelSize);
}
image.UnlockBits(bmd);
result.UnlockBits(bmdResult);
}
return result;
}
Recent Comments