简单的C#图片压缩类-winform

news/2024/7/17 7:54:53
#region  C# 图片处理功能 -- BY DREAMDLM
/*
{*******************************************************************}
{                                                                  }
{                    C#图片处理功能-DREAMDLM                      }
{                                                                  }
{*******************************************************************}
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System. Windows .Forms;

namespace EU.EUClass
{
    class ImgGDI
    {
        public ImgGDI()
        {
            //构造函数
        }

        /// <summary>
        /// Bitmap转换byte[]数组
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public byte[] Bmptobyte(Bitmap bmp)
        {
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Jpeg);
            ms.Flush();
            byte[] buffer = ms.GetBuffer();
            ms.Close();
            return buffer;
        }

        /// <summary>
        /// byte[]数组转换Bitmap
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public Bitmap bytetobmp(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(buffer, 0, buffer.Length);
            Bitmap bmp = new Bitmap(ms);
            ms.Close();
            return bmp;
        }

        /// <summary>
        /// 返回默认图片
        /// </summary>
        /// <returns></returns>
        public Bitmap getInstance()
        {
            Bitmap bmp = DefaultPic();
            return bmp;
        }

        /// <summary>
        /// 选取本地图片
        /// </summary>
        /// <param name="IMG"></param>
        /// <returns></returns>
        public Bitmap LocalIMG(string IMG)
        {
            FileStream fs = new FileStream(IMG, FileMode.Open);
            Bitmap bmp = new Bitmap(fs);
            fs.Close();
            return bmp;
        }

        /// <summary>
        /// 返回流状态图片
        /// </summary>
        /// <param name="Img"></param>
        /// <returns></returns>
        public Bitmap ImgFromBase64(string Img)
        {
            Bitmap bmp;
            byte[] buffer = Convert.FromBase64String(Img);
            if (buffer.Length > 0)
            {
                MemoryStream ms = new MemoryStream();
                ms.Write(buffer, 0, buffer.Length);
                bmp = new Bitmap(ms);
                ms.Close();
                return bmp;
            }
            else
            {
                bmp = DefaultPic() ;
                return bmp;
            }
        }

        /// <summary>
        /// 默认图片
        /// </summary>
        /// <returns></returns>
        private Bitmap DefaultPic()
        {
            FileStream fs = new FileStream(Application.StartupPath + @"\Goodr.jpg", FileMode.Open);
            Bitmap bmp = new Bitmap(fs);
            fs.Close();
            return bmp;
        }

        /// <summary>
        /// GDI压缩图片
        /// </summary>
        /// <param name="bmp">传入参数Bitmap</param>
        /// <returns></returns>
        public byte[] ImageGdi(Bitmap bmp)
        {
            Bitmap xbmp = new Bitmap(bmp);
            MemoryStream ms = new MemoryStream();
            xbmp.Save(ms, ImageFormat.Jpeg);
            byte[] buffer;
            ms.Flush();
            if (ms.Length > 95000)
            {
                //buffer = ms.GetBuffer();
                double new_width = 0;
                double new_ height  = 0;

                Image m_src_image = Image.FromStream(ms);
                if (m_src_image.Width >= m_src_image.Height)
                {
                    new_width = 1024;
                    new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
                }
                else if (m_src_image.Height >= m_src_image.Width)
                {
                    new_height = 768;
                    new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
                }

                Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
                Graphics m_graphics = Graphics.FromImage(bbmp);
                m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height);

                ms = new MemoryStream();

                bbmp.Save(ms, ImageFormat.Jpeg);
                buffer = ms.GetBuffer();
                ms.Close();

                return buffer;
            }
            else
            {
                buffer = ms.GetBuffer();
                ms.Close();
                return buffer;
            }
        }
    }
}




本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/235686,如需转载请自行联系原作者


http://www.niftyadmin.cn/n/2011076.html

相关文章

sql 先进先出 库存

createtablet( id intidentity(1,1), name varchar(50),--商品名称 j int, --入库数量 c int, --出库数量 jdate datetime--入库时间 ) insertintot(name,j,c,jdate) selectA,100,0,2007-12-01insertintot(name,j,c,jdate) selectA,200,0,2008-01-07insertintot…

使用Spring RestTemplate解析RESTful服务

在上一篇“使用Spring MVC 4构建Restful服务”博文中&#xff0c;我们谈到了如何使用Spring MVC 4搭建RESTful服务&#xff0c;本文以上篇文章为基础&#xff0c;继续讲解如何解析Spring RESTful服务。上篇文章中&#xff0c;我们用了jacson包进行Java对象的序列化&#xff0c;…

javaApi免费下载

压缩包&#xff1a; 链接&#xff1a;http://pan.baidu.com/s/1qYoizco 密码&#xff1a;cxur .chm文件&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1WRFecsOo9-pZSb9CoeT7FQ 密码&#xff1a;g7ym CSDN下载&#xff1a;https://download.csdn.net/download/eseszb/…

ACM训练 算法集锦

链接&#xff1a;http://pan.baidu.com/s/1jH4Zp6u 密码&#xff1a;2cuo

VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程

之前论坛里的 莱茵哈特网友发过一帖&#xff1a; VMware虚拟机安装MAC OS X Mountain Lion详细图文教程 &#xff0c;这个帖子发的时候还是VMware8&#xff0c;现在的最新版已经更新到了9.01版本&#xff0c;而且 Mountain Lion也更新到了10.8.2版本&#xff0c;加上看到帖子后…

Win7下安装苹果MAC OS X Mountain Lion 双系统详细图文教程

本教程为在win7系统中安装苹果 Mountain Lion 双系统的图文教程&#xff0c; 如果仅是想要安装苹果尝鲜&#xff0c;建议在虚拟机中安装&#xff0c;请参考&#xff1a; VMware虚拟机安装MAC OS X Mountain Lion详细图文教程如果对PC机安装黑苹果有兴趣的朋友&#xff0c;请详细…

自学IT 必去的两个学习网站

慕课网和传智播客&#xff0c;都有大量的的免费学习视频&#xff0c;如c&#xff0c;c&#xff0c;java&#xff0c;web&#xff0c;数据库&#xff0c;python&#xff0c;大数据&#xff0c;人工智能&#xff0c;机器学习&#xff0c;项目实战等&#xff0c;想自学的小伙伴们可…

动态规划专题训练

专题练习的压缩包在网盘里&#xff0c;可自行下载 链接&#xff1a;https://pan.baidu.com/s/1pK773HD 密码&#xff1a;hqjj