Usage analysis of OpenFileDialog and PictrueBox in C#
本文实例讲述了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,
以上就是C#中OpenFileDialog和PictrueBox的用法分析的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
