2017-02-22 66 views
0

我想在c#中動態地裁剪圖像。使用c裁剪圖像或縮小比例#

我已經提到了一些鏈接和實現如下。

參考:

但我越來越低質量的圖像。我在他們的服務器看到的網站,他們所上傳圖片和縮小網頁不失品質

他們是如何做呢?爲什麼我們不能在c#中完成?

CODE

public static Image ScaleImage(Image image, int width, int height) 
     { 
      if (image.Height < height && image.Width < width) return image; 
      using (image) 
      { 
       double xRatio = (double)image.Width/width; 
       double yRatio = (double)image.Height/height; 
       double ratio = Math.Max(xRatio, yRatio); 
       int nnx = (int)Math.Floor(image.Width/xRatio); 
       int nny = (int)Math.Floor(image.Height/yRatio); 
       Bitmap resizedImage = new Bitmap(nnx, nny, PixelFormat.Format64bppArgb); 
       using (Graphics graphics = Graphics.FromImage(resizedImage)) 
       { 
        graphics.Clear(Color.Transparent); 

        // This is said to give best quality when resizing images 
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
        graphics.SmoothingMode = SmoothingMode.HighQuality; 
        graphics.CompositingQuality = CompositingQuality.HighQuality; 
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 

        graphics.DrawImage(image, 
         new Rectangle(0, 0, nnx, nny), 
         new Rectangle(0, 0, image.Width, image.Height), 
         GraphicsUnit.Pixel); 
       } 
       return resizedImage; 
      } 
     } 
+0

我以前有過這個問題並解決了它,但我不記得自動櫃員機和我在工作的方式。我不知道怎麼說,你可以將它轉換爲JPEG,縮小它,然後回到位圖,因爲我認爲位圖的縮放比較不好......我不確定這甚至是我做的,但它感覺像是什麼我做到了。已經有幾年了......除非你測試並看到它,否則這將有所幫助。 –

+0

[調整圖像C#]的可能的重複(http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp) – Nino

+0

裁剪不應該失去質量。如果你縮小它,你將永遠失去信息。請務必爲這兩個圖像設置__same dpi__設置! - 請參閱[這裏是一個類似的問題](http://stackoverflow.com/questions/26612057/where-does-this-quality-loss-on-images-come-from/26614735?s=6|0.0626#26614735) 。注意第二部分! - 也[見這裏關於你的像素格式](http://stackoverflow.com/questions/34590509/pixelformat-format32bppargb-vs-pixelformat-format64bppargb) – TaW

回答

0

你可以使用imagemagic了點。它有自己的DLL與VS一起工作。它也有很棒的論壇,你可以找到各種各樣的例子。我用它做了一個程序來做到這一點。它花了我大約半小時(不是一個真正的大應用程序,它只用於商業公司製作大量裁剪後調整大小的圖像(約4 terrabyte的圖片哈哈))。 在這裏你可以找到一些例子。

https://www.imagemagick.org/script/index.php

0

ImageMagick的肯定是一個可行的解決方案,所以是GraphicsMagick工具或libvips(雖然我不知道是否有libvips C#特定的綁定,但它是a lot faster than IM or GM)。

你也可以試試ImageKit或其他類似的全功能的第三方圖像優化和交付解決方案,做到這一切調整大小爲您服務。