最近做的项目当中,需要将视频采集卡采集过来的图片进行压缩处理,原有一张JPG默认320*240大小为300KB,经过压缩之后为6KB,压缩50倍!

先放上截图吧:

可以添加单个文件,支持多选,也可以添加文件夹,自动遍历文件夹中的图片,当然,还有很多不完善的地方,只是个例子而已!呵呵!

贴出所有完整代码吧,一看就懂!呵呵,用到了皮肤加载,就在构造函数当中!不好意思有点懒,代码都没有注释!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SKINPPDOTNETLib;

namespace EcanPicTools
{
    public partial class frmMain : Form
    {
        Image img;
        Bitmap bmp;
        Graphics grap;
        int width, height;

SKINPPDOTNETLib.SkinPPDotNetClass myskin = new SkinPPDotNetClass();
        public frmMain()
        {
            InitializeComponent();
            this.txtbili.KeyPress += new KeyPressEventHandler(txt_KeyPress);
            this.txtWidth.KeyPress += new KeyPressEventHandler(txt_KeyPress);
            this.txtHeight.KeyPress += new KeyPressEventHandler(txt_KeyPress);
            Control.CheckForIllegalCrossThreadCalls = false;
            myskin.LoadSkin(Application.StartupPath + @"\spring.ssk", true);
        }

private void frmMain_Load(object sender, EventArgs e)
        {
            init();
        }

private void init()
        {
            this.Text = "图片压缩工具(作者:刘典武)---普通模式";
            labTransparent.Text = "透明值:100%";
            txtWidth.Enabled = false;
            txtHeight.Enabled = false;
            rbtnbili.Checked = true;
            txtbili.Focus();
        }

private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }

private void yasuo(string frompath, string topath)
        {
            try
            {
                img = Image.FromFile(frompath);

if (rbtnbili.Checked)
                {
                    width = Convert.ToInt32(img.Width * (Convert.ToDouble(txtbili.Text) / 100));
                    height = Convert.ToInt32(img.Height * (Convert.ToDouble(txtbili.Text) / 100));
                }
                else
                {
                    width = Convert.ToInt32(txtWidth.Text.Trim());
                    height = Convert.ToInt32(txtHeight.Text.Trim());
                }

bmp = new Bitmap(width, height);
                grap = Graphics.FromImage(bmp);
                grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                grap.DrawImage(img, new Rectangle(0, 0, width, height));

bmp.Save(topath, System.Drawing.Imaging.ImageFormat.Jpeg);
                bmp.Dispose();
                img.Dispose();
                grap.Dispose();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            finally { }
        }

private void btnStart_Click(object sender, EventArgs e)
        {
            if (lboxPicPath.Items.Count <= 0)
            {
                MessageBox.Show("你还没有选择要压缩的图片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtSavePath.Text == "")
            {
                MessageBox.Show("你还没有选择要保存的文件夹路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

pbar.Maximum = lboxPicPath.Items.Count;
            pbar.Value = 0;

if (rbtnbili.Checked && txtbili.Text == "")
            {
                MessageBox.Show("请填好比例值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtbili.Focus();
                return;
            }
            else if (rbtnkg.Checked && (txtHeight.Text == "" || txtWidth.Text == ""))
            {
                MessageBox.Show("请填好宽高值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWidth.Focus();
                return;
            }

for (int i = 0; i < lboxPicPath.Items.Count; i++)
            {
                pbar.Value = i + 1;
                this.yasuo(lboxPicPath.Items[i].ToString(), txtSavePath.Text + "\\" + Path.GetFileName(lboxPicPath.Items[i].ToString()));
                labInfo.Text = "已经压缩图片张数:" + Convert.ToString(i + 1);
            }
            MessageBox.Show("恭喜,压缩图片成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

private void btnShow_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                this.getFile(fbd.SelectedPath);
            }
        }

private void getFile(string path)
        {
            DirectoryInfo pic = new DirectoryInfo(path);
            foreach (FileInfo file in pic.GetFiles("*.*"))
            {
                lboxPicPath.Items.Add(file.FullName);
            }
        }

private void btnShowSavePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择保存输出图像路径";
            fbd.ShowNewFolderButton = true;

if (fbd.ShowDialog() == DialogResult.OK)
            {
                if (fbd.SelectedPath.ToString() != "")
                {
                    txtSavePath.Text = fbd.SelectedPath;
                }
            }
        }

private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "请选择要压缩的图片";
            open.Filter = "图片文件(*.jpg,*.bmp,*.png,*.gif)|*.jpg;*.bmp;*.png;*.gif";
            open.Multiselect = true;
            if (open.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in open.FileNames)
                {
                    lboxPicPath.Items.Add(file);
                }
            }
        }

private void picTop_Click(object sender, EventArgs e)
        {
            if (this.TopMost)
            {
                this.TopMost = false;
                this.Text = "图片压缩工具(作者:刘典武)---普通模式";
            }
            else
            {
                this.TopMost = true;
                this.Text = "图片压缩工具(作者:刘典武)---置顶模式";
            }
        }

private void tbarTransparent_Scroll(object sender, EventArgs e)
        {
            labTransparent.Text = "透明值:" + Convert.ToString(100 - tbarTransparent.Value) + "%";

this.Opacity = 1 - (float)(tbarTransparent.Value) / 100;
        }

private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lboxPicPath.SelectedItems.Count > 0)
            {
                for (int i = lboxPicPath.SelectedItems.Count - 1; i >= 0; i--)
                {
                    lboxPicPath.Items.Remove(lboxPicPath.SelectedItems[i]);
                }
            }
            else
            {
                MessageBox.Show("请选择要移除的文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

private void rbtnbili_CheckedChanged(object sender, EventArgs e)
        {
            txtbili.Enabled = rbtnbili.Checked;
            if (rbtnbili.Checked)
            {
                txtbili.Focus();
            }
        }

private void rbtnkg_CheckedChanged(object sender, EventArgs e)
        {
            txtWidth.Enabled = rbtnkg.Checked;
            txtHeight.Enabled = rbtnkg.Checked;
            if (rbtnkg.Checked)
            {
                txtWidth.Focus();
            }
        }

}
}

源文件下载:点击这里下载

转载于:https://www.cnblogs.com/feiyangqingyun/archive/2010/12/07/1899032.html

C#制作图片压缩工具相关推荐

  1. Electron-Vue + Element-UI 制作图片压缩工具实战

    本场 Chat 的主要内容是,分享用 Electron 技术栈制作一个图片压缩客户端小工具,让入门了 Electron 的同学,有一个练手的小项目,此项目能一直当作自己的一个项目进行维护,也能在自己的 ...

  2. 怎么直接压缩图片?好用的图片压缩工具推荐

    在我们现在的工作生活当中,图片已经变得无处不在了,但由于现在的拍摄设备像素越来越高,因此图片也变得越来越大,因此在使用一些高清图片的时候经常会遇到图片太大无法上传或者图片太大传输太慢的情况.因此,图片 ...

  3. Android攻城狮Gallery和ImageSwitcher制作图片浏览器

    使用Gallery 和 ImageSwitcher 制作图片浏览器 Gallery介绍 我们有时候在手机上或者PC上看到动态的图片,可以通过鼠标或者手指触摸来移动它,产生动态的图片滚动效果,还可以根据 ...

  4. jar包导出无法显示图片或者音乐_如何制作图片视频短片,配上音乐闪耀朋友圈!...

    把图片制作成视频短片,再配上一首好听的音乐,发到朋友圈,不仅可以更具创意的分享自己的生活点滴,更能因您的创意获得一大票的赞哦!看到别人分享自己制作的图片视频短片,是不是心痒痒也想做一个呢?今天就教你使 ...

  5. 小动画制作 图片盒子配合定时器 winform 114869633

    小动画制作 图片盒子配合定时器 winform 114869633 目标一 让图片盒子与图片列表控件可以互动起来 让图片盒子控件的图片,从图片列表控件中获取 新建一个图片列表控件 把相关的图片加载 进 ...

  6. iphone iPhone开发中如何将制作图片放大缩小代码实现案例

    1:原文摘自:http://mobile.51cto.com/iphone-285108.htm iPhone开发中如何将制作图片放大缩小案例是本文要介绍的内容,主要是来学习iphone开发中动画的制 ...

  7. 如何制作图片一句话木马

    如何制作图片一句话木马 新建一个文件夹,在其中放入一张图片(图片后缀名只要是图片的那几个后缀名即可)和一句话木马,例如: 其中的一句话木马为 <?php @eval($_POST["v ...

  8. css背景图片高斯模糊_CSS3 filter(滤镜) 制作图片高斯模糊无需JS

    本帖最后由 fengrui99 于 2019-7-8 11:20 编辑 这是一个寂寞的工作日,因为没有女盆友所以很寂寞,因为很寂寞所以来写教程.希望再看教程的你也是单身!啊,知道你是单身枫瑞贼开心!爽 ...

  9. Python第三方库巧用,制作图片验证码只需三行代码

    现在验证码的种类真的是越来越多,短信验证码.语音验证码.图片验证码.滑块验证码 - 我们在 PC 的网页端或者手机上的 app 进行登录或者注册时,应该总会遇见图片验证码,比如下面这类: 上面这些图片 ...

最新文章

  1. 一维码Code 128简介及其解码实现(zxing-cpp)
  2. Node.js 究竟是什么?(zz)
  3. 联想e480笔记本如何拆屏幕_如何评价 2020 年 10 月 20 日联想发布的小新 Pro14 笔记本?有哪些亮点和槽点?...
  4. 树莓派400键盘计算机发布!全新的电路板布局,更快,更酷!
  5. 在OSX中制作加密压缩文件
  6. 一个简单的dotnet tool
  7. 递归 遍历目录下的所有文件
  8. 备忘--简单比较SPSS、RapidMiner、KNIME以及Kettle四款数据分析工具
  9. 2019牛客多校第一场I Points Division(DP)题解
  10. ITIL 4: 培训与认证
  11. Ubuntu10.04下搞定D-Link DWA-125无线网卡驱动
  12. 前端开发人员必备的十项技能
  13. mysql一条sql是一个事物么_mysql 事物浅析
  14. html制作唐诗,Steve:HTML创始人来中国当教师 痴迷李白自称“唐诗”
  15. C++算法——DFS
  16. 鸿蒙 OS 的到来,能为我们改变什么?
  17. 三分钟让闲置电脑变云主机
  18. end kernel panic not syncing
  19. uevent netlink(KOBJECT_UEVENT)
  20. [SARscape操作] 哨兵数据的导入 - 以Sentinel-1A为例

热门文章

  1. Newtonsoft.Json的简单使用
  2. r指定位置插入一列数值
  3. OK335xS CAN device register and deiver match hacking
  4. 六、CPU优化(4)NUMA架构
  5. apache用户名和密码验证
  6. 网络生活点滴 网络管理实用8招技巧
  7. 如何修改world 2007文档结构图的字体 ???
  8. jQuery CSS 操作 - css() 方法
  9. ViewTreeObserver
  10. hash 建表 query 统计重复个数