2010-06-01 32 views
0

我調整了超過最大大小的圖像大小。方法我試過至今都不夠好:-(圖像大小調整的最佳選項

  1. System.Drawing.Image.GetThumbnailImage一般產生很差的圖像質量。
  2. 像這樣one選項我可以生成質量,但更重比原來更好的圖像播放。

也許是第二個選項(或類似的東西)是最好的選擇,我會需要用合適的選項來調整。

有什麼建議?

編輯
我的選項2正在生成某些特定圖片較重的圖像。一般情況下按預期工作,所以我會說這是解決的。

回答

3

嘗試是這樣的:

public Bitmap Resize(Bitmap originalImage, int newWidth) 
{ 
    int newHeight = (int)Math.Round(originalImage.Height * (decimal)newWidth/originalImage.Width, 0); 
    var destination = new Bitmap(newWidth, newHeight); 
    using (Graphics g = Graphics.FromImage(destination)) 
    { 
     g.SmoothingMode = SmoothingMode.AntiAlias; 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
     g.DrawImage(originalImage, 0, 0, newWidth, newHeight); 
    } 
    return destination; 
} 
+0

謝謝OrbMan。剛剛測試過它。不幸的是,我的問題與我的選擇相同。2.縮小圖像比原始圖像重:-( – 2010-06-01 20:46:40

+0

它們被檢索爲/保存在什麼格式?如果將縮略圖保存爲位圖,我不會感到驚訝如果它們很大,請確保使用jpeg或png或其他壓縮格式 – 2010-06-01 20:55:37

+0

@Sean:相同的格式我檢索相同的格式我保存我正在處理任何類型的圖像 – 2010-06-01 21:00:50

2

創建一個新的Bitmap對象,然後使用Graphics對象,根據所需的調整大小引擎將舊圖像以增加/減小的大小重新繪製到新圖像的緩衝區中。

// inImage is your original 
Bitmap outImage = new Bitmap(newWid, newHei); 

Graphics gfx = Graphics.FromImage(outImage); 
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 

gfx.DrawImage(inImage, 
    new Rectangle(0, 0, newWid, new Hei), 
    new Rectangle(0, 0, inImage.Width, inImage.Height), 
    GraphicsUnit.Pixel); 
+0

感謝AREN。這與OrbMan發佈的代碼非常相似,我的問題是它會生成比原始版本更重的圖像 – 2010-06-01 20:49:10

+0

這是因爲默認格式可能是BMP格式。您可以使用ImageFormatter將它「保存」到內存流中,並使用png/jpg壓縮圖像數據。 – Aren 2010-06-01 23:45:40