C # OpenFileDialog and PictrueBox usage analysis,

Source: Internet
Author: User

C # OpenFileDialog and PictrueBox usage analysis,

This example describes how to use OpenFileDialog and PictrueBox in C. Share it with you for your reference. The usage analysis is as follows:

Let's take a look at this Code:
Copy codeThe Code is as follows: 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 to get the path of the selected file

The OpenFileDialog control has the following basic attributes:

Initial directory in the InitialDirectory dialog box

The file Filter to display in the dialog box. For example, "text file (*. txt) | *. txt | all files (*. *) | *.*"
FilterIndex: The index of the file filter selected in the dialog box. If the first item is selected, it is set to 1.
Whether to restore the current directory before the RestoreDirectory control dialog box is closed
FileName: The first file displayed in the dialog box or the last selected file
The Title will contain characters in the Title bar of the dialog box.
Whether AddExtension automatically adds the default Extension
CheckPathExists check whether the specified path exists before returning in the dialog box
DefaultExt default Extension
DereferenceLinks: whether to cancel referencing shortcuts before returning from the dialog box
ShowHelp enable the "help" button
The ValiDateNames control dialog box checks whether invalid characters or sequences are not included in the file name.

How can I set the Filter of the OpenFileDialog component to Filter files with multiple extensions at a time?
Copy codeThe Code is as follows: dlg. filter = "Image Files (*. BMP ;*. JPG ;*. GIF) | *. BMP ;*. JPG ;*. GIF | All files (*. *) | *. *"

The first parameter is the width of picturebox, the second parameter is the height of picturebox, and the third parameter is your image. This method can adjust the image size to a proper size. You don't need to set the SizeMode attribute. After obtaining the appropriate image, set the image attribute of picturebox to the image. Do not set the background image. I did not test. Test it by yourself. If there is still a problem, the picture is too small. You need to create a new graph.
Copy codeThe Code is as follows: 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;
}

The SizeMode attribute of PictrueBox:
Copy codeThe Code is as follows: // summary:
// The image is placed in the upper left corner of System. Windows. Forms. PictureBox. If the image is better than the System. Windows. Forms. PictureBox
// Large, the image will be cropped.
Normal = 0,
//
// Summary:
// Images in System. Windows. Forms. PictureBox are stretched or shrunk to fit System. Windows. Forms. PictureBox
.
StretchImage = 1,
//
// Summary:
// Adjust the size of System. Windows. Forms. PictureBox so that it is equal to the included image size.
AutoSize = 2,
//
// Summary:
// If System. Windows. Forms. PictureBox is larger than the image, the image is centered. If the image is better than System. Windows. Forms. PictureBox
// If the image is large, the image is placed in the center of System. Windows. Forms. PictureBox, and the outer edge is cropped.
CenterImage = 3,
//
// Summary:
// The image size is increased or decreased according to its original size ratio.
Zoom = 4,

I hope this article will help you with C # programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.