如何压缩图像之前存储在asp.net中

本文关键字:存储 asp net 图像 何压缩 压缩 | 更新日期: 2023-09-27 18:03:54

这是我的add image代码。

 protected void SubmitButton_Click(object sender, EventArgs e)
        {
            ProductImages productImage = new ProductImages();
            productImage.ProductID = Convert.ToInt32(ProductDropDownList.SelectedValue.ToString());
            if (!FileUpload1.HasFile)
            {
                MessageLabel1.Text = "Please Select Image File";    //checking if file uploader has no file selected
            }
            else
            {
                int length = FileUpload1.PostedFile.ContentLength;
                productImage.ProductImage = new byte[length];
                FileUpload1.PostedFile.InputStream.Read(productImage.ProductImage, 0, length);
                try
                {
                    ProductImageBL.AddProductImages(productImage);
                    MessageLabel1.Text = "Product Image has been successfully added!";
                }
                catch (Exception ex)
                {
                    MessageLabel1.Text = "Some error occured while processing the request. Error Description <br/>" + ex.Message;
                }
            }
        }

如何压缩图像之前存储在asp.net中

图像压缩取决于图像类型和图像上的内容。现实生活中物体的照片通常是jpg格式的,你无法在不明显损失质量的情况下压缩它们。

可能你真正想做的是调整图像的大小,如500*500,如果你知道这将足以满足你的所有需求。请记住在调整大小时保存图像的宽高比。

相关问题:调整图片大小c#

dlxeon发布的SO链接非常棒。我自己用的例子。然而,所有这些例子调整图像的大小,但你也可以增加jpeg文件的压缩和'或降低DPI。

下面是一个如何调整和压缩jpeg的完整示例。它还会检查图像是否需要旋转,例如,如果手机被垂直握住。如果你想让图像正方形,你可以添加padding(内边距)

请注意,如果你使用这个例子是。png和。gif文件的透明度将丢失,因为他们被转换为jpg。

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == true)
        {
            using (Bitmap postedImage = new Bitmap(FileUpload1.PostedFile.InputStream))
            {
                byte [] bin = Common.scaleImage(postedImage, 400, 400, false);
                Image1.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(bin);
            }
        }
    }

    public static byte[] scaleImage(Image image, int maxWidth, int maxHeight, bool padImage)
    {
        try
        {
            int newWidth;
            int newHeight;
            byte[] returnArray;
            //check if the image needs rotating (eg phone held vertical when taking a picture for example)
            foreach (var prop in image.PropertyItems)
            {
                if (prop.Id == 0x0112)
                {
                    int rotateValue = image.GetPropertyItem(prop.Id).Value[0];
                    RotateFlipType flipType = getRotateFlipType(rotateValue);
                    image.RotateFlip(flipType);
                    break;
                }
            }
            //apply padding if needed
            if (padImage == true)
            {
                image = applyPaddingToImage(image);
            }
            //check if the with or height of the image exceeds the maximum specified, if so calculate the new dimensions
            if (image.Width > maxWidth || image.Height > maxHeight)
            {
                var ratioX = (double)maxWidth / image.Width;
                var ratioY = (double)maxHeight / image.Height;
                var ratio = Math.Min(ratioX, ratioY);
                newWidth = (int)(image.Width * ratio);
                newHeight = (int)(image.Height * ratio);
            }
            else
            {
                newWidth = image.Width;
                newHeight = image.Height;
            }
            //start with a new image
            var newImage = new Bitmap(newWidth, newHeight);
            //set the new resolution, 72 is usually good enough for displaying images on monitors
            newImage.SetResolution(72, 72);
            //or use the original resolution
            //newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
            //resize the image
            using (var graphics = Graphics.FromImage(newImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                graphics.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            image = newImage;
            //save the image to a memorystream to apply the compression level, higher compression = better quality = bigger images
            using (MemoryStream ms = new MemoryStream())
            {
                EncoderParameters encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 80L);
                image.Save(ms, getEncoderInfo("image/jpeg"), encoderParameters);
                //save the stream as byte array
                returnArray = ms.ToArray();
            }
            //cleanup
            image.Dispose();
            newImage.Dispose();
            return returnArray;
        }
        catch (Exception ex)
        {
            //there was an error: ex.Message
            return null;
        }
    }

    private static ImageCodecInfo getEncoderInfo(string mimeType)
    {
        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
        for (int j = 0; j < encoders.Length; ++j)
        {
            if (encoders[j].MimeType.ToLower() == mimeType.ToLower())
                return encoders[j];
        }
        return null;
    }

    private static Image applyPaddingToImage(Image image)
    {
        //get the maximum size of the image dimensions
        int maxSize = Math.Max(image.Height, image.Width);
        Size squareSize = new Size(maxSize, maxSize);
        //create a new square image
        Bitmap squareImage = new Bitmap(squareSize.Width, squareSize.Height);
        using (Graphics graphics = Graphics.FromImage(squareImage))
        {
            //fill the new square with a color
            graphics.FillRectangle(Brushes.Red, 0, 0, squareSize.Width, squareSize.Height);
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //put the original image on top of the new square
            graphics.DrawImage(image, (squareSize.Width / 2) - (image.Width / 2), (squareSize.Height / 2) - (image.Height / 2), image.Width, image.Height);
        }
        return squareImage;
    }

    private static RotateFlipType getRotateFlipType(int rotateValue)
    {
        RotateFlipType flipType = RotateFlipType.RotateNoneFlipNone;
        switch (rotateValue)
        {
            case 1:
                flipType = RotateFlipType.RotateNoneFlipNone;
                break;
            case 2:
                flipType = RotateFlipType.RotateNoneFlipX;
                break;
            case 3:
                flipType = RotateFlipType.Rotate180FlipNone;
                break;
            case 4:
                flipType = RotateFlipType.Rotate180FlipX;
                break;
            case 5:
                flipType = RotateFlipType.Rotate90FlipX;
                break;
            case 6:
                flipType = RotateFlipType.Rotate90FlipNone;
                break;
            case 7:
                flipType = RotateFlipType.Rotate270FlipX;
                break;
            case 8:
                flipType = RotateFlipType.Rotate270FlipNone;
                break;
            default:
                flipType = RotateFlipType.RotateNoneFlipNone;
                break;
        }
        return flipType;
    }