我正在尝试调整图像大小的功能。
public static Bitmap FixedSize(Bitmap imgPhoto, int Width, int Height, InterpolationMode im)
{
if ((Width == 0) && (Height == 0))
return imgPhoto;
if ((Width < 0) || (Height < 0))
return imgPhoto;
int destWidth = Width;
int destHeight = Height;
int srcWidth = imgPhoto.Size.Width;
int srcHeight = imgPhoto.Size.Height;
if (Width == 0)
destWidth = (int)(((float)Height / (float)srcHeight) * (float)srcWidth);
if (Height == 0)
destHeight = (int)(((float)Width / (float)srcWidth) * (float)srcHeight);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = im;
grPhoto.DrawImage(imgPhoto,
new Rectangle(new Point(0, 0), new Size(destWidth, destHeight)),
new Rectangle(0, 0, srcWidth, srcHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return new Bitmap(bmPhoto);
}
当我调试代码时,所有数字似乎都没问题但是当我保存图像时,它在左边和顶部边框上都有一条白线。知道什么应该是错的吗?我试图搜索,我使用完全相同的代码,它应该工作,但线仍然存在。
感谢。
答案 0 :(得分:0)
public static Bitmap ResizeImage(Bitmap image, int percent)
{
try
{
int maxWidth = (int)(image.Width * (percent * .01));
int maxHeight = (int)(image.Height * (percent * .01));
Size size = GetSize(image, maxWidth, maxHeight);
Bitmap newImage = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
SetGraphics(image, size, newImage);
return newImage;
}
finally { }
}
public static void SetGraphics(Bitmap image, Size size, Bitmap newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, size.Width, size.Height);
}
}
标记这一点,如果它对你有所帮助。