图片上传没有质量损失 [英] Image upload without loss of quality

查看:136
本文介绍了图片上传没有质量损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在站点的用户库,它可以为游客上传一些图片。后上传图像应该被调整到一些predefined preset中。除了原有的图像应保存了。所有的 PNG BMP 图像格式正常工作。但是,如果我上传 GIF 格式或 JPEG 与上传的原始图像中的$ P $的一种颜色pdominance似乎是COM pressed。

I have a user gallery at the site and it is possible for visitors to upload some images. After upload image should be resized to some predefined presets. In addition original image should be saved too. All works fine for png and bmp image formats. But if I upload gif format or jpeg with a predominance of one color uploaded original image seems to be compressed.

例如:

原文:

Original:

上传时间:

Uploaded:

我看一遍搜索在谷歌和发现了一些示例如何上传图片的权利。 最后我写了一个方法:

I thorougly searched in google and found some examples how to upload image right. At the end I have written the next method:

void UploadImagePreset(Image image, ImageFormat imgFormat, string imgPath)
{
    using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
    {
        var newWidth = image.Width;
        var newHeight = image.Height;
        using (var outBitmap = new Bitmap(newWidth, newHeight))
        {
            using (var outGraph = Graphics.FromImage(outBitmap))
            {
                outGraph.CompositingQuality = CompositingQuality.HighQuality;
                outGraph.SmoothingMode = SmoothingMode.HighQuality;
                outGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
                outGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

                var imageRect = new Rectangle(0, 0, newWidth, newHeight);
                outGraph.DrawImage(image, imageRect);

                outBitmap.Save(uploadStream, imgFormat);
            }
        }
    }
}

但它并没有帮助。载的图像的相同,如上所述。 我已经tryied指定结果图像的热心公益事业是这样的:

But it did not help. Uploaded image was the same as described above. I have tryied to specify qaulity of the results image like this:

var encoderParameters = new EncoderParameters();
var encoderParameter = new EncoderParameter(Encoder.Quality, 100);
encoderParameters.Param[0] = encoderParameter;
var encoder = ImageCodecInfo.GetImageEncoders()
    .Where(e => e.FormatID == imgFormat.Guid)
    .FirstOrDefault();
outBitmap.Save(uploadStream, encoder, encoderParameters);

它不工作了。另外,当我上传时JPEG图像异常。

It does not work too. In addition when I upload jpeg image exception occurs.

什么办法可以上传图像,而COM pressing或调整大小?我花了几个小时试图解决这个问题,并坚持就可以了。

Is any way to upload image without compressing or resizing? I have spent some hours trying solve this issue and stuck on it.

推荐答案

您正在过载的图像通过图片类 - 这是一个的图像的基础上,通过在流

You are passing the uploaded image through the Image class - this is a new image, based on the passed in stream.

此将保存使用一些默认参数,这将不再反映原始图像

This is then saved using some default parameters, which would no longer reflect the original image.

您应该简单地直接保存传入的流到磁盘 - 这将导致在原始图像中的精确副本

You should simply save the incoming stream directly to disk - this will result in an exact copy of the original image.

这样的(.NET 4.0):

Like this (.NET 4.0):

using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
{
   using(var fileStream = new FileStream(imgPath, FileMode.Create))
   {
       uploadStream.CopyTo(fileStream);
   }
}

或(pre .NET 4.0):

Or (pre .NET 4.0):

using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
{
    using(var fileStream = new FileStream(imgPath, FileMode.Create))
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = uploadStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            fileStream.Write(buffer, 0, read);
        }
    }
}

从<适于href="http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c-sharp">this回答。

这篇关于图片上传没有质量损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆