C#中OpenFileDialog和PictrueBox的用法分析

先来看看这段代码:

复制代码 代码如下:

string resultFile = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\Patch";
openFileDialog1.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
  if (openFileDialog1.ShowDialog() == DialogResult.OK)
     resultFile = openFileDialog1.FileName;


resultFile 就能得到你选中文件的路径

OpenFileDialog控件有以下基本属性

InitialDirectory 对话框的初始目录

Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
FilterIndex 在对话框中选择的文件筛选器的索引,如果选第一项就设为1
RestoreDirectory 控制对话框在关闭之前是否恢复当前目录
FileName 第一个在对话框中显示的文件或最后一个选取的文件
Title 将显示在对话框标题栏中的字符
AddExtension 是否自动添加默认扩展名
CheckPathExists 在对话框返回之前,检查指定路径是否存在
DefaultExt 默认扩展名
DereferenceLinks 在从对话框返回前是否取消引快捷方式
ShowHelp 启"帮助"按钮
ValiDateNames 控制对话框检查文件名中是否不含有无效的字符或序列

怎样设置OpenFileDialog组件的Filter,使实现一次过滤出多种扩展名的文件?

复制代码 代码如下:

dlg.Filter   =   "Image   Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All   files   (*.*)|*.* "

第一个参数是picturebox的宽度,第二个是picturebox的高度,第三个是你的图片。这个方法可以把图片调整到合适的大小。你就不要设置SizeMode的属性了,通过这个方法得到合适的图片后,设置picturebox的image属性等于这个图片,不要设置背景图。我没有测试。你自己去测试下吧,如果还是有问题,那就是图片太小了。你要重新做张图

复制代码 代码如下:

public Image GetNewImage(int newImgWidth, int newImgHeight, Image srcImage)
{
    Image newImg = srcImage.GetThumbnailImage(newImgWidth, newImgHeight, null, new IntPtr());
    Graphics gr = Graphics.FromImage(newImg);
    gr.DrawImage(newImg, 0, 0, newImg.Width, newImg.Height);
    gr.Dispose();
    return newImg;
}

PictrueBox的SizeMode属性:

复制代码 代码如下:

// 摘要:
//     图像被置于 System.Windows.Forms.PictureBox 的左上角。如果图像比包含它的   System.Windows.Forms.PictureBox
//     大,则该图像将被剪裁掉。
Normal = 0,
//
// 摘要:
//     System.Windows.Forms.PictureBox 中的图像被拉伸或收缩,以适合 System.Windows.Forms.PictureBox
//     的大小。
StretchImage = 1,
//
// 摘要:
//     调整 System.Windows.Forms.PictureBox 大小,使其等于所包含的图像大小。
AutoSize = 2,
//
// 摘要:
//     如果 System.Windows.Forms.PictureBox 比图像大,则图像将居中显示。如果图像比 System.Windows.Forms.PictureBox
//     大,则图片将居于 System.Windows.Forms.PictureBox 中心,而外边缘将被剪裁掉。
CenterImage = 3,
//
// 摘要:
//     图像大小按其原有的大小比例被增加或减小。
Zoom = 4,

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzgyj.html