简体   繁体   English

图像裁剪质量下降MVC3 .Net C#应用

[英]Image cropping losing quality MVC3 .Net C# Application

I'm having trouble cropping an image in an MVC3 C# application. 我在MVC3 C#应用程序中裁剪图像时遇到问题。 I am cropping the image but its losing quality when rendered back in a view. 我正在裁剪图像,但是在视图中渲染时会失去图像质量。 The image to be cropped is loaded from a database and is created from a ByteArray like so... 要裁剪的图像是从数据库加载的,是从ByteArray创建的,就像这样...

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

When this image is rendered on a view it looks fine and is off the expected quality. 在视图上渲染该图像时,它看起来很好,并且超出了预期的质量。 I am then selecting a region to crop and using the below method to do the crop... 然后,我选择要裁剪的区域并使用以下方法进行裁剪...

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

This cropped image is then converted back to a ByteArray and can be saved into a database like so... 然后将裁剪后的图像转换回ByteArray,并可以将其保存到数据库中,如下所示:

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

When this image is later rendered on the view its quality is a lot worse than the original image. 当稍后在视图上渲染此图像时,其质量比原始图像差很多。 It looks quite pixilated. 它看起来很像。 The original image in this case is .jpg but could be any format. 在这种情况下,原始图像为.jpg,但可以为任何格式。

This is an image of the image after it has been loaded from the database and being cropped... 这是从数据库加载并裁剪后的图像的图像。

预先裁剪的图像

This image is the result of the crop. 此图像是裁剪的结果。 As you can see its not good. 如您所见,它不是很好。

在此处输入图片说明

I have seen some other posts on the topic but they haven't helped. 我看过有关该主题的其他文章,但没有帮助。 Can anyone suggest a solution? 谁能提出解决方案?

Thank you 谢谢

This is a great blog that sums it all up. 这是一个很棒的博客,总结了所有内容。 I have it working in my application now. 我现在可以在我的应用程序中使用它了。 http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/ http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/

Gunnar Peipman has a blog post about the quality called Resizing images without loss of quality . 贡纳尔·皮普曼(Gunnar Peipman)在博客上发表了有关质量的文章,名为“ 调整图像大小而又不损失质量” Gunnar says some trick about what is going wrong; 贡纳尔(Gunnar)对出了什么问题说了一些技巧

If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. 如果Image包含嵌入的缩略图图像,则此方法将检索嵌入的缩略图并将其缩放到请求的大小。 If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image. 如果图像不包含嵌入的缩略图,则此方法通过缩放主图像来创建缩略图。

The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. 当请求的缩略图图像的大小约为120 x 120像素时, GetThumbnailImage方法效果很好。 If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. 如果从嵌入了缩略图的图像中请求较大的缩略图(例如300 x 300),则缩略图中的图像质量可能会明显下降。 It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method. 通过调用DrawImage方法来缩放主图像(而不是缩放嵌入的缩略图)可能会更好。

And razor syntax is amicable with JCrop and there is a cool blog post about how you can use it. 剃刀语法与JCrop友好,并且有很酷的博客文章介绍了如何使用它。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM