search
HomeBackend DevelopmentC#.Net TutorialUsage 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)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use