将大图像压缩为小格式

时间:2015-03-08 19:47:34

标签: c# image binary compression

我将图像存储在DB中作为二进制文件,以显示它们我想将它们压缩为较小的图像(4000 x 3000)到(400 x 300),这基本上可以工作,但图像看起来很棒,有人可以指向我正确的方向?

我现在正在使用:

System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(bytes);
        System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
        Type typeoff = fullsizeImage.GetType();
        double height = fullsizeImage.Height;
        double width = Convert.ToDouble(fullsizeImage.Width);
        double aspect = setWidth / width;
        setHeight = Convert.ToInt32(aspect * height);
        System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(Convert.ToInt32(setWidth), setHeight, null, IntPtr.Zero);
        System.IO.MemoryStream myResult = new System.IO.MemoryStream();
        using (System.IO.MemoryStream imageMemStream = new System.IO.MemoryStream(bytes))
        {
            using (Bitmap bitmap = new Bitmap(imageMemStream))
            {
                ImageFormat imageFormat = bitmap.RawFormat;
                if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                {
                    newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                {
                    newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Gif);
                }
                if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
                {
                    newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Bmp);
                }
                if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
                {
                    newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Png);
                }
                if (bitmap.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
                {
                    newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Icon);
                }
            }
        }
        _bytes = myResult.ToArray();  //Returns a new byte array.

一直在寻找这个,但还不知道如何使用我的二进制文件和输出来实现:

        Bitmap image = new Bitmap(fullsizeImage, Convert.ToInt32(newWidth), setHeight);
        using (Graphics gr = Graphics.FromImage(image))
        {
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawImage(fullsizeImage, new Rectangle(0, 0, Convert.ToInt32(newWidth), setHeight));
            _bytes = gr.T.ToArray();
        }

可疑我做错了什么但不知道在哪里做正确的事情,没有太多的图像压缩经验。

任何帮助都是适当的

更新

试图从图像中取出mime类型,但不是很幸运得到它,使用此并且无法找到任何其他代码,返回null。

public byte[] imageToByteArray(System.Drawing.Image newImage)
    {

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        ImageFormat format = newImage.RawFormat;
        if (ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == format.Guid) != null)
        {
            ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == format.Guid);
            string mimeType = codec.MimeType;
        }
        newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();

1 个答案:

答案 0 :(得分:1)

您可以使用此功能创建缩小的Images

public static Image ShrinkImage(Image original, int scale)
{
    Bitmap bmp = new Bitmap(original.Width / scale, original.Height / scale,
                            original.PixelFormat);
    using (Graphics G = Graphics.FromImage(bmp))
    {
        G.InterpolationMode = InterpolationMode.HighQualityBicubic;
        G.SmoothingMode = SmoothingMode.HighQuality;
        Rectangle srcRect = new Rectangle(0,0,original.Width, original.Height);
        Rectangle destRect = new Rectangle(0,0,bmp.Width, bmp.Height);
        G.DrawImage(original, destRect, srcRect, GraphicsUnit.Pixel);
        bmp.SetResolution( original.HorizontalResolution, original.VerticalResolution);
    }
    return (Image)bmp;
}

请注意,它只适用于真实的Bitmap Images,而不适用于Icons;但是无论如何都试图减少图标毫无意义!

另请注意,您可能要也可能不想更改新图片的Dpi。在代码中,我不是可能,您希望将其扩展或将其设置为固定值。

当你完成Dispose后,别忘了Images