zoukankan      html  css  js  c++  java
  • C#制作图片压缩工具

    最近做的项目当中,需要将视频采集卡采集过来的图片进行压缩处理,原有一张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(00, 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();
                }
            }

        }
    }


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

  • 相关阅读:
    "Hello world" of ML
    数据读进set,进行后处理
    从csv文件读取数据到二维vector
    logistic regression
    Probabilistic interpretation
    python3 批量管理Linux服务器 下发命令与传输文件
    Redis 主从复制、读写分离(基于docker)
    Springboot 整合Redis
    Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 106.xx.xxx229:6379
    docker 创建redis容器以及redis命令笔记
  • 原文地址:https://www.cnblogs.com/feiyangqingyun/p/1899032.html
Copyright © 2011-2022 走看看