使用抗锯齿功能在.Net中调整图像大小

时间:2023-02-07 23:41:15

I've got some C# code that resizes images that I think is pretty typical:

我有一些C#代码调整了我认为非常典型的图像大小:

Bitmap bmp = new Bitmap(image, new Size(width, height));
Graphics graphics = Graphics.FromImage(bmp);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawImage(bmp, width, height);

The problem is that the resultant images are clearly aliased and changes to the InterpolationMode and SmoothingMode properties seem to make no difference.

问题是结果图像明显有别名,并且对InterpolationMode和SmoothingMode属性的更改似乎没有区别。

Any pointers?

5 个解决方案

#1


18  

It turns the code was just wrong. It was actually resizing the image without interpolation in the Bitmap constructor, and then attempting to smoothly resize that version to the size it was already at. Here is the amended code:

它变成了代码错误。它实际上是在Bitmap构造函数中没有插值的情况下调整图像大小,然后尝试将该版本平滑地调整到它已经存在的大小。这是修改后的代码:

Bitmap bmp = new Bitmap(width, height);
Graphics graph = Graphics.FromImage(bmp);
graph.InterpolationMode = InterpolationMode.High;
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.AntiAlias;
graph.DrawImage(image, new Rectangle(0, 0, width, height));

As far as anti-aliasing goes, the most important parameter is graph.InterpolationMode.

就抗锯齿而言,最重要的参数是graph.InterpolationMode。

Thanks.

#2


2  

Try graphics.DrawImage(bmp, 0, 0, width, height); Also check this MSDN Article on interpolation.

尝试graphics.DrawImage(bmp,0,0,width,height);还要检查这篇有关插值的MSDN文章。

#3


2  

Anti-aliasing has nothing to do with raster graphics. It's only applicable to vector graphics. Obviously, an image is a raster graphic.

消除锯齿与光栅图形无关。它仅适用于矢量图形。显然,图像是光栅图形。

You need to look at InterpolationMode.

您需要查看InterpolationMode。

#4


0  

The problem might be another place. I use similar code to resize images and it works ok, but the biggest difference is that when you save the image you have to specify quality (jpeg):

问题可能是另一个地方。我使用类似的代码来调整图像大小,它可以正常工作,但最大的区别是,当你保存图像时,你必须指定质量(jpeg):

ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo codec = null;
for (int i = 0; i<codecs.Length;i++)
{
  if(codecs[i].MimeType.Equals("image/jpeg"))
    codec = codecs[i];
}

EncoderParameters encoderParametersInstance = null;

if (codec!=null)
{
  Encoder encoderInstance=Encoder.Quality;
  encoderParametersInstance = new EncoderParameters(2);
  //100% quality, try different values, around 80-90 gives good results.
  EncoderParameter encoderParameterInstance=new EncoderParameter(encoderInstance, 100L);
  encoderParametersInstance.Param[0]=encoderParameterInstance;
  encoderInstance=Encoder.ColorDepth;
  encoderParameterInstance=new EncoderParameter(encoderInstance, 24L);
  encoderParametersInstance.Param[1]=encoderParameterInstance;
}

MemoryStream ms = new MemoryStream();
resizedImage.Save(ms, codec, encoderParametersInstance);

#5


0  

There is an article on CodeProject describing an improved antialiasing method:

CodeProject上有一篇文章描述了一种改进的抗锯齿方法:

http://www.codeproject.com/KB/GDI-plus/AntiAliasingIssues.aspx

#1


18  

It turns the code was just wrong. It was actually resizing the image without interpolation in the Bitmap constructor, and then attempting to smoothly resize that version to the size it was already at. Here is the amended code:

它变成了代码错误。它实际上是在Bitmap构造函数中没有插值的情况下调整图像大小,然后尝试将该版本平滑地调整到它已经存在的大小。这是修改后的代码:

Bitmap bmp = new Bitmap(width, height);
Graphics graph = Graphics.FromImage(bmp);
graph.InterpolationMode = InterpolationMode.High;
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.AntiAlias;
graph.DrawImage(image, new Rectangle(0, 0, width, height));

As far as anti-aliasing goes, the most important parameter is graph.InterpolationMode.

就抗锯齿而言,最重要的参数是graph.InterpolationMode。

Thanks.

#2


2  

Try graphics.DrawImage(bmp, 0, 0, width, height); Also check this MSDN Article on interpolation.

尝试graphics.DrawImage(bmp,0,0,width,height);还要检查这篇有关插值的MSDN文章。

#3


2  

Anti-aliasing has nothing to do with raster graphics. It's only applicable to vector graphics. Obviously, an image is a raster graphic.

消除锯齿与光栅图形无关。它仅适用于矢量图形。显然,图像是光栅图形。

You need to look at InterpolationMode.

您需要查看InterpolationMode。

#4


0  

The problem might be another place. I use similar code to resize images and it works ok, but the biggest difference is that when you save the image you have to specify quality (jpeg):

问题可能是另一个地方。我使用类似的代码来调整图像大小,它可以正常工作,但最大的区别是,当你保存图像时,你必须指定质量(jpeg):

ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo codec = null;
for (int i = 0; i<codecs.Length;i++)
{
  if(codecs[i].MimeType.Equals("image/jpeg"))
    codec = codecs[i];
}

EncoderParameters encoderParametersInstance = null;

if (codec!=null)
{
  Encoder encoderInstance=Encoder.Quality;
  encoderParametersInstance = new EncoderParameters(2);
  //100% quality, try different values, around 80-90 gives good results.
  EncoderParameter encoderParameterInstance=new EncoderParameter(encoderInstance, 100L);
  encoderParametersInstance.Param[0]=encoderParameterInstance;
  encoderInstance=Encoder.ColorDepth;
  encoderParameterInstance=new EncoderParameter(encoderInstance, 24L);
  encoderParametersInstance.Param[1]=encoderParameterInstance;
}

MemoryStream ms = new MemoryStream();
resizedImage.Save(ms, codec, encoderParametersInstance);

#5


0  

There is an article on CodeProject describing an improved antialiasing method:

CodeProject上有一篇文章描述了一种改进的抗锯齿方法:

http://www.codeproject.com/KB/GDI-plus/AntiAliasingIssues.aspx