程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#學習之圖片查看器

C#學習之圖片查看器

編輯: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 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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved