C#学习之图片查看器

来源:互联网 发布:游族网络林奇2017 编辑:程序博客网 时间:2024/05/01 19:25

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;namespace demo10{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            this.openFileDialog1.InitialDirectory = @"D:";//设置文件对话框显示的初始目录            this.openFileDialog1.Filter = "bmp文件(*.bmp)|*.bmp|gif文件(*.gif)|*.gif|Jpeg文件(*.jpg)|*.jpg";//设置当前选定筛选器字符串以决定对话框中“文档类型”选项            this.openFileDialog1.FilterIndex = 3;         //设置对话框中当前选定筛选器的索引            this.openFileDialog1.RestoreDirectory = true;  //关闭对话框,还原当前的目录            this.openFileDialog1.Title = "选择图片";    //设置对话框的标题            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)            {                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;//图像充满相框且维持比例                string strpath = this.openFileDialog1.FileName;//获取文件路径                      this.pictureBox1.Image = Image.FromFile(strpath);//加载图片                int index = strpath.LastIndexOf("\\");          //路径中最后一个反斜杠位置                this.richTextBox1.Text = "文件名:" + this.openFileDialog1.FileName.Substring(index + 1);//显示文件名            }        }        private void button2_Click(object sender, EventArgs e)        {             if (this.pictureBox1.Image != null)            {                saveFileDialog1.Filter = "Jpeg 图像(*.jpg)|*.jpg|Bitmap 图像(*.bmp)|*.bmp|Gif 图像(*.gif)|*.gif";                saveFileDialog1.Title = "保存图片"; //设置对话框的标题                saveFileDialog1.CreatePrompt = true;//如果指定不存在的文件,提示允许创建该文件                saveFileDialog1.OverwritePrompt = true;//如果用户指定的文件名已存在,显示警告                saveFileDialog1.ShowDialog();       //弹出保存对话框                if (saveFileDialog1.FileName != "")                {                    System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();                    switch (saveFileDialog1.FilterIndex)    //选择保存文件类型                    {                        case 1:                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); //保存为jpeg文件                            break;                        case 2:                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);                            break;                        case 3:                            this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Gif);                            break;                    }                    fs.Close();         //关闭文件流                }            }            else            {MessageBox.Show("请选择要保存的图片");}        }        private void button3_Click(object sender, EventArgs e)        {             this.colorDialog1.AllowFullOpen = true; //可以使用该对话框定义自定义颜色            this.colorDialog1.AnyColor = true;      //显示基本颜色集中可用的所有颜色            this.colorDialog1.FullOpen = true;      //创建自定义颜色的控件在对话框打开时是可见的            this.colorDialog1.SolidColorOnly = false;//不限制只选择纯色            this.colorDialog1.ShowDialog();     //弹出对话框  /*设置richTextBox1中字体的颜色为选定的颜色*/            this.richTextBox1.ForeColor = this.colorDialog1.Color;          }        private void button4_Click(object sender, EventArgs e)        {               this.fontDialog1.AllowVerticalFonts = true;//指示对话框既显示垂直字体又显示水平字体            this.fontDialog1.FixedPitchOnly = true; //只允许选择固定间距字体            this.fontDialog1.ShowApply = true;      //包含应用按钮            this.fontDialog1.ShowEffects = true;    //允许指定删除线、下画线和文本颜色选项的控件            this.fontDialog1.ShowDialog();       //弹出对话框            this.richTextBox1.Font = this.fontDialog1.Font;//设置richTextBox1中字体为选定的字体        }        private void button5_Click(object sender, EventArgs e)        {             this.printDialog1.AllowCurrentPage = true;//显示当前页            this.printDialog1.AllowPrintToFile = true;//允许选择打印到文件            this.printDialog1.AllowSelection = true;  //启用“选择”单选按钮            this.printDialog1.AllowSomePages = true; //启用“页”单选按钮            this.printDialog1.Document = this.printDocument1;//指定设置的PrintDocument对象            this.printDialog1.PrinterSettings = this.printDocument1.PrinterSettings;//打印页的默认设置            this.printDialog1.PrintToFile = false;//不选择“打印到文件”            this.printDialog1.ShowHelp = true;    //显示“帮助”按钮            this.printDialog1.ShowNetwork = true;//可以选择网络打印机            if (this.printDialog1.ShowDialog() == DialogResult.OK)            {this.printDocument1.Print();  }//打印        }    }}


1 0
原创粉丝点击