C#制作图片压缩工具
生活随笔
收集整理的這篇文章主要介紹了
C#制作图片压缩工具
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近做的項目當中,需要將視頻采集卡采集過來的圖片進行壓縮處理,原有一張JPG默認320*240大小為300KB,經過壓縮之后為6KB,壓縮50倍!
?先放上截圖吧:
?
可以添加單個文件,支持多選,也可以添加文件夾,自動遍歷文件夾中的圖片,當然,還有很多不完善的地方,只是個例子而已!呵呵!
?貼出所有完整代碼吧,一看就懂!呵呵,用到了皮膚加載,就在構造函數(shù)當中!不好意思有點懶,代碼都沒有注釋!
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("請?zhí)詈帽壤?#xff01;",?"提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
????????????????txtbili.Focus();
????????????????return;
????????????}
????????????else?if?(rbtnkg.Checked?&&?(txtHeight.Text?==?""?||?txtWidth.Text?==?""))
????????????{
????????????????MessageBox.Show("請?zhí)詈脤捀咧?#xff01;",?"提示",?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?=?"已經壓縮圖片張數(shù):"?+?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#制作图片压缩工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决mysqlslap执行命令报错(BE
- 下一篇: js刷新页面大全