Paul Turner

Paul Turner

  • NA
  • 6
  • 3.6k

Crop image without pixel colour change

Oct 14 2011 1:09 PM
i have been using the following code to crop images:

public static System.Drawing.Image cropImage(System.Drawing.Image img, Rectangle cropArea)
{
 Bitmap bmpImage = new Bitmap(img);
 Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
 return (System.Drawing.Image)(bmpCrop);
}

which works fine except that the pixels RGB colour values change when you compare the cropped image to the original.  i need them to stay exactly the same. it seems that by default the new image created has a smaller colour palette, and i have found ways to get better results, but not a way which just gives the same colour palette and the same RGB values for the pixels in the new cropped image which you create. it seems crazy to me that you can't do this.

one way around this would be to avoid creating a new image but instead to actually crop the original image but again i cant find anything on this.

is there anyone really clever that can help me here?

Answers (6)