using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO ;namespace 图片浏览器{ public partial class Form1 : Form { //不知道变量类型也没有错 String str;//当前打开的图片的绝对路径存放地址 int k;//打开文件夹下已经排好序的第k张照片 int size;//文件夹下的图片文件个数 string[] strfiles;//文件夹下的所有图片地址 public Form1() { InitializeComponent(); } private void OpenToolStripMenuItem1_Click(object sender, EventArgs e)//打开单张照片 { OpenFileDialog OpenFileDialog = new OpenFileDialog(); OpenFileDialog.Filter = "jpg文件(*.jpg)|*.ipg|bmp文件(*.bmp)|*.bmp|png文件(*.png)|*.png"; if (OpenFileDialog.ShowDialog() == DialogResult.OK) { this.pictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog.FileName); str = OpenFileDialog.FileName; this.SaveToolStripMenuItem.Visible = true; this.FormatChangeToolStripMenuItem.Visible = true; this.EditToolStripMenuItem.Visible = true; } } private void OpenFolderToolStripMenuItem_Click(object sender, EventArgs e)//打开一个目录下的图片文件 { int i = 0; for (int j = 0; j < 100; j++) { strfiles[j] = ""; } FolderBrowserDialog n = new FolderBrowserDialog(); if (n.ShowDialog() == DialogResult.OK) { try { DirectoryInfo dir = new DirectoryInfo(n.SelectedPath); FileInfo[] file1 = dir.GetFiles("*.jpg"); foreach (FileInfo str1 in file1) { strfiles[i] = str1.FullName.ToString(); i++; } FileInfo[] file2 = dir.GetFiles("*.bmp"); foreach (FileInfo str1 in file2) { strfiles[i] = str1.FullName.ToString(); i++; } FileInfo[] file3 = dir.GetFiles("*.png"); foreach (FileInfo str1 in file3) { strfiles[i] = str1.FullName.ToString(); i++; } size = i; this.pictureBox1.Image = System.Drawing.Image.FromFile(strfiles[k]); str = strfiles[k]; this.SaveToolStripMenuItem.Visible = true; this.FormatChangeToolStripMenuItem.Visible = true; this.EditToolStripMenuItem.Visible = true; } catch { MessageBox.Show("没有图片文件", "警告", MessageBoxButtons.OK); } } } private void SaveToolStripMenuItem_Click(object sender, EventArgs e)//保存 { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "jpg文件(*.jpg)|*.ipg|bmp文件(*.bmp)|*.bmp|png文件(*.png)|*.png"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { this.pictureBox1.Image.Save(saveFileDialog1.FileName); } } private void GrayToolStripMenuItem_Click(object sender, EventArgs e)//灰度处理 { try { Color c; int red, green, blue, cl; Bitmap befor = new Bitmap(pictureBox1.Image); Bitmap after = new Bitmap(befor); for (int i = 0; i < befor.Width; i++) { for (int j = 0; j < befor.Height; j++) { c = befor.GetPixel(i, j); red = c.R; green = c.G; blue = c.B; cl = (red + green + blue) / 3; Color cc = Color.FromArgb(cl, cl, cl); after.SetPixel(i, j, cc); } } this.pictureBox1.Image = after; } catch { MessageBox.Show("未加载图片", "警告", MessageBoxButtons.OK); } } }}
1 回答
- 1 回答
- 0 关注
- 1935 浏览
添加回答
举报
0/150
提交
取消