目录
批量图像转换。
批量图像转换主要用到了Thread类和ImageFormat类。使用Thread类新开一个线程去执行图像的批量转化,防止程序实现假死的状态。
namespace PictureBatchConversion
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
string[] path1=null; //用于存储选择的文件列表
string path2=""; //用于存储保存的路径
Bitmap bt; //声明一个转换图片格式的Bitmap对象
Thread td; //声明一个线程
int Imgtype1; //声明一个变量用于标记ConvertImage方法中转换的类型
string OlePath; //声明一个变量用于存储ConvertImage方法中原始图片的路径
string path; //声明一个变量用于存储ConvertImage方法中转换后图片的保存路径
int flags; //用于标记已转换图片的数量,用于计算转换进度
private void Form2_Load(object sender, EventArgs e)
{
tscbType.SelectedIndex = 0; //设置第一个转换类型被选中
CheckForIllegalCrossThreadCalls = false;//屏蔽线程弹出的错误提示
}
private void toolStripButton3_Click(object sender, EventArgs e)//选择转换文件的按钮
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //判断是否选择文件
{
listView1.Items.Clear(); //清空listView1
string[] info = new string[7]; //存储每一行数据
FileInfo fi; //创建一个FileInfo对象,用于获取图片信息
path1 = openFileDialog1.FileNames; //获取选择的图片集合
for (int i = 0; i < path1.Length; i++) //读取集合中的内容
{
//获取图片名称
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1, path1[i].Length - path1[i].LastIndexOf("\\") - 1);
//获取图片类型
string ImgType = ImgName.Substring(ImgName.LastIndexOf(".") + 1, ImgName.Length - ImgName.LastIndexOf(".") - 1);
fi = new FileInfo(path1[i].ToString()); //实例化FileInfo对象
//将每一行数据第一个位置的图标添加到imageList1中
imageList1.Images.Add(ImgName,Properties.Resources.图标__23_);
info[1] = ImgName; //图片名称
info[2] = ImgType; //图片类型
info[3] = fi.LastWriteTime.ToShortDateString();//图片最后修改日期
info[4] = path1[i].ToString(); //图片位置
info[5] = (fi.Length/1024)+"KB"; //图片大小
info[6] = "未转换"; //图片状态
ListViewItem lvi = new ListViewItem(info, ImgName); //实例化ListViewItem对象
listView1.Items.Add(lvi); //将信息添加到listView1控件中
}
tsslFileNum.Text = "当前共有" + path1.Length.ToString() + "个文件";//状态栏中显示图片数量
}
}
private void toolStripButton2_Click(object sender, EventArgs e) //关闭按钮
{
Application.Exit(); //退出系统
}
private void toolStripButton5_Click(object sender, EventArgs e) //清空列表的按钮
{
listView1.Items.Clear(); //清空列表
path1 = null; //清空图片的集合
tsslFileNum.Text = "当前没有文件"; //状态栏中提示
tsslPlan.Text = ""; //清空进度数字
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (path1 == null) //判断是否选择图片
{
MessageBox.Show("请选择图片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (path2.Length == 0) //判断是否选择保存位置
{
MessageBox.Show("请选择保存位置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
flags = 1; //初始化flags变量为1,用于计算进度
toolStrip1.Enabled = false; //当转换开始时,禁用工具栏
int flag = tscbType.SelectedIndex; //判断将图片转换为何种格式
switch (flag) //根据不同的格式进行转换
{
case 0:
Imgtype1 = 0; //如果选择第一项则转换为BMP格式
td = new Thread(new ThreadStart(ConvertImage)); //通过线程调用ConvertImage方法进行转换
td.Start();
break;
case 1: //如果选择第二项则转换为JPG格式
Imgtype1 = 1;
td = new Thread(new ThreadStart(ConvertImage));//通过线程调用ConvertImage方法进行转换
td.Start();
break;
case 2: //如果选择第三项则转换为PNG格式
Imgtype1 = 2;
td = new Thread(new ThreadStart(ConvertImage));//通过线程调用ConvertImage方法进行转换
td.Start();
break;
case 3: //如果选择第四项则转换为GIF格式
Imgtype1 = 3;
td = new Thread(new ThreadStart(ConvertImage));//通过线程调用ConvertImage方法进行转换
td.Start();
break;
default: td.Abort(); break;
}
}
}
}
private void toolStripButton4_Click(object sender, EventArgs e)//选择保存路径按钮
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) //判断是否选择保存路径
{
path2 = folderBrowserDialog1.SelectedPath; //获取保存路径
}
}
private void ConvertImage()
{
flags = 1;
switch (Imgtype1)
{
case 0:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1, path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".bmp";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换"+flags*100/path1.Length+"%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
case 1:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1, path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".jpeg";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
case 2:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1, path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".png";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Png);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
case 3:
for (int i = 0; i < path1.Length; i++)
{
string ImgName = path1[i].Substring(path1[i].LastIndexOf("\\") + 1, path1[i].Length - path1[i].LastIndexOf("\\") - 1);
ImgName = ImgName.Remove(ImgName.LastIndexOf("."));
OlePath = path1[i].ToString();
bt = new Bitmap(OlePath);
path = path2 + "\\" + ImgName + ".gif";
bt.Save(path, System.Drawing.Imaging.ImageFormat.Gif);
listView1.Items[flags - 1].SubItems[6].Text = "已转换";
tsslPlan.Text = "正在转换" + flags * 100 / path1.Length + "%";
if (flags == path1.Length)
{
toolStrip1.Enabled = true;
tsslPlan.Text = "图片转换全部完成";
}
flags++;
}
break;
default: bt.Dispose(); break;
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)//关闭窗口时要关闭线程
{
if (td != null) //判断是否存在线程
{
if (td.ThreadState == ThreadState.Running) //然后判断线程是否正在运行
{
td.Abort(); //如果运行则关闭线程
}
}
}
}
}
生成缩略图
本例实现主要用到了Image类的GetThumbnailImage方法生成Image缩略图。将生成的缩略图放到ListView中,为了防止出现非常严重的闪烁现象,影响整体的浏览下过,本例还重写了ListView控件,消除了闪烁现象。
public partial class Frm_Main : Form
{
public class CustomListView : ListView
{
public CustomListView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
}
CustomListView clv = new CustomListView();
public Frm_Main()
{
InitializeComponent();
}
string filePath;
public Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
public Thread td;
public bool GetReducedImage(double Percent, string targetFilePath)
{
try
{
Bitmap bt = new Bitmap(120, 120); //创建Bitmap实例
Graphics g = Graphics.FromImage(bt); //创建Graphics实例
g.Clear(Color.White); //设置画布背景颜色为白色
Image ReducedImage; //缩略图
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent); //设置宽度
ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent); //设置高度
//获取所谓图
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
if (ImageWidth > ImageHeight) //如果原图宽度大于高度
{
//缩放图片
g.DrawImage(ReducedImage, 0, (int)(120 - ImageHeight) / 2, ImageWidth, ImageHeight);
}
else
{
g.DrawImage(ReducedImage, (int)(120 - ImageWidth) / 2, 0, ImageWidth, ImageHeight);
}
g.DrawRectangle(new Pen(Color.Gray), 0, 0, 119, 119); //绘制缩略图的边框
bt.Save(@targetFilePath, ImageFormat.Jpeg); //保存缩略图
bt.Dispose(); //释放对象
ReducedImage.Dispose(); //释放对象
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
}
public bool ThumbnailCallback()
{
return false;
}
private void a()
{
double percent;
string[] a = new string[2];
DirectoryInfo di = new DirectoryInfo(filePath);
FileSystemInfo[] fi = di.GetFileSystemInfos();
DirectoryInfo di2 = new DirectoryInfo("c:\\LsTemp");
for (int i = 0; i < fi.Length; i++)
{
string imgType = fi[i].ToString().Substring(fi[i].ToString().LastIndexOf(".") + 1, fi[i].ToString().Length - 1 - fi[i].ToString().LastIndexOf("."));
if (imgType.ToLower() == "jpeg" || imgType.ToLower() == "gif" || imgType.ToLower() == "png" || imgType.ToLower() == "jpg" || imgType.ToLower() == "bmp")
{
string imgName = fi[i].ToString();
imgName = imgName.Remove(imgName.LastIndexOf("."));
string newPath;
if (filePath.Length == 3)
{
newPath = filePath + imgName + "." + imgType;
}
else
{
newPath = filePath + "\\" + imgName + "." + imgType;
}
ResourceImage = Image.FromFile(newPath);
if (ResourceImage.Width < ResourceImage.Height)
{
percent = (double)120 / ResourceImage.Height;
}
else
{
percent = (double)120 / ResourceImage.Width;
}
if (!di2.Exists)
{
Directory.CreateDirectory("c:\\LsTemp");
if (GetReducedImage(percent, "c:\\LsTemp\\_" + imgName + ".JPG"))
{
imageList1.Images.Add(i.ToString(), Image.FromFile("c:\\LsTemp\\_" + imgName + ".JPG"));
a[0] = imgName + "." + imgType;
ListViewItem lvi = new ListViewItem(a);
lvi.ImageKey = i.ToString();
clv.Items.Add(lvi);
}
}
else
{
if (GetReducedImage(percent, "c:\\LsTemp\\_" + imgName + ".JPG"))
{
imageList1.Images.Add(i.ToString(), Image.FromFile("c:\\LsTemp\\_" + imgName + ".JPG"));
a[0] = imgName + "." + imgType;
ListViewItem lvi = new ListViewItem(a);
lvi.ImageKey = i.ToString();
clv.Items.Add(lvi);
}
}
ResourceImage.Dispose();
}
tsslText.Text = "共有" + this.clv.Items.Count.ToString() + "张图片";
}
td.Abort();
}
private void toolStripButton1_Click_1(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
clv.Items.Clear();
filePath = folderBrowserDialog1.SelectedPath;
td = new Thread(new ThreadStart(a));
td.Start();
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void deletefile()
{
try
{
DirectoryInfo di2 = new DirectoryInfo("c:\\LsTemp");
if (di2.Exists)
{
Scripting.FileSystemObject fso = new Scripting.FileSystemObject();
fso.DeleteFolder("c:\\LsTemp", true);
}
}
catch
{
deletefile();
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (td != null)
{
td.Abort();
}
deletefile();
}
private void clv_Click(object sender, EventArgs e)
{
if (clv.SelectedItems.Count > 0)
{
string pName = clv.SelectedItems[0].Text;
if (filePath.Length == 3)
{
tsslPath.Text = "图片路径:" + filePath + pName;
}
else
{
tsslPath.Text = "图片路径:" + filePath + "\\" + pName;
}
}
}
private void clv_DoubleClick(object sender, EventArgs e)
{
if (clv.SelectedItems.Count > 0)
{
string pName = clv.SelectedItems[0].Text;
if (filePath.Length == 3)
{
System.Diagnostics.Process.Start(filePath + pName);
tsslPath.Text = "图片路径:" + filePath + pName;
}
else
{
System.Diagnostics.Process.Start(filePath + "\\" + pName);
tsslPath.Text = "图片路径:" + filePath + "\\" + pName;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
panel1.Controls.Add(clv);
clv.Dock = DockStyle.Fill;
clv.LargeImageList = imageList1;
clv.View = View.LargeIcon;
clv.DoubleClick += new EventHandler(clv_DoubleClick);
clv.Click+=new EventHandler(clv_Click);
}
}
旋转和翻转图像
旋转前的图像:
旋转后的图像:
旋转图像主要用到了Bitmap类的RotateFlip方法。该方法拥来时对图像的旋转、翻转。
代码:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.jpg|*.jpg"; //设置图片的类型
openFileDialog1.ShowDialog(); //打开文件对话框
Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName); //实例化Image类
pictureBox1.Image = myImage; //显示打开的图片
}
private void button2_Click(object sender, EventArgs e)
{
Image myImage = pictureBox1.Image; //实例化Image类
myImage.RotateFlip(RotateFlipType.Rotate90FlipXY); //调用RotateFlip方法将JPG格式图像进行旋转
pictureBox1.Image = myImage; //显示旋转后的图片
}
展示椭圆形的图像。
主要使用Graphics类的FillEllipse方法将打开的图像以椭圆的形式进行显示。
private void button1_Click(object sender, EventArgs e)
{
this.panel1.Refresh();//刷新Panel控件
Graphics g = this.panel1.CreateGraphics();//实例化绘图对象
TextureBrush MyBrush = new TextureBrush(MyBitmap);//实例化TextureBrush对象
g.FillEllipse(MyBrush, this.panel1.ClientRectangle);//绘制椭圆图像
}