C#简单的图片浏览器
2014-06-12 11:41
155 查看
public partial class Form1 : Form
{
private int ImageCount=0;
private List<string> ImagePaths = new List<string>();
private int nowCount = 0;
Boolean flag = false;
Thread t;
public Form1()
{
InitializeComponent();
}
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string n = fd.FileName.Replace(fd.SafeFileName,"");
foreach (string Path in Directory.GetFiles(@n))
{
if (Path.Contains(".jpg") || Path.Contains(".bmp")
|| Path.Contains(".png") || Path.Contains(".gif")
|| Path.Contains(".ico"))
{
ImagePaths.Add(Path);
ImageCount++;
}
if (Path.Contains(fd.SafeFileName))
{
nowCount = ImageCount - 1;
}
}
ImageCount--;
this.pictureBox1.Image = new Bitmap(fd.FileName);
}
}
private void 反转ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
int width = this.pictureBox1.Width;
int height = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
for (int x = -width / 2; x <= width / 2; x++)
{
Rectangle DestRect = new Rectangle(0, height / 2 - x, width, 2 * x);
Rectangle SrcRect = new Rectangle(0, 0, this.pictureBox1.Image.Width, this.pictureBox1.Image.Height);
g.DrawImage(this.pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);
}
g.Dispose();
}
catch { }
}
private void 旋转ToolStripMenuItem_Click(object sender, EventArgs e)
{
Image myImage = pictureBox1.Image;
myImage.RotateFlip(RotateFlipType.Rotate90FlipXY); //调用RotateFlip方法将JPG格式图像进行旋转
pictureBox1.Image = myImage;
}
private void 下一张ToolStripMenuItem_Click(object sender, EventArgs e)
{
nowCount++;
if (nowCount <= ImageCount)
{
this.pictureBox1.Image = new Bitmap(ImagePaths[nowCount]);
}
else
{
MessageBox.Show("已经是最后一张了");
}
}
private void 上一张ToolStripMenuItem_Click(object sender, EventArgs e)
{
nowCount--;
if (nowCount <= ImageCount && nowCount>=0)
{
this.pictureBox1.Image = new Bitmap(ImagePaths[nowCount]);
}
else
{
MessageBox.Show("已经是第一张了");
}
}
private void 幻灯片ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!flag)
{
t = new Thread(OpenImage);
t.IsBackground = true;
t.Start();
flag = true;
}
else
{
t.Abort();
flag = false;
}
}
public void OpenImage() {
foreach (string s in ImagePaths)
{
AddImage(s);
Thread.Sleep(1000);
}
t.Abort();
}
private delegate void AddImageDelegate(string path);
public void AddImage(string path) {
if (pictureBox1.InvokeRequired)
{
AddImageDelegate d = new AddImageDelegate(AddImage);
pictureBox1.Invoke(d, path);
}
else
{
pictureBox1.Image = new Bitmap(path);
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择图片保存的位置!";
try
{
if (fbd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image.Save(@fbd.SelectedPath+"\\text.jpeg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
catch { }
}
}
窗体类的设计如图,form中有一个menustrip,一个panel,panel中有一个picturebox
{
private int ImageCount=0;
private List<string> ImagePaths = new List<string>();
private int nowCount = 0;
Boolean flag = false;
Thread t;
public Form1()
{
InitializeComponent();
}
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string n = fd.FileName.Replace(fd.SafeFileName,"");
foreach (string Path in Directory.GetFiles(@n))
{
if (Path.Contains(".jpg") || Path.Contains(".bmp")
|| Path.Contains(".png") || Path.Contains(".gif")
|| Path.Contains(".ico"))
{
ImagePaths.Add(Path);
ImageCount++;
}
if (Path.Contains(fd.SafeFileName))
{
nowCount = ImageCount - 1;
}
}
ImageCount--;
this.pictureBox1.Image = new Bitmap(fd.FileName);
}
}
private void 反转ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
int width = this.pictureBox1.Width;
int height = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(this.BackColor);
for (int x = -width / 2; x <= width / 2; x++)
{
Rectangle DestRect = new Rectangle(0, height / 2 - x, width, 2 * x);
Rectangle SrcRect = new Rectangle(0, 0, this.pictureBox1.Image.Width, this.pictureBox1.Image.Height);
g.DrawImage(this.pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);
}
g.Dispose();
}
catch { }
}
private void 旋转ToolStripMenuItem_Click(object sender, EventArgs e)
{
Image myImage = pictureBox1.Image;
myImage.RotateFlip(RotateFlipType.Rotate90FlipXY); //调用RotateFlip方法将JPG格式图像进行旋转
pictureBox1.Image = myImage;
}
private void 下一张ToolStripMenuItem_Click(object sender, EventArgs e)
{
nowCount++;
if (nowCount <= ImageCount)
{
this.pictureBox1.Image = new Bitmap(ImagePaths[nowCount]);
}
else
{
MessageBox.Show("已经是最后一张了");
}
}
private void 上一张ToolStripMenuItem_Click(object sender, EventArgs e)
{
nowCount--;
if (nowCount <= ImageCount && nowCount>=0)
{
this.pictureBox1.Image = new Bitmap(ImagePaths[nowCount]);
}
else
{
MessageBox.Show("已经是第一张了");
}
}
private void 幻灯片ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!flag)
{
t = new Thread(OpenImage);
t.IsBackground = true;
t.Start();
flag = true;
}
else
{
t.Abort();
flag = false;
}
}
public void OpenImage() {
foreach (string s in ImagePaths)
{
AddImage(s);
Thread.Sleep(1000);
}
t.Abort();
}
private delegate void AddImageDelegate(string path);
public void AddImage(string path) {
if (pictureBox1.InvokeRequired)
{
AddImageDelegate d = new AddImageDelegate(AddImage);
pictureBox1.Invoke(d, path);
}
else
{
pictureBox1.Image = new Bitmap(path);
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择图片保存的位置!";
try
{
if (fbd.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image.Save(@fbd.SelectedPath+"\\text.jpeg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
catch { }
}
}
窗体类的设计如图,form中有一个menustrip,一个panel,panel中有一个picturebox
相关文章推荐
- C# 系统应用之ListView实现简单图片浏览器
- 用C#做的一个简单的图片浏览器
- C#编写简单的图片浏览器
- c# 实现简单浏览器
- C# GDI+ 简单绘图 (三) 仿浏览器截屏效果
- C#对图片的几种简单处理
- C#对图片的几种简单处理
- C#对图片的几种简单处理 [ZT]
- C#对图片的几种简单处理
- CSS+Table图文混排中实现文本自适应图片宽度(超简单+跨所有浏览器)
- CSS+Table图文混排中实现文本自适应图片宽度(超简单+跨所有浏览器)
- 简单的IE内核多页浏览器(支持解锁右键锁定和文字选择复制锁定支持网页截成图片)
- 上传图片并显示缩略图的最简单方法(c#)
- 简单的浏览器(C#)
- 简单的C#图片压缩类-winform
- c# 图片合并的简单代码
- 简单的GDI+处理图片大小(C#代码)
- 上传图片并显示缩略图的最简单方法(c#)
- C#的简单图片透明处理
- 简单的GDI+处理图片大小(C#代码)