我正在寻找一种方法来调整大小,并以16:9的固定比例将图像裁剪为特定大小。所以我有任何图像和任何尺寸,它应该调整大小并以16:9的比例裁剪。
刚刚裁剪不是很好。我想要更多地调整图像大小,如果需要可能会裁剪。或者更好:我想尽可能多地从原始图像重用,但调整大小并将其裁剪为中心,这样我就可以在一个固定的html div中使用它,比例为16:9。
谢谢!
答案 0 :(得分:3)
这是我多次使用的解决方案。这应首先调整大小,然后在任何超出的维度上裁剪。图像不会被拉伸。
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
}
以下是对函数调用的示例:
Image image = image.FromFile(imagePath);
Image thumb = imageCrop(image, 100, 100, AnchorPosition.Top);
答案 1 :(得分:1)
我做了类似的事情来创建标准尺寸的缩略图。这是代码的链接(在博客文章中)。
http://blog.bobcravens.com/2009/07/mobileme-scrolling-image-viewer/
概念是一样的。我使用了一些逻辑来根据尺寸自动选择要裁剪的位图区域。
希望这有帮助。
鲍勃
答案 2 :(得分:1)
您可以使用此代码:
Size NewSize = new Size();
NewSize.Width = 70;
NewSize.Height = 60;
System.Drawing.Image small = resizeImage(Image.FromFile(source), NewSize);
small.Save(your path);
//这是调整大小的功能
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;
}