using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ImageExplorerMain
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 显示控制
private void StretchToFit()
{
stToolStripMenuItem.Checked = true;
aytalSizeToolStripMenuItem.Checked = false;
centerInToolStripMenuItem.Checked = false;
stretchToFitToolStripMenuItem.Checked = true;
actualSizeToolStripMenuItem.Checked = false;
centerImageToolStripMenuItem.Checked = false;
stretchToFitToolStripMenuItem1.Checked = true;
actualSizeToolStripMenuItem1.Checked = false;
centerImageToolStripMenuItem1.Checked = false;
}
//拉伸
private void ActualSize()
{
stToolStripMenuItem.Checked = false;
aytalSizeToolStripMenuItem.Checked = true;
centerInToolStripMenuItem.Checked = false;
stretchToFitToolStripMenuItem.Checked = false;
actualSizeToolStripMenuItem.Checked = true;
centerImageToolStripMenuItem.Checked = false;
stretchToFitToolStripMenuItem1.Checked = false;
actualSizeToolStripMenuItem1.Checked = true;
centerImageToolStripMenuItem1.Checked = false;
}
//平扑
private void CenterImage()
{
stToolStripMenuItem.Checked = false;
aytalSizeToolStripMenuItem.Checked = false;
centerInToolStripMenuItem.Checked = true;
stretchToFitToolStripMenuItem.Checked = false;
actualSizeToolStripMenuItem.Checked = false;
centerImageToolStripMenuItem.Checked = true;
stretchToFitToolStripMenuItem1.Checked = false;
actualSizeToolStripMenuItem1.Checked = false;
centerImageToolStripMenuItem1.Checked = true;
}
//居中
private void ShowImage()
{
if(stToolStripMenuItem.Checked == true)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
else if (aytalSizeToolStripMenuItem.Checked == true)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
else
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
//对图片显示方式进行控制
#endregion
private void RecentFiles()
{
}
// Recent Files 功能实现方法
private void AllImage()
{
int i = 0;
//为数组初始化做准备
DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(openFileDialog1.FileName));
FileInfo[] all = d.GetFiles();
FileInfo fi;
string fikz;
foreach (FileInfo alleg in all)
{
fi = new FileInfo(alleg.ToString());
fikz = fi.Extension.ToString();
if(fikz == ".jpg" || fikz == ".gif" || fikz == ".bmp")
{
i++;
}
}
//获取所需数组元素个数
Images = new string[i];
//初始化数组
foreach (FileInfo alleg in all)
{
fi = new FileInfo(alleg.ToString());
fikz = fi.Extension.ToString();
if (fikz == ".jpg" || fikz == ".gif" || fikz == ".bmp")
{
if (alleg.ToString() == Path.GetFileName(openFileDialog1.FileName))
{
NowIndex = index;
//获取当前显示图片在数组的位置
}
Images[index] = alleg.ToString();
index++;
}
fi = null;
}
//对数组元素赋值
}
//获取有图片文件的文件名
private void ShowtoolStripProgressBar()
{
if (pictureBox1.Image != null)
{
pictureBoxH = pictureBox1.Size.Height;
pictureBoxW = pictureBox1.Size.Width;
if (pictureBox1.SizeMode == PictureBoxSizeMode.StretchImage)
{
if (pictureBox1.Height > 0 && pictureBox1.Width > 0)
{
toolStripProgressBar1.Value = 100;
ProgressBar = 100;
}
//pictureBox1 部分可见
else
{
toolStripProgressBar1.Value = 0;
ProgressBar = 0;
}
//pictureBox1 完全不可见
}
else
{
if (pictureBoxH >= ImageH && pictureBoxW >= ImageW)
{
toolStripProgressBar1.Value = 100;
ProgressBar = 100;
}
else if (pictureBoxH >= ImageH && pictureBoxW < ImageW)
{
ProgressBar = ((ImageH * pictureBoxW *100) / (ImageH * ImageW));
toolStripProgressBar1.Value = ProgressBar;
}
else if (pictureBoxH < ImageH && pictureBoxW >= ImageW)
{
ProgressBar = ((ImageW * pictureBoxH * 100) / (ImageH * ImageW));
toolStripProgressBar1.Value = ProgressBar;
}
else if (pictureBoxH < ImageH && pictureBoxW < ImageW)
{
ProgressBar = (pictureBoxW * pictureBoxH * 100) / (ImageH * ImageW);
toolStripProgressBar1.Value = ProgressBar;
}
}
toolStripStatusLabel4.Text = ProgressBar.ToString() + "%";
}
}
// 进程条控制方法
private void ShowpictureBoxImage(string a)
{
string aa = a;
pictureBox1.Image = Image.FromFile(aa);
toolStripStatusLabel1.Text = aa;
//将路径信息显示到 toolStripStatusLabel1
toolStripStatusLabel2.Text = pictureBox1.Image.Size.Height.ToString() + " x " + pictureBox1.Image.Size.Width.ToString();
//将图片 长-宽 信息显示到 toolStripStatusLabel2
ImageH = pictureBox1.Image.Size.Height;
ImageW = pictureBox1.Image.Size.Width;
ShowtoolStripProgressBar();
//在 toolStripProgressBar 中显示图片显示状态
}
// 显示图片方法
#region Open键功能实现代码
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Open";
openFileDialog1.Filter = "JPEG Files|*.JPG|GIF Files|*.GIF|BMP Files|*.BMP";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (biaozhi == true)
{
index = 0;
//防止数组索引超出了数组界限
}
AllImage();
//获取打开图片同目录下的所以图片,并储存文件名
ShowpictureBoxImage(openFileDialog1.FileName);
//在 pictureBox 控件中显示图片
stToolStripMenuItem.Enabled = true;
aytalSizeToolStripMenuItem.Enabled = true;
centerInToolStripMenuItem.Enabled = true;
评论2