【问题标题】:Image cropping losing quality MVC3 .Net C# Application图像裁剪失去质量 MVC3 .Net C# 应用程序
【发布时间】:2012-11-27 04:49:00
【问题描述】:

我在 MVC3 C# 应用程序中裁剪图像时遇到问题。我正在裁剪图像,但在视图中渲染时它的质量会下降。 要裁剪的图像是从数据库中加载的,并且是从 ByteArray 中创建的......

public static Image ByteArrayToImage(byte[] byteArrayIn)
{
   MemoryStream ms = new MemoryStream(byteArrayIn);
   Image returnImage = Image.FromStream(ms);
   return returnImage;
}

当此图像在视图上呈现时,它看起来不错,但质量不符合预期。 然后我选择要裁剪的区域并使用以下方法进行裁剪...

public Image CropImage(System.Drawing.Image Image, int Height, int Width, int StartAtX, int StartAtY)
        {
            Image outimage;
            MemoryStream mm = null;
            try
            {
                //check the image height against our desired image height
                if (Image.Height < Height)
                {
                    Height = Image.Height;
                }

                if (Image.Width < Width)
                {
                    Width = Image.Width;
                }

                //create a bitmap window for cropping
                Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(Image.VerticalResolution,Image.HorizontalResolution);

                //create a new graphics object from our image and set properties
                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

                //now do the crop
                grPhoto.DrawImage(Image, new Rectangle(0, 0, Width, Height), StartAtX, StartAtY, Width, Height, GraphicsUnit.Pixel);

                // Save out to memory and get an image from it to send back out the method.
                mm = new MemoryStream();
                bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                Image.Dispose();
                bmPhoto.Dispose();
                grPhoto.Dispose();
                outimage = Image.FromStream(mm);

                return outimage;
            }
            catch (Exception ex)
            {
                throw new Exception("Error cropping image, the error was: " + ex.Message);
            }
        }

这个裁剪后的图像然后被转换回 ByteArray 并且可以像这样保存到数据库中......

public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

稍后在视图上渲染此图像时,其质量比原始图像差很多。它看起来很像素化。 在这种情况下,原始图像是 .jpg,但可以是任何格式。

这是从数据库加载并裁剪后的图像...

这张图片是裁剪的结果。如您所见,它并不好。

我看过其他一些关于该主题的帖子,但它们没有帮助。任何人都可以提出解决方案吗?

谢谢

【问题讨论】:

标签: c# asp.net .net asp.net-mvc image-processing


【解决方案1】:

Gunnar Peipman 有一篇关于质量的博文,名为 Resizing images without loss of quality。 Gunnar 说了一些关于出了什么问题的技巧;

如果 Image 包含嵌入的缩略图,此方法 检索嵌入的缩略图并将其缩放到请求的大小。 如果 Image 不包含嵌入的缩略图,则此方法 通过缩放主图像创建缩略图。

GetThumbnailImage 方法在请求缩略图时效果很好 图像的大小约为 120 x 120 像素。如果您要求大 缩略图(例如,300 x 300)来自具有 嵌入的缩略图,可能会有明显的质量损失 缩略图。缩放主图像可能会更好(而不是 通过调用 DrawImage 方法来缩放嵌入的缩略图)。

razor 语法与 JCrop 友好,还有一个 cool blog post 关于如何使用它。

【讨论】:

    【解决方案2】:

    这是一个总结一切的好博客。我现在让它在我的应用程序中工作。 http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 2019-10-27
      • 1970-01-01
      • 2011-12-16
      相关资源
      最近更新 更多