开发者_如何学运维I am searching for a way to resize and crop images to a specific size in a fixed ratio like 16:9. So I have any image and any size and it should be resized and cropped in a ratio 16:9.
Just cropping is not very nice. I want that the image is resized more and if necessary maybe cropped. Or better: I want to reuse as much as possible from the original image but resize and crop it centered so I could use it in a fixed html div with a ration of 16:9.
Thanks!
Here is a solution that I have used several times. This should resize first, then crop on whichever dimension has excess. The image will not be stretched.
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
protected Image imageCrop(Image image, int width, int height, AnchorPosition anchor)
{
int sourceWidth = image.Width;
int sourceHeight = image.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = (Convert.ToSingle(width) / Convert.ToSingle(sourceWidth));
nPercentH = (Convert.ToSingle(height) / Convert.ToSingle(sourceHeight));
if (nPercentH < nPercentW)
{
nPercent = nPercentW;
switch (anchor) {
case AnchorPosition.Top:
destY = 0;
break;
case AnchorPosition.Bottom:
destY = Convert.ToInt32(height - (sourceHeight * nPercent));
break;
default:
destY = Convert.ToInt32((height - (sourceHeight * nPercent)) / 2);
break;
}
}
else
{
nPercent = nPercentH;
switch (anchor) {
case AnchorPosition.Left:
destX = 0;
break;
case AnchorPosition.Right:
destX = Convert.ToInt32((width - (sourceWidth * nPercent)));
break;
default:
destX = Convert.ToInt32(((width - (sourceWidth * nPercent)) / 2));
break;
}
}
int destWidth = Convert.ToInt32((sourceWidth * nPercent));
int destHeight = Convert.ToInt32((sourceHeight * nPercent));
Bitmap bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(image, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
public enum Dimensions
{
Width,
Height
}
public enum AnchorPosition
{
Top,
Center,
Bottom,
Left,
Right
}
Here is an example of a call to the function:
Image image = image.FromFile(imagePath);
Image thumb = imageCrop(image, 100, 100, AnchorPosition.Top);
I did something similar to create a thumbnail of a standard size. Here is a link to the code (in a blog post).
http://blog.bobcravens.com/2009/07/mobileme-scrolling-image-viewer/
The concept is the same. I used some logic to automatically pick an area of the bitmap to crop based upon the dimensions.
Hope this helps.
Bob
You can use this code:
Size NewSize = new Size();
NewSize.Width = 70;
NewSize.Height = 60;
System.Drawing.Image small = resizeImage(Image.FromFile(source), NewSize);
small.Save(your path);
// and this is function for resizing
public System.Drawing.Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW) nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
精彩评论