1

我在 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,但可以是任何格式。

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

预裁剪图像

此图像是裁剪的结果。如您所见,它并不好。

在此处输入图像描述

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

谢谢

4

2 回答 2

1

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

于 2012-12-09T20:46:27.620 回答
0

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

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

GetThumbnailImage当请求的缩略图图像的大小约为 120 x 120 像素时,该方法效果很好。如果您从具有嵌入式缩略图的图像中请求较大的缩略图图像(例如,300 x 300),则缩略图图像的质量可能会明显下降。通过调用 DrawImage 方法来缩放主图像(而不是缩放嵌入的缩略图)可能会更好。

razor 语法与JCrop友好,有一篇很酷的博客文章介绍了如何使用它。

在此处输入图像描述

于 2012-12-09T17:26:40.470 回答