使用以下代码重新调整图像大小
using (Image thumbnail = new Bitmap(100, 50))
{
using (Bitmap source = new Bitmap(imageFile))
{
using (Graphics g = Graphics.FromImage(thumbnail))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(source, 0, 0, 100, 50);
}
}
using (MemoryStream ms = new MemoryStream())
{
thumbnail.Save(ms, ImageFormat.Png);
thumbnail.Save(dest, ImageFormat.Png);
}
}
但它没有给出任何质量的图像。像素化正在使图像连线。
我也试过了代码
但是我得到一个黑屏,而不是jpg,使用png是唯一的区别。
任何改善图像质量的建议。我必须将透明图像的大小调整为100 x50的大小。
提前致谢。
答案 0 :(得分:2)
试试这个,假设你可以使用它
public static Image Resize(Image originalImage, int w, int h)
{
//Original Image attributes
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
// Figure out the ratio
double ratioX = (double)w / (double)originalWidth;
double ratioY = (double)h / (double)originalHeight;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
Image thumbnail = new Bitmap(newWidth, newHeight);
Graphics graphic = Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.Clear(Color.Transparent);
graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight);
return thumbnail;
}