这一切都在C#中:
我正在使用此代码调整图片大小:
_image = (Image)new Bitmap(_refImage, _width, _height);
_refImage只是一个参考图像,与原始图像相同,因此如果我多次调整大小,分辨率就不会消失。
如果我将图像做得更大,这段代码可以正常工作,它可以按原样拉伸它。
但是如果我将图像缩小,那么它就会切断边缘。
我只是调整宽度,因为我只想改变宽度。
答案 0 :(得分:1)
我找到了一个可能有用的链接:Here。希望有所帮助。
答案 1 :(得分:0)
试试这个:
/// <summary>
/// Scales to within given boundaries - Aspect ratio is kept. High Quality Bi-Cubic interpolation is used.
/// If boundary is larger than the image, then image is scaled up; if smaller, it is scaled down.
/// </summary>
/// <param name="originalImg">Image: Image to scale</param>
/// <param name="width">Int: Restriction on width for output size. Must be greater than zero</param>
/// <param name="height">Int: Restriction on height for output size. Must be greater than zero</param>
/// <param name="backgroundColour">Color: Colour to shade background behind image</param>
/// <returns>Image: Scaled Image</returns>
/// <exception cref="ArgumentException">[ArgumentException] Boundary dimensions must exceed zero</exception>
public static Image ScaleToFit(Image originalImg, int width, int height, Color backgroundColour)
{
if (originalImg == null) return null;
if (width < 1 || height < 1) throw new ArgumentException("ScaleToFit: Boundary dimensions must exceed zero.");
var destX = 0;
var destY = 0;
float nPercent;
var nPercentW = (width / (float)originalImg.Width);
var nPercentH = (height / (float)originalImg.Height);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = Convert.ToInt16((width - (originalImg.Width * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = Convert.ToInt16((height - (originalImg.Height * nPercent)) / 2);
}
var destWidth = (int)(originalImg.Width * nPercent);
var destHeight = (int)(originalImg.Height * nPercent);
var bmPhoto = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
var grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(backgroundColour);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(originalImg,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(0, 0, originalImg.Width, originalImg.Height),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
注意:这样可以保持纵横比,如果你想轻松地改变它,可以改变它。