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?
Loading
Javeed M ShaikhPosted Oct 15, 2011, 10:44 PM
http://imageresizing.net/
http://getlara.com/north-america/canada/alberta/edmonton/post/2008/10/13/png-jpg-gif-image-resize-with-net-with-transparency
http://glennjones.net/2005/10/high-quality-dynamically-resized-images-with-dot-net/
Javeed M ShaikhPosted Oct 15, 2011, 9:15 PM
Let me look into other options for this, also did you try using the PixelFormat option while creating the new Bitmap?
not sure if this helps.
http://www.gutgames.com/post/Getting-an-HTML-Based-Color-Palette-from-an-Image-in-C.aspx
Paul TurnerPosted Oct 15, 2011, 8:25 AM
the two ways i can see to solve the problems would be;
1. copy the colour palette from the original image to the new one,
or
2. crop the original image - do not create a new cropped image from it,
i cant see how to do either of the above though!
Paul TurnerPosted Oct 15, 2011, 8:12 AM
thanks for your quick response. unfortunately this doesn't seem to do any different, the pixels still change colour. i had better show you the other part of the code which i use to call this method in case you think the problem lies here:
System.Drawing.Image i = System.Drawing.Image.FromFile(path);
i = SupersizePrint.DB.cropImage(i, CropRectangle);
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with quality level 100.
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
i.Save(path, myImageCodecInfo, myEncoderParameters);
i.Dispose();
Prabhu RajaPosted Oct 15, 2011, 1:00 AM
Javeed M ShaikhPosted Oct 14, 2011, 3:46 PM
can you try this:
public static System.Drawing.Image cropImage(System.Drawing.Image img, Rectangle cropArea)
{
Bitmap target = new Bitmap(cropArea.Width, cropArea.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, target.Width, target.Height), cropArea, GraphicsUnit.Pixel);
}
return img;
}
Please do not forget to mark "Accepted Answer".