最近做的项目当中,需要将视频采集卡采集过来的图片进行压缩处理,原有一张JPG默认320*240大小为300KB,经过压缩之后为6KB,压缩50倍!
先放上截图吧:
可以添加单个文件,支持多选,也可以添加文件夹,自动遍历文件夹中的图片,当然,还有很多不完善的地方,只是个例子而已!呵呵!
贴出所有完整代码吧,一看就懂!呵呵,用到了皮肤加载,就在构造函数当中!不好意思有点懒,代码都没有注释!
usingSystem;
usingSystem.Collections.Generic;
ponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
usingSKINPPDOTNETLib;
namespaceEcanPicTools
{
publicpartialclassfrmMain:Form
{
Imageimg;
Bitmapbmp;
Graphicsgrap;
intwidth,height;
SKINPPDOTNETLib.SkinPPDotNetClassmyskin=newSkinPPDotNetClass();
publicfrmMain()
{
InitializeComponent();
this.txtbili.KeyPress+=newKeyPressEventHandler(txt_KeyPress);
this.txtWidth.KeyPress+=newKeyPressEventHandler(txt_KeyPress);
this.txtHeight.KeyPress+=newKeyPressEventHandler(txt_KeyPress);
Control.CheckForIllegalCrossThreadCalls=false;
myskin.LoadSkin(Application.StartupPath+@"\spring.ssk",true);
}
privatevoidfrmMain_Load(objectsender,EventArgse)
{
init();
}
privatevoidinit()
{
this.Text="图片压缩工具(作者:刘典武)---普通模式";
labTransparent.Text="透明值:100%";
txtWidth.Enabled=false;
txtHeight.Enabled=false;
rbtnbili.Checked=true;
txtbili.Focus();
}
privatevoidtxt_KeyPress(objectsender,KeyPressEventArgse)
{
if((e.KeyChar<48||e.KeyChar>57)&&(e.KeyChar!=8))
{
e.Handled=true;
}
base.OnKeyPress(e);
}
privatevoidyasuo(stringfrompath,stringtopath)
{
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=newBitmap(width,height);
grap=Graphics.FromImage(bmp);
grap.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grap.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grap.DrawImage(img,newRectangle(0,0,width,height));
bmp.Save(topath,System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
img.Dispose();
grap.Dispose();
}
catch(Exceptionex){MessageBox.Show(ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}
finally{}
}
privatevoidbtnStart_Click(objectsender,EventArgse)
{
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;
}
elseif(rbtnkg.Checked&&(txtHeight.Text==""||txtWidth.Text==""))
{
MessageBox.Show("请填好宽高值!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtWidth.Focus();
return;
}
for(inti=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);
}
privatevoidbtnShow_Click(objectsender,EventArgse)
{
FolderBrowserDialogfbd=newFolderBrowserDialog();
if(fbd.ShowDialog()==DialogResult.OK)
{
this.getFile(fbd.SelectedPath);
}
}
privatevoidgetFile(stringpath)
{
DirectoryInfopic=newDirectoryInfo(path);
foreach(FileInfofileinpic.GetFiles("*.*"))
{
lboxPicPath.Items.Add(file.FullName);
}
}
privatevoidbtnShowSavePath_Click(objectsender,EventArgse)
{
FolderBrowserDialogfbd=newFolderBrowserDialog();
fbd.Description="请选择保存输出图像路径";
fbd.ShowNewFolderButton=true;
if(fbd.ShowDialog()==DialogResult.OK)
{
if(fbd.SelectedPath.ToString()!="")
{
txtSavePath.Text=fbd.SelectedPath;
}
}
}
privatevoidbtnSelect_Click(objectsender,EventArgse)
{
OpenFileDialogopen=newOpenFileDialog();
open.Title="请选择要压缩的图片";
open.Filter="图片文件(*.jpg,*.bmp,*.png,*.gif)|*.jpg;*.bmp;*.png;*.gif";
open.Multiselect=true;
if(open.ShowDialog()==DialogResult.OK)
{
foreach(stringfileinopen.FileNames)
{
lboxPicPath.Items.Add(file);
}
}
}
privatevoidpicTop_Click(objectsender,EventArgse)
{
if(this.TopMost)
{
this.TopMost=false;
this.Text="图片压缩工具(作者:刘典武)---普通模式";
}
else
{
this.TopMost=true;
this.Text="图片压缩工具(作者:刘典武)---置顶模式";
}
}
privatevoidtbarTransparent_Scroll(objectsender,EventArgse)
{
labTransparent.Text="透明值:"+Convert.ToString(100-tbarTransparent.Value)+"%";
this.Opacity=1-(float)(tbarTransparent.Value)/100;
}
privatevoidbtnDelete_Click(objectsender,EventArgse)
{
if(lboxPicPath.SelectedItems.Count>0)
{
for(inti=lboxPicPath.SelectedItems.Count-1;i>=0;i--)
{
lboxPicPath.Items.Remove(lboxPicPath.SelectedItems[i]);
}
}
else
{
MessageBox.Show("请选择要移除的文件","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
privatevoidrbtnbili_CheckedChanged(objectsender,EventArgse)
{
txtbili.Enabled=rbtnbili.Checked;
if(rbtnbili.Checked)
{
txtbili.Focus();
}
}
privatevoidrbtnkg_CheckedChanged(objectsender,EventArgse)
{
txtWidth.Enabled=rbtnkg.Checked;
txtHeight.Enabled=rbtnkg.Checked;
if(rbtnkg.Checked)
{
txtWidth.Focus();
}
}
}
}
源文件下载:点击这里下载