您的位置:首页 > 编程语言 > C#

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: