繁体   English   中英

将大图像压缩为小格式

[英]Compressing large image to small format

我将图像以二进制形式存储在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();
        }

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

任何帮助都将被申请

UPDATE

试图摆脱Image的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();

您可以使用此功能来创建缩小的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 在代码中我没有,但是也许您想按比例放大或将其设置为固定值。

完成对Images Dispose ,请不要忘记Dispose它们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM