2008-12-02 131 views
8

我已經得到了重新調整,我認爲一些圖像C#代碼是很典型:形象的.Net調整與抗鋸齒

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); 

的問題是,得到的圖像清楚地別名,並轉移到InterpolationMode和SmoothingMode屬性似乎沒有區別。

任何指針?

回答

0

問題可能是另一個地方。我使用類似的代碼來調整圖像和它的作品不錯,但最大的不同是,當你將圖像保存您必須指定質量(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); 
2

抗鋸齒無關與光柵圖形。它只適用於矢量圖形。很明顯,圖像是一個光柵圖形。

您需要查看InterpolationMode。

+0

嗨,恕我直言,你的抗鋸齒定義並不完全正確。這不是一個光柵與矢量圖形的事情。 – 2008-12-02 11:22:28

18

它變成了錯誤的代碼。它實際上在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)); 

至於抗混疊得好,最重要的參數是graph.InterpolationMode

謝謝。