http://www.developer.com/net/cplus/article.php/3439411/Managed-C-Loading-and-Displaying-Images.htm
The OpenFileDialog::Filter property is a string that represents the types listed in the File Types combo box when the dialog is displayed. Each type is represented by a pair of values delimited by a vertical bar where the first value is the text that the user will see and the second value is the filter that the dialog will use in determining which files to display. Here are some examples to illustrate my point:
To download the accompanying source code for the demo, click here.
The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.
Managed C++: Loading and Displaying Images
November 26, 2004
One of the biggest drawbacks of programming in Visual C++/MFC was its lack of support for even the most basic of imaging functions—such as loading and displaying an image. Therefore, this article illustrates how incredibly easy .NET makes displaying a user-selected image and allowing the user to dynamically resize that image.
The File Open Dialog
The following figure shows the accompanying demo for this article. As you can see, the user can select an image file to display:
As opposed to MFC, where you instantiate and use a CFileDialog class, using the Windows Forms version of the common file open dialog involves these steps:
Only display GIF files
"GIFs (*.gif)|*.gif"
Two filters—GIF and JPG
"GIFs (*.gif)|*.gif|JPEGs (*.jpg)|*.jpg;*.jpeg"
One combined filter (GIF and JPG)
"GIF & JPG (*.gif;*.jpg;)|*.gif;*.jpg;"
The FilterIndex property is used to specify which of the filters is the default.
Note: Because index values in C++ are always relative to 0, C++ programmers naturally think to set the FilterIndex property to a value of 0 when they want the first element. However, don't be fooled. While setting the value to 0 will select the first entry, that's only because any invalid index value defaults to the first entry. To correctly specify the index value, you must state it relative to 1 (i.e., 1 is the first entry—not 0).
Here's the code pasted from the demo where the user is allowed to specify an image file to open. As you can see, the OpenFileDialog::ShowDialog is used to display the dialog and the return value is DialogResult::OK if the user selected a file (as opposed to canceling the dialog):
dlgOpenFile->InitialDirectory = S"C:\\";
dlgOpenFile->Filter =
S"All Image Formats (*.bmp;*.gif;*.jpg;*.jpeg;*.tif)|"
S"*.bmp;*.gif;*.jpg;*.jpeg;*.tif|"
S"Bitmaps (*.bmp)|*.bmp|"
S"GIFs (*.gif)|*.gif|"
S"JPEGs (*.jpg)|*.jpg;*.jpeg|"
S"TIFs (*.tif)|*.tif";
dlgOpenFile->FilterIndex = 1;
if (DialogResult::OK == dlgOpenFile->ShowDialog())
{
// Display the image
}
Displaying an Image on a PictureBox Control
Once the user has selected a file using the OpenFileDialog, its FileName property (or FileNames if you specified to allow a multi-file selection) will contain the name of the selected file. Loading the image is as simple as calling the static Image::FromFile method. The following snippet sets the PictureBox::Image property to the Image object returned from Image::FromFile:
picImage->Image = Image::FromFile(dlgOpenFile->FileName);
Sizing an Image Within a PictureBox Control
You can use four basic values for displaying an image. You can specify this value in the designer (via the PictureBox control's SizeMode property) or in code if you want the user to be able to dynamically control this settings (as the demo illustrates).
Note: If you allow the user to switch between sizing modes, you need to be aware of the fact that once you specify the Autosize mode (where the PictureBox control resizes itself to conform to the image's size), you'll need to reset the PictureBox::Width and PictureBox::Height properties when switching to any of the other modes.
Download the Code
About the Author