2017-02-22 65 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或其他类似的全功能的第三方图像优化和交付解决方案,做到这一切调整大小为您服务。