using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void butOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFiledDialog = new OpenFileDialog();
openFiledDialog.InitialDirectory = "c:\\";
openFiledDialog.Title = "打开和选择文件:孟庆波";
openFiledDialog.Filter = "文本|*.txt|图片|*.jpg"; //设置文件类型 筛选模式为 文本|*.txt|所有的文件|*
openFiledDialog.FilterIndex = 1;//设置默认文件类型显示顺序 //1-文本|*.txt, 2-图片|*.jpg
openFiledDialog.RestoreDirectory = true;//RestoreDirectory有问题??
if (openFiledDialog.ShowDialog() == DialogResult.OK)
{
//打开文件对话框中选择的文件名
string fname = openFiledDialog.FileName;
this.richTextBox1.Text += fname + "\n\r";
}
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
//#region;保存文本
//SaveFileDialog saveFileDialog = new SaveFileDialog();
////saveFileDialog.Title = "保存啊!!";
//saveFileDialog.Filter = "文本|*.txt|图片|*.jpg|word文档|*.docx";//设置文件类型
//saveFileDialog.FilterIndex = 1;//设置默认文件类型显示顺序 //1-文本|*.txt, 2-图片|*.jpg
//saveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
//if (saveFileDialog.ShowDialog() == DialogResult.OK)
//{
// #region; 文件名,文件路径,文件父目录
// ////获得文件的路径
// //string locaFilePath = saveFileDialog.FileName.ToString();
// ////获取文件名,不带路径
// //string fileName = locaFilePath.Substring(locaFilePath.LastIndexOf("\\") + 1);
// ////获取文件父目录
// //string filePath = locaFilePath.Substring(0, locaFilePath.LastIndexOf("\\"));
// ////给文件名加上时间
// //string newFileName = DateTime.Now.ToString("yymmdd") + fileName;
// ////在文件名里夹加字符
// // saveFileDialog.FileName.Insert(1, "damemgqimngbo");
// //this.richTextBox1.Text = locaFilePath;
// //System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog.OpenFile();
// #endregion;
// //// System.IO.File.WriteAllText(saveFileDialog.FileName, richTextBox1.Text);
// //using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
// //{
// // using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
// // {
// // sw.Write(this.richTextBox1.Text);
// // }
// //}
// //MessageBox.Show("成功");
//}
//#endregion;
#region;保存图片
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "图片保存";
saveFileDialog.Filter = "JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Gif Image(*.gif)|*.gif|All Image(*.*)|*.*";//设置文件类型
//saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
//Image img = this.pictureBox1.Image;
//Bitmap bit = new Bitmap(img, 100, 100);
//bit.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Gif);
//this.pictureBox1.Image.Save(saveFileDialog.FileName);
using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
switch (saveFileDialog.FilterIndex)
{
case 1:
this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.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;
default:
this.pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.MemoryBmp);//为null??异常
break;
}
}
}
#endregion;
}
private void btnColorDialog_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.AllowFullOpen = true;
colorDialog.FullOpen = true;
colorDialog.ShowHelp = true;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SelectionColor = colorDialog.Color;
}
else
{
this.richTextBox1.SelectionColor = Color.Red;
}
} |