Reputation: 47945
Suppose I have a 400x300px image, and I'd like to cut it as 200x200px centering it, on server side (C#, .NET 4.0).
How can I do it? Use a sort of canvas and move it? Any tutorial/code example/suggestions?
Upvotes: 3
Views: 5514
Reputation: 159
This method will save the image cropped at center if needed:
bool SaveCroppedImage(Image image, int targetWidth, int targetHeight, string filePath)
{
ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/jpeg").First();
Image finalImage = image;
System.Drawing.Bitmap bitmap = null;
try
{
int left = 0;
int top = 0;
int srcWidth = targetWidth;
int srcHeight = targetHeight;
bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
double croppedHeightToWidth = (double)targetHeight / targetWidth;
double croppedWidthToHeight = (double)targetWidth / targetHeight;
if (image.Width > image.Height)
{
srcWidth = (int)(Math.Round(image.Height * croppedWidthToHeight));
if (srcWidth < image.Width)
{
srcHeight = image.Height;
left = (image.Width - srcWidth) / 2;
}
else
{
srcHeight = (int)Math.Round(image.Height * ((double)image.Width / srcWidth));
srcWidth = image.Width;
top = (image.Height - srcHeight) / 2;
}
}
else
{
srcHeight = (int)(Math.Round(image.Width * croppedHeightToWidth));
if (srcHeight < image.Height)
{
srcWidth = image.Width;
top = (image.Height - srcHeight) / 2;
}
else
{
srcWidth = (int)Math.Round(image.Width * ((double)image.Height / srcHeight));
srcHeight = image.Height;
left = (image.Width - srcWidth) / 2;
}
}
using (Graphics g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(left, top, srcWidth, srcHeight), GraphicsUnit.Pixel);
}
finalImage = bitmap;
}
catch { }
try
{
using (EncoderParameters encParams = new EncoderParameters(1))
{
encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);
//quality should be in the range [0..100] .. 100 for max, 0 for min (0 best compression)
finalImage.Save(filePath, jpgInfo, encParams);
return true;
}
}
catch { }
if (bitmap != null)
{
bitmap.Dispose();
}
return false;
}
Upvotes: 0
Reputation: 130
Try something like this:
Bitmap sourceImage = ...;
int targetWidth = 200;
int targetHeight = 200;
int x = sourceImage.Width / 2 - targetWidth / 2;
int y = sourceImage.Height / 2 - targetHeight / 2;
Rectangle cropArea =
new Rectangle(x, y, targetWidth, targetHeight);
Bitmap targetImage =
sourceImage.Clone(cropArea, sourceImage.PixelFormat);
If the source image is smaller than the target image size, this will obviously fail, but you get the idea.
Upvotes: 5
Reputation: 19956
Create NEW Bitmap object, with the destination size.
Create Graphics object around that bitmap.
On that Graphcs object, call DrawImage() with proper parameters and that will cut out proper segment from the first picture.
Code will be something like:
Bitmap dstBitmap=new Bitmap(200, 200);
using (Graphics g=Graphics.FromImage(dstBitmap))
{
srcBitmap.DrawImage(dstBitmap, /* cropping parameters here */);
}
// at the end you'll have your bitmap in dstBitmap, ...
I didn't include literal parameters for the methods, use intellisense and manual to figure them out.
Upvotes: -1