Microsoft KB Archive/827046

= How to open multiple image files without using the OpenFileDialog class in Visual Basic .NET or Visual Basic 2005 =

Article ID: 827046

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 2005

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Windows Application
 * Replace the Existing Constructor of Your Form
 * Add a New Module to Your Project
 * Change the Startup Object of Your Project
 * Add Code to Open and Then to Display Images
 * Complete Code Listing
 * Verify That the Code Works
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article describes how to open multiple image files without using the OpenFileDialog class. This article also includes a sample code listing that describes the concepts that are discussed in this article.

Note The sample code listing in this article does not contain error-checking. Modify the code to suit your application.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005

This article assumes that you are familiar with the following topics:
 * Programming with Visual Basic .NET or Visual Basic 2005
 * The System.Drawing.Image class
 * The System.IO.FileStream class

back to the top

Create a Windows Application

 * 1) Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types.
 * 1) Under Templates, click Windows Application.
 * 2) In the Name text box, type ImageDemo, and then click OK.

Form1.vb is created.

back to the top

Replace the Existing Constructor of Your Form
By default, the existing constructor of Form1 does not accept any parameters. To pass a list of images and their descriptions to the constructor of Form1, modify the default constructor. To do this, follow these steps:  On the View menu, click Code.  In the Form1.vb file, locate the following code in the Windows Form Designer generated code region: Public Sub New MyBase.New

'The Windows Form Designer requires this call. InitializeComponent

'Add any initialization after the InitializeComponent call.

End Sub   Replace the code that you located in step 2 with the following code: ' Declare the constructor of Form1 to accept the passed command-line arguments. Public Sub New(ByVal CmdArgs As String) MyBase.New

'The Windows Form Designer requires this call. InitializeComponent

'Add any initialization after the InitializeComponent call. Me.WindowState = FormWindowState.Maximized

' Call the method that opens and then displays your images. OpenAndDisplayImages(CmdArgs) End Sub 

back to the top

Add a New Module to Your Project
To pass a list of images and their descriptions to the constructor of Form1, add a new module to your project and then create a new Form1 object in the Main method of your new module. The Main method accepts command-line arguments. To do this, follow these steps:  On the Project menu, click Add Module. In the Add New Item - ImageDemo dialog box, click Open.

By default, Module1.vb is created.  In the Module1.vb file, locate the following statement: Module Module1 </li>  Add the following code after the code that you located in step 3: ' Declare a Main method that accepts your list of images and their descriptions ' as command-line arguments. Public Sub Main(ByVal CmdArgs As String) ' Create a Form1 object by passing the received command-line arguments ' to the constructor of Form1 and then display the new Form1 object. Application.Run(New Form1(CmdArgs)) End Sub </li></ol>

back to the top

Change the Startup Object of Your Project
Your project is currently configured to use Form1 as the Startup object. To pass command-line arguments to your project, modify the Startup object for your project to Module1. To do this, follow these steps:
 * 1) On the Project menu, click ImageDemo Properties.
 * 2) In the ImageDemo Property Pages dialog box, click to select Module1 from the Startup object combo box, and then click OK.

back to the top

Add Code to Open and Then to Display Images
To open and then to display the list of images that are passed to the constructor of your Form1 object, follow these steps:   In Form1.vb, locate the following code: End Class </li>  Add the following code before the code that you located in step 1: ' Declare a method to control the programming logic that you must have ' to open and then to display all your images. Private Sub OpenAndDisplayImages(ByVal CmdArgs As String) ' Variable to iterate through your list of images. Dim Count As Integer Try ' Temporarily suspend the layout logic for your Form1 object. Me.SuspendLayout ' Iterate through your list of images. For Count = 0 To (CmdArgs.Length - 1) Step 2 ' Call the method that opens and then displays the image that corresponds to         ' the image file name that is indexed by the current value of Count. OpenAndDisplay(CmdArgs(Count), Count) Next Me.ResumeLayout(False) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub

' Declare a method to open and then to display the image that ImageFileName specifies. Private Sub OpenAndDisplay(ByVal ImageFileName As String, ByVal Count As Integer) ' Declare a variable to hold a reference to your image. Dim ImageStream As System.IO.FileStream Try ' Open your image for Read access as a new FileStream. ImageStream = New System.IO.FileStream(ImageFileName, _        System.IO.FileMode.Open, System.IO.FileAccess.Read) ' Declare a variable to hold a reference to your PictureBox. Dim MyPictureBox As New PictureBox ' Set the image for the PictureBox to reduce or to expand ' to fit the size of the PictureBox. MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage ' Set the location of the PictureBox based on the value of Count. MyPictureBox.Location = New System.Drawing.Point(Count * 64, 0) ' Set the size of the PictureBox. MyPictureBox.Size = New System.Drawing.Size(128, 128) ' Set the BorderStyle of the PictureBox. MyPictureBox.BorderStyle = BorderStyle.Fixed3D ' Set the image of the PictureBox from the opened FileStream. MyPictureBox.Image = System.Drawing.Image.FromStream(ImageStream) ' Add the PictureBox to your Form1 object. Me.Controls.Add(MyPictureBox) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub </li></ol>

back to the top

Module1.vb
Option Strict On

Module Module1 ' Declare a Main method that accepts your list of images and their descriptions, ' as command-line arguments. Public Sub Main(ByVal CmdArgs As String) ' Create a Form1 object by passing the received command-line arguments ' to the constructor of Form1, and then display the new Form1 object. Application.Run(New Form1(CmdArgs)) End Sub

End Module

Form1.vb
Option Strict On

Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

' Declare the constructor of Form1 to accept the passed command-line arguments. Public Sub New(ByVal CmdArgs As String) MyBase.New

'The Windows Form Designer requires this call. InitializeComponent

'Add any initialization after the InitializeComponent call. Me.WindowState = FormWindowState.Maximized

' Call the method that opens and then displays your images. OpenAndDisplayImages(CmdArgs) End Sub

'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If     End If      MyBase.Dispose(disposing) End Sub

'Required by the Windows Form Designer. Private components As System.ComponentModel.IContainer

'NOTE: The Windows Form Designer requires the following procedure. 'It can be modified by using the Windows Form Designer. 'Do not modify the procedure by using the Code editor. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent components = New System.ComponentModel.Container Me.Text = &quot;Form1&quot; End Sub


 * 1) End Region

' Declare a method to control the programming logic that you must have ' to open and then to display all your images. Private Sub OpenAndDisplayImages(ByVal CmdArgs As String) ' Variable to iterate through your list of images. Dim Count As Integer Try ' Temporarily suspend the layout logic for your Form1 object. Me.SuspendLayout ' Iterate through your list of images. For Count = 0 To (CmdArgs.Length - 1) Step 2 ' Call the method that opens and then displays the image that corresponds to            ' the image file name that is indexed by the current value of Count. OpenAndDisplay(CmdArgs(Count), Count) Next Me.ResumeLayout(False) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub

' Declare a method to open and then to display the image that ImageFileName specifies. Private Sub OpenAndDisplay(ByVal ImageFileName As String, ByVal Count As Integer) ' Declare a variable to hold a reference to your image. Dim ImageStream As System.IO.FileStream Try ' Open your image for Read access as a new FileStream. ImageStream = New System.IO.FileStream(ImageFileName, _           System.IO.FileMode.Open, System.IO.FileAccess.Read) ' Declare a variable to hold a reference to your PictureBox. Dim MyPictureBox As New PictureBox ' Set the image for the PictureBox to reduce or to expand ' to fit the size of the PictureBox. MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage ' Set the location of the PictureBox based on the value of Count. MyPictureBox.Location = New System.Drawing.Point(Count * 64, 0) ' Set the size of the PictureBox. MyPictureBox.Size = New System.Drawing.Size(128, 128) ' Set the BorderStyle of the PictureBox. MyPictureBox.BorderStyle = BorderStyle.Fixed3D ' Set the image of the PictureBox from the opened FileStream. MyPictureBox.Image = System.Drawing.Image.FromStream(ImageStream) ' Add the PictureBox to your Form1 object. Me.Controls.Add(MyPictureBox) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub

End Class Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

back to the top

Verify That the Code Works

 * 1) On the Build menu, click Build Solution.
 * 2) Open a command prompt.
 * 3) At the command prompt, change the directory path to the location of ImageDemo.exe that you built in step 1.
 * 4) Type the following command at the command prompt, and then press the ENTER key to run ImageDemo after the appropriate command-line arguments are passed:

ImageDemo &quot; &quot; &quot;Description of ImageFile1&quot; &quot; &quot; &quot;Description of ImageFile2&quot;

Note  and   are placeholders for the file paths of the image files on your computer. You may use file names instead of file paths if the corresponding image files are located in the same directory as the directory path that you built in step 3.

Form1 is displayed. Notice that your image files are displayed on Form1.

Note You may open and then display up to eight image files by using the sample code listing that is provided in this article.

back to the top

Troubleshooting

 * You may receive a &quot;System.IO.FileNotFoundException&quot; error message when you run ImageDemo in step 4 of the &quot;Verify That the Code Works&quot; section of this article.

You receive this message because one of the file paths that you provided in step 4 of the &quot;Verify That the Code Works&quot; section of this article is incorrect. Your file paths may be incorrect if you do not use quotation marks for command-line arguments that contain spaces.
 * Some of your image files are not displayed on Form1.

This problem occurs if you provide more than eight image file names as command-line arguments. Only the first eight image files are displayed on Form1. You can modify the OpenAndDisplayImages method and the OpenAndDisplay method to display more than eight images on Form1.

back to the top

<div class="references_section">