图像裁剪质量下降MVC3.Net C#应用程序
我在MVC3 C应用程序中裁剪图像时遇到问题。我正在裁剪图像,但在视图中渲染时图像质量会下降。 要裁剪的图像是从数据库加载的,并且是从ByteArray创建的,就像这样图像裁剪质量下降MVC3.Net C#应用程序,c#,asp.net,.net,asp.net-mvc,image-processing,C#,Asp.net,.net,Asp.net Mvc,Image Processing,我在MVC3 C应用程序中裁剪图像时遇到问题。我正在裁剪图像,但在视图中渲染时图像质量会下降。 要裁剪的图像是从数据库加载的,并且是从ByteArray创建的,就像这样 public static Image ByteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return
public static Image ByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
在视图上渲染此图像时,它看起来很好,并且质量不符合预期。
然后我选择一个区域进行裁剪,并使用下面的方法进行裁剪
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);
}
}
稍后在视图上渲染此图像时,其质量比原始图像差得多。它看起来很生动。
本例中的原始图像是.jpg,但可以是任何格式
这是图像从数据库加载并被裁剪后的图像
此图像是裁剪的结果。正如你所看到的,这并不好
我看过其他一些关于这个话题的帖子,但都没用。有人能提出解决办法吗
谢谢Gunnar Peipman有一篇关于质量的博客文章,名为。Gunnar说了一些关于出错原因的技巧 如果图像包含嵌入的缩略图图像,则此方法 检索嵌入的缩略图并将其缩放到请求的大小。 如果图像不包含嵌入的缩略图图像,则此方法 通过缩放主图像来创建缩略图
GetThumbnailImage
方法在请求的缩略图
图像的大小约为120 x 120像素。如果你要求一个大的
来自具有
嵌入缩略图后,图像质量可能会明显下降
缩略图。最好是缩放主图像(而不是
通过调用DrawImage方法缩放嵌入的缩略图)
razor语法是友好的,并且有一个关于如何使用它的问题
Gunnar Peipman有一篇关于质量的博客文章,名为。Gunnar说了一些关于出错原因的技巧 如果图像包含嵌入的缩略图图像,则此方法 检索嵌入的缩略图并将其缩放到请求的大小。 如果图像不包含嵌入的缩略图图像,则此方法 通过缩放主图像来创建缩略图
GetThumbnailImage
方法在请求的缩略图
图像的大小约为120 x 120像素。如果你要求一个大的
来自具有
嵌入缩略图后,图像质量可能会明显下降
缩略图。最好是缩放主图像(而不是
通过调用DrawImage方法缩放嵌入的缩略图)
razor语法是友好的,并且有一个关于如何使用它的问题
这是一个伟大的博客,总结了这一切。我现在在我的应用程序中使用它 这是一个很好的博客,总结了这一切。我现在在我的应用程序中使用它 NET用于保存JPEG的默认质量非常糟糕,您可以使用此重载指定自己的质量级别:我找到了一个有效的解决方案。这是一个很棒的博客,它总结了.NET用来保存JPEG的默认质量非常糟糕,你可以使用这个重载来指定你自己的质量级别:我找到了一个有效的解决方案。这是一个伟大的博客,总结了它