Jumat, 06 Februari 2009

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


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.

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
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.
4. Under Templates, click Windows Application.
5. In the Name text box, type ImageDemo, and then click OK.

Form1.vb is created.
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: 1. On the View menu, click Code.
2. 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

3. 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

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: 1. On the Project menu, click Add Module.
2. In the Add New Item - ImageDemo dialog box, click Open.

By default, Module1.vb is created.
3. In the Module1.vb file, locate the following statement:Module Module1

4. 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

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: 1. In Form1.vb, locate the following code:End Class

2. 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

Complete Code Listing
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

#Region " Windows Form Designer generated 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

'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.
Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
End Sub

#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.

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

-------------------------------------------------
Related :
Barcode-string-encoderVbnet

Changing-backgroundcolor

Trik finding google

Directly-filling control

Date-time format vb2008

Finding google rank

Domain checker phps cript

Foreign key sql2005

Create-insert-tableadapter

Check datagrid

Hapus error handling vb2008

Function2 vb2008

Explorer vb2008

Buat splashscreen vb2008

Foundation in net30

Backup-database-SQL2008

Executing SQL SERVER

Data grid view attending