【问题标题】:Cropping image or scale down using c#使用 c# 裁剪图像或按比例缩小
【发布时间】:2017-02-22 13:22:16
【问题描述】:

我想在 c# 中即时裁剪图像。

我已经参考了一些链接并实现如下。

参考:

但我得到的是低质量的图像。我见过他们在他们的服务器上上传图片并在不损失质量的情况下缩小网站的网站

他们做得怎么样?为什么我们不能在 c# 中做?

代码

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

【问题讨论】:

  • 我过去遇到过这个问题并解决了,但我不记得我和 ATM 是如何工作的。我不知何故想说你可以将它转换为 JPEG,缩小它,然后再回到位图,因为我认为位图的缩放不是那么好......我什至不确定我是如何做到的,但感觉就像是我做到了。已经好几年了...除非您测试并看到它,否则我会调查它,这会有所帮助。
  • Resize an Image C#的可能重复
  • 裁剪不应损失质量。如果你缩小它,你总是会丢失信息。确保两张图片的相同的dpi设置!! - 见here for a similar problem。注意第二部分! - 还有see here about your pixelformat
  • 你试过了吗?使用 DrawImage 获得正确的结果需要这点:Bitmap resizedImage = new Bitmap(nnx, nny, PixelFormat.???); resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

标签: c# image


【解决方案1】:

您可以尝试net-vips,这是libvips 的C# 绑定。它是一个惰性、流式、需求驱动的图像处理库,因此无需加载整个图像即可完成此类操作。

例如,它带有一个方便的图像缩略图:

Image image = Image.Thumbnail("image.jpg", 300, 300);
image.WriteToFile("my-thumbnail.jpg");

【讨论】:

    【解决方案2】:

    您可以为此使用 imagemagic。它有自己的 dll 可以与 VS 一起使用。它还有很棒的论坛,您可以在其中找到各种示例。我用它做了一个程序来做到这一点。我花了大约半个小时(不是一个真正的大应用程序,它仅用于商业公司制作大量裁剪调整大小的图片(大约 4 TB 的图片哈哈))。 在这里你可以找到一些例子。

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

    【讨论】:

      【解决方案3】:

      ImageMagick 绝对是一个可行的解决方案,GraphicsMagick 或 libvips 也是如此(虽然我不确定 libvips 是否具有 C# 特定绑定,但它是 a lot faster than IM or GM)。

      您可能还想尝试 ImageKit 或其他类似的功能齐全的第三方图像优化和交付解决方案来为您完成所有这些调整。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 2019-03-13
        • 2012-01-07
        相关资源
        最近更新 更多