📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PicYaSuo
{
public partial class Form1 : Form
{
public string[] picpathlist;
public string savepath;
public int c;
public int witdh, height, persent;
public Image img;
public Bitmap bmp;
public Graphics grap;
public bool stop = false;
public bool runing = false;
public int pasttime = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
textBox1.Text="";
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
textBox1.Text += openFileDialog1.FileNames[i].ToString();
}
picpathlist = openFileDialog1.FileNames;
}
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
folderBrowserDialog1.Description = "请选择保存输出图象路径";
folderBrowserDialog1.ShowNewFolderButton = true;
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
if (folderBrowserDialog1.SelectedPath.ToString() != "")
{
textBox7.Text = folderBrowserDialog1.SelectedPath.ToString();
}
}
}
public void YaSuoIt()
{
if (runing)
{
button2.Text = "开始";
//progressBar1.Value = 0;
runing = false;
return;
}
else
{
button2.Text = "停止";
}
pasttime = 0;
runing = true;
if (picpathlist==null||picpathlist.Length<1)
{
MessageBox.Show("您还没有选择要压缩的图片!");
button2.Text = "开始";
progressBar1.Value = 0;
runing = false;
return;
}
if (textBox7.Text=="")
{
MessageBox.Show("您还没有选择要图片的保存路径!");
button2.Text = "开始";
progressBar1.Value = 0;
runing = false;
return;
}
progressBar1.Maximum = picpathlist.Length;
progressBar1.Value = 0;
for (int i = 0; i < picpathlist.Length; i++)
{
progressBar1.Value = i + 1;
Application.DoEvents();
if (!YaSuo(picpathlist[i].ToString(), textBox7.Text.ToString() + "\\" + picpathlist[i].Substring(picpathlist[i].LastIndexOf('\\'))))
{
button2.Text = "开始";
progressBar1.Value = 0;
runing = false;
return;
}
}
button2.Text = "开始";
progressBar1.Value = 0;
runing = false;
MessageBox.Show("Grearo恭喜您,大功告成!");
}
public bool YaSuo(string frompath, string topath)
{
img = Image.FromFile(frompath);
switch (c)
{
case 1:
try
{
witdh = Convert.ToInt32(textBox2.Text);
}
catch
{
MessageBox.Show("参数必须为整数!");
return false;
}
height = img.Height * witdh / img.Width;
break;
case 2:
try
{
height = Convert.ToInt32(textBox3.Text);
}
catch
{
MessageBox.Show("参数必须为整数!");
return false;
}
witdh = img.Width * height / img.Height;
break;
case 3:
try
{
witdh = img.Width * Convert.ToInt32(textBox4.Text) / 100;
height = img.Height * Convert.ToInt32(textBox4.Text) / 100;
}
catch
{
MessageBox.Show("参数必须为整数!");
return false;
}
break;
case 4:
try
{
witdh = Convert.ToInt32(textBox5.Text);
height = Convert.ToInt32(textBox6.Text);
}
catch
{
MessageBox.Show("参数必须为整数!");
return false;
}
break;
default:
MessageBox.Show("未设置输出方式!");
return false;
}
bmp = new Bitmap(witdh, 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, witdh, height));
bmp.Save(topath, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
bmp.Dispose();
grap.Dispose();
//img = null;
//bmp = null;
//grap = null;
return true;
}
private void checkBox1_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox1.Checked = true;
c = 1;
}
private void checkBox2_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox2.Checked = true;
c = 2;
}
private void checkBox3_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox3.Checked = true;
c = 3;
}
private void checkBox4_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
checkBox4.Checked = true;
c = 4;
}
private void button2_Click(object sender, EventArgs e)
{
YaSuoIt();
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
e.Handled = true;
base.OnKeyPress(e);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
Application.DoEvents();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (runing)
{
pasttime++;
label6.Text = pasttime.ToString();
Application.DoEvents();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -