压缩图片

来源:互联网 发布:linux查看硬盘大小 编辑:程序博客网 时间:2024/05/16 18:10

/*
     * Compress the image
     * if the image not compressed,and we will get a very big byte data,
     * beacuse the bmp's Resolution is 1200*800
     * so we should compress the bmp.
     */
    /// <summary>
    /// 对图片进行高质量的压缩,不影响其显示效果(Compress Image)
    /// </summary>
    public class compressionImage {
        /// <summary>
        /// Convertor Bmp to byte[]
        /// </summary>
        /// <param name="bmp">Bitmap</param>
        /// <returns></returns>
        public byte[] ImageGdi(Bitmap bmp) {
            //Evaluate the bmp to xbmp
            Bitmap xbmp = new Bitmap(bmp);
            //use MemoryStream to store the bmp
            MemoryStream ms = new MemoryStream();
            //save the bmp to MemoryStream as Jpeg
            xbmp.Save(ms, ImageFormat.Jpeg);
            //define Buffer
            byte[] buffer;
            //reflush MemoryStream
            ms.Flush();
            //If MemoryStream's Length longer then 95000, Compress the Bmp
            //Else convert Bmp to byte[]
            //Compress the Bmp just to change the Bmp's size as big as 1024*768
            if (ms.Length > 95000) {
                //buffer = ms.GetBuffer();
                //Bmp's width
                double new_width = 0;
                //Bmp's height
                double new_height = 0;

                //Revert the MemoryStream to Image
                Image m_src_image = Image.FromStream(ms);
                //If image's Width longer than Height
                if (m_src_image.Width >= m_src_image.Height) {
                    //Evaluate the Image's new Width  to 1024
                    new_width = 1024;
                    //Calculate Image's new Height
                    new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
                }
                //If image's Height Longer than Width
                else if (m_src_image.Height >= m_src_image.Width) {
                    //Evaluate the Image's new Height  to 768
                    new_height = 768;
                    //Calculate Image's new Width
                    new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
                }
                //Get compressed Bitmap
                Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
                //Repair the Bitmap's quality
                Graphics m_graphics = Graphics.FromImage(bbmp);
                m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //Get Repair Bitmap
                m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height);

                ms = new MemoryStream();
                //save the new Bitmap to MemoryStream
                bbmp.Save(ms, ImageFormat.Jpeg);
                //Convert MemoryStream to Byte[]
                buffer = ms.GetBuffer();
                //Dispose Memory
                ms.Close();
                //Return Image's byte
                return buffer;
            }
            else {
                //Convert MemoryStream to Byte[]
                buffer = ms.GetBuffer();
                //Dispose Memory
                ms.Close();
                //Return Image's byte
                return buffer;
            }
        }
    }

原创粉丝点击