C#学习之图片查看器

简介: 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

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();	  }				//打印
        }
    }
}


目录
相关文章
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
334 1
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
350 0
|
数据采集 JavaScript C#
C#图像爬虫实战:从Walmart网站下载图片
C#图像爬虫实战:从Walmart网站下载图片
|
Java 物联网 C#
C#/.NET/.NET Core学习路线集合,学习不迷路!
C#/.NET/.NET Core学习路线集合,学习不迷路!
930 0
|
C#
C#的基本语法结构学习
【5月更文挑战第17天】C#基础语法包括变量(如`int x = 10`)、常量(`const int MAX_VALUE = 100`)、运算符(如算术和比较运算符)、控制语句(if、for、while等)和函数声明(`int Add(int x, int y) { return x + y; }`)。这些构成C#程序的基本元素。
417 0
|
Linux C# 开发者
Uno Platform 驱动的跨平台应用开发:从零开始的全方位资源指南与定制化学习路径规划,助您轻松上手并精通 C# 与 XAML 编程技巧,打造高效多端一致用户体验的移动与桌面应用程序
【9月更文挑战第8天】Uno Platform 的社区资源与学习路径推荐旨在为初学者和开发者提供全面指南,涵盖官方文档、GitHub 仓库及社区支持,助您掌握使用 C# 和 XAML 创建跨平台原生 UI 的技能。从官网入门教程到进阶技巧,再到活跃社区如 Discord,本指南带领您逐步深入了解 Uno Platform,并提供实用示例代码,帮助您在 Windows、iOS、Android、macOS、Linux 和 WebAssembly 等平台上高效开发。建议先熟悉 C# 和 XAML 基础,然后实践官方教程,研究 GitHub 示例项目,并积极参与社区讨论,不断提升技能。
850 2
|
开发框架 缓存 算法
开源且实用的C#/.NET编程技巧练习宝库(学习,工作,实践干货)
开源且实用的C#/.NET编程技巧练习宝库(学习,工作,实践干货)
977 0
|
前端开发 Java C#
GitHub突破5k Star!这件事情我坚持了3年,努力打造C#/.NET/.NET Core全面的学习、工作、面试指南知识库
GitHub突破5k Star!这件事情我坚持了3年,努力打造C#/.NET/.NET Core全面的学习、工作、面试指南知识库
390 3
|
C#
C#的类和对象的概念学习案例刨析
【5月更文挑战第17天】C#是一种面向对象的语言,以类和对象为核心。类作为对象的模板,定义了属性(如Name, Age)和行为(如Greet)。对象是类的实例,可设置属性值。封装通过访问修饰符隐藏实现细节,如Customer类的私有name字段通过Name属性访问。继承允许新类(如Employee)从现有类(Person)继承并扩展。多态让不同对象(如Circle, Square)共享相同接口(Shape),实现抽象方法Area,提供灵活的代码设计。
294 1