图片上传质量不好

时间:2014-03-14 04:45:10

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

我有一个应用程序将图像上传到数据库,然后使用处理程序在网页中显示它们。我的问题是图像显示的质量和颜色与原始图像不同。我不知道问题是什么。

                File = FileUpload1.PostedFile;
            imgbin = new Byte[File.ContentLength];
            File.InputStream.Read(imgbin, 0, File.ContentLength);
            imagename = FileUpload1.FileName;
            // Here the image size is defined --------------------
            System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
            UploadedImageWidth = UploadedImage.PhysicalDimension.Width;
            UploadedImageHeight = UploadedImage.PhysicalDimension.Height;
            //Resize Image
            int compressWidth;
            if (UploadedImageWidth >639)
            {
                compressWidth = 640;
            }
            else
            {
                compressWidth = Convert.ToInt32(UploadedImageWidth);
            }
            ResizeImage compressPicture = new ResizeImage();
            imgbin = compressPicture.ResizeImageFile(imgbin, compressWidth);
            System.Drawing.Image Image = System.Drawing.Image.FromStream(new MemoryStream(imgbin));


  public class ResizeImage
{
    public byte[] ResizeImageFile(byte[] imageFile, int targetSize) // Set targetSize to 1024
    {
        using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
        {
            Size newSize = CalculateDimensions(oldImage.Size, targetSize);
            using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format64bppPArgb))
            {
                byte[] byteArray = new byte[0];
                using (Graphics canvas = Graphics.FromImage(newImage))
                {
                    canvas.Clear(Color.White);
                    canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    canvas.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    canvas.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                    int quality = 100;
                    ImageCodecInfo codec = RetrieveCodec("image/jpeg");

                    using (MemoryStream m = new MemoryStream())
                    {
                        using (EncoderParameters codeParams = new EncoderParameters())
                        {
                            using (EncoderParameter p = new EncoderParameter(Encoder.Quality, quality))
                            {
                                codeParams.Param[0] = p;
                                newImage.Save(m, codec, codeParams);
                                byteArray = m.ToArray();
                            }
                        }
                    }
                    return byteArray;
                }
            }
        }
    }

    private ImageCodecInfo RetrieveCodec(string mimeType)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.MimeType == mimeType)
                return codec;
        }

        return null;
    }

    public static Size CalculateDimensions(Size oldSize, int targetSize)
    {
        decimal ajusteTamaño;
        Size newSize = new Size();
        if (oldSize.Width > targetSize)
        {
            ajusteTamaño = oldSize.Width / Convert.ToDecimal(targetSize);
            newSize.Width = (int)(oldSize.Width / ajusteTamaño);
            newSize.Height = (int)(oldSize.Height / ajusteTamaño);
        }
        else
        {
            newSize.Width = oldSize.Width;
            newSize.Height = oldSize.Height;
        }
        return newSize;
    }
}

0 个答案:

没有答案