public static Image CutImage(Image img, int width, int height)
{
Bitmap image = new Bitmap(img);
int calheight = width * image.Height / image.Width;
int calwidth = height * image.Width / image.Height;
Image img2;
if (calheight >= height)
{
img2 = image.GetThumbnailImage(width, calheight, null, IntPtr.Zero);
}
else
{
img2 = image.GetThumbnailImage(calwidth, height, null, IntPtr.Zero);
}
Bitmap bitmap = new Bitmap(img2);
int x = (bitmap.Width - width) / 2;
int y = (bitmap.Height - height) / 2;
Rectangle cloneRect = new Rectangle(x, y, width, height);
PixelFormat format = bitmap.PixelFormat;
Bitmap cloneBitmap = bitmap.Clone(cloneRect, format);
return cloneBitmap;
}
{
Bitmap image = new Bitmap(img);
int calheight = width * image.Height / image.Width;
int calwidth = height * image.Width / image.Height;
Image img2;
if (calheight >= height)
{
img2 = image.GetThumbnailImage(width, calheight, null, IntPtr.Zero);
}
else
{
img2 = image.GetThumbnailImage(calwidth, height, null, IntPtr.Zero);
}
Bitmap bitmap = new Bitmap(img2);
int x = (bitmap.Width - width) / 2;
int y = (bitmap.Height - height) / 2;
Rectangle cloneRect = new Rectangle(x, y, width, height);
PixelFormat format = bitmap.PixelFormat;
Bitmap cloneBitmap = bitmap.Clone(cloneRect, format);
return cloneBitmap;
}