Image Viewer in VB.NET in VB.NET

Description.

This program allows you to open and view image files including JPEG, GIF, WMF and other images. Program also provides options to stretch and shrink them, rotate at different angles through all axes and save them in different formats.

This example uses menus, radio buttons, group boxes, open and save dialog boxes, picture box and status bar controls.

Compiling Code with Visual Studio .NET.

To compile and execute code with VS.NET, create a Windows application and copy and paste this code in Form1.vb class. Replace all code from Form1.vb accept namespace includes.

ImageViewerST1-in-vb.net.jpg 

Source Code:

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Text
Imports System.Drawing.Imaging
Imports System.IO
Public Class win
Inherits Form
Private stm As Stream
Private pnlTop As Panel
Private stbBtm As StatusBar
Private lblPicMode As Label
Private mnuMain As MainMenu
Private pbxImg As PictureBox
Private blnPicLoaded As Boolean
Private
cmbPicMode As ComboBox
Private dlgFile As OpenFileDialog
Private btnTransform, btnOrg As Button
Private gpbRotate, gpbFlip As GroupBox
Private mnuFile, mnuOpen, mnuSave, mnuExit, mnuSep As MenuItem
Private strImgName, strRot, strFlip, strRotFlip, strStatus As [String]
Private rbnRotNone, rbnRotX, rbnRotY, rbnRotXY, rbnFlipNone, rbnFlip90, rbnFlip180, rbnFlip270, rbnTemp As RadioButton
Public Sub New()
Try
Me
.Text = "Images"
pnlTop =
New Panel
Me.Size = New Size(770, 570)
Me.Controls.Add(pnlTop)
Me.Menu = fncBuildMenus()
setControls()
strRot = rbnRotNone.Name
strFlip = rbnFlipNone.Name
pnlTop.Location =
New Point(0, 0)
pnlTop.Size =
New Size(750, 500)
Catch e As Exception
Console.WriteLine(("error ...... " + e.StackTrace))
End Try
End
Sub
'New
Private Shared Sub Main()
Application.Run(
New win)
End Sub
'Main
Private Function fncBuildMenus() As MainMenu ' build the menu's
mnuMain =
New MainMenu
mnuFile =
New MenuItem
mnuOpen =
New MenuItem
mnuSave =
New MenuItem
mnuExit =
New MenuItem
mnuSep =
New MenuItem
mnuFile.Text = "&File"
mnuOpen.Text = "&Open"
mnuSave.Text = "Save &As"
mnuExit.Text = "E&xit"
mnuSep.Text = "-"
mnuFile.MenuItems.Add(mnuOpen)
mnuFile.MenuItems.Add(mnuSave)
mnuFile.MenuItems.Add(mnuSep)
mnuFile.MenuItems.Add(mnuExit)
mnuMain.MenuItems.Add(mnuFile)
AddHandler mnuOpen.Click, AddressOf fncOpen
AddHandler mnuSave.Click, AddressOf fncSave
AddHandler mnuExit.Click, AddressOf fncExit
Return mnuMain
End Function
'fncBuildMenus
Private Sub setControls()
' initialize and add the controls to the form.
gpbRotate = New GroupBox
gpbRotate.Location =
New Point(0, 0)
gpbRotate.Size =
New Size(300, 50)
gpbRotate.Text = "Rotate"
gpbFlip =
New GroupBox
gpbFlip.Location =
New Point(300, 0)
gpbFlip.Size =
New Size(300, 50)
gpbFlip.Text = "Flip"
rbnRotNone = fncRadBtns("None", "None", 50, 10, 20)
rbnFlipNone = fncRadBtns("None", "None", 50, 10, 20)
rbnRotX = fncRadBtns("90 deg", "90", 70, 80, 20)
rbnRotY = fncRadBtns("180 deg", "180", 70, 150, 20)
rbnRotXY = fncRadBtns("270 deg", "270", 70, 220, 20)
rbnFlip90 = fncRadBtns("X - axis", "X", 70, 80, 20)
rbnFlip180 = fncRadBtns("Y - axis", "Y", 70, 150, 20)
rbnFlip270 = fncRadBtns("XY - axis", "XY", 70, 220, 20)
rbnRotNone.Checked =
True
rbnFlipNone.Checked = True
btnTransform = New Button
btnTransform.Text = "Transform Image"
btnTransform.Location =
New Point(0, 65)
btnTransform.Width = 100
btnOrg =
New Button
btnOrg.Text = "Original Position"
btnOrg.Location =
New Point(200, 65)
btnOrg.Width = 100
lblPicMode =
New Label
lblPicMode.Text = "Picture Mode "
lblPicMode.Location =
New Point(350, 67)
lblPicMode.Width = 70
cmbPicMode =
New ComboBox
cmbPicMode.Location =
New Point(420, 65)
cmbPicMode.DropDownStyle = ComboBoxStyle.DropDownList
cmbPicMode.Items.Add("Auto Size")
cmbPicMode.Items.Add("Center Image")
cmbPicMode.Items.Add("Normal")
cmbPicMode.Items.Add("Stretch Image")
cmbPicMode.SelectedIndex = 2
pbxImg =
New PictureBox
pbxImg.Location =
New Point(0, 100)
pbxImg.Size =
New Size(750, 400)
stbBtm =
New StatusBar
stbBtm.Text = "Normal mode - Image is clipped if it is bigger than the Picture Box."
stbBtm.BackColor = Color.Green
stbBtm.Size =
New Size(750, 20)
stbBtm.Location =
New Point(0, 550)
gpbRotate.Controls.Add(rbnRotNone)
gpbRotate.Controls.Add(rbnRotX)
gpbRotate.Controls.Add(rbnRotY)
gpbRotate.Controls.Add(rbnRotXY)
gpbFlip.Controls.Add(rbnFlipNone)
gpbFlip.Controls.Add(rbnFlip90)
gpbFlip.Controls.Add(rbnFlip180)
gpbFlip.Controls.Add(rbnFlip270)
pnlTop.Controls.Add(gpbRotate)
pnlTop.Controls.Add(gpbFlip)
pnlTop.Controls.Add(btnTransform)
pnlTop.Controls.Add(btnOrg)
pnlTop.Controls.Add(lblPicMode)
pnlTop.Controls.Add(cmbPicMode)
Controls.Add(stbBtm)
pnlTop.Controls.Add(pbxImg)
blnPicLoaded =
False
strStatus = stbBtm.Text
End Sub
'setControls
Private Function fncRadBtns(ByVal strText As [String], ByVal strName As [String], ByVal intWidth As Integer, ByVal intX As Integer, ByVal intY As Integer) As RadioButton
Dim rbnTmp As RadioButton
rbnTmp =
New RadioButton
rbnTmp.Text = strText
rbnTmp.Name = strName
rbnTmp.Width = intWidth
rbnTmp.Location =
New Point(intX, intY)
Return rbnTmp
End Function 'fncRadBtns
Private Sub fncOpen(ByVal obj As Object, ByVal ea As EventArgs) ' load the picture.
Try
dlgFile = New OpenFileDialog
dlgFile.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|Bitmaps (*.bmp)|*.bmp"
dlgFile.FilterIndex = 1
If dlgFile.ShowDialog() = DialogResult.OK Then
If
Not ((stm <<= dlgFile.OpenFile()) Is Nothing) Then 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
strImgName = dlgFile.FileName
stm.Close()
pbxImg.Image = Image.FromFile(strImgName)
blnPicLoaded =
True
End If
End
If
If
blnPicLoaded Then ' if the picture is loaded then enable the events.
AddHandler rbnRotNone.Click, AddressOf fncRot
AddHandler rbnRotX.Click, AddressOf fncRot
AddHandler rbnRotY.Click, AddressOf fncRot
AddHandler rbnRotXY.Click, AddressOf fncRot
AddHandler rbnFlipNone.Click, AddressOf fncFlip
AddHandler rbnFlip90.Click, AddressOf fncFlip
AddHandler rbnFlip180.Click, AddressOf fncFlip
AddHandler rbnFlip270.Click, AddressOf fncFlip
AddHandler btnTransform.Click, AddressOf fncTransform
AddHandler btnOrg.Click, AddressOf fncTransform
AddHandler cmbPicMode.SelectionChangeCommitted, AddressOf fncPicMode
End If
Catch
e As Exception
Console.WriteLine(e.StackTrace)
End Try
End
Sub
'fncOpen
Private Sub fncSave(ByVal sender As Object, ByVal ea As EventArgs)
Try ' save the image in the required format.
Dim dlgSave As New SaveFileDialog
dlgSave.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|Bitmaps (*.bmp)|*.bmp"
If dlgSave.ShowDialog() = DialogResult.OK Then
strImgName = dlgSave.FileName
If strImgName.EndsWith("jpg") Then
pbxImg.Image.Save(strImgName, ImageFormat.Jpeg)
End If
If
strImgName.EndsWith("gif") Then
pbxImg.Image.Save(strImgName, ImageFormat.Gif)
End If
If
strImgName.EndsWith("bmp") Then
pbxImg.Image.Save(strImgName, ImageFormat.Bmp)
End If
End
If
Catch
e As Exception
Console.WriteLine(e.StackTrace)
End Try
End
Sub
'fncSave
Private Sub fncRot(ByVal obj As Object, ByVal ea As EventArgs)
rbnTemp =
CType(obj, RadioButton)
strRot = rbnTemp.Name
setStatus()
End Sub
'fncRot
Private Sub fncFlip(ByVal obj As Object, ByVal ea As EventArgs)
rbnTemp =
CType(obj, RadioButton)
strFlip = rbnTemp.Name
setStatus()
End Sub 'fncFlip
Private Sub fncTransform(ByVal obj As Object, ByVal ea As EventArgs)
Dim btnTemp As Button = CType(obj, Button)
If btnTemp.Text = "Transform Image" Then
strRotFlip = strRot + strFlip
Select Case strRotFlip
Case "180None"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
Case "180X"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipX)
Case "180Y"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipY)
Case "180XY"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate180FlipXY)
Case "270None"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
Case "270X"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipX)
Case "270Y"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipY)
Case "270XY"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate270FlipXY)
Case "90None"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
Case "90X"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipX)
Case "90Y"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipY)
Case "90XY"
pbxImg.Image.RotateFlip(RotateFlipType.Rotate90FlipXY)
Case "NoneNone"
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone)
Case "NoneX"
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
Case "NoneY"
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
Case "NoneXY"
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipXY)
Case Else
pbxImg.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone)
End Select
Else
If btnTemp.Text = "Original Position" Then
pbxImg.Image = Image.FromFile(strImgName)
pbxImg.Refresh()
End If
End
If
pbxImg.Refresh()
End Sub
'fncTransform
Private Sub fncPicMode(ByVal obj As Object, ByVal ea As EventArgs)
Dim objTemp As ComboBox = CType(obj, ComboBox)
Select Case objTemp.SelectedIndex
Case 0
pbxImg.SizeMode = PictureBoxSizeMode.AutoSize
strStatus = "AutoSize mode - The PictureBox is sized equal to the size of the image that it contains."
Case 1
pbxImg.SizeMode = PictureBoxSizeMode.CenterImage
strStatus = "CenterImage mode - Image is placed in the center of the Picture Box."
strStatus = strStatus + "If the image is big then outside edges of Image is clipped."
Case 2
pbxImg.SizeMode = PictureBoxSizeMode.Normal
strStatus = "Normal mode - Image is clipped if it is bigger than the Picture Box."
Case 3
pbxImg.SizeMode = PictureBoxSizeMode.StretchImage
strStatus = "Stretch mode - Image is stretched or shrunk to fit the Picture Box."
End Select
pbxImg.Refresh()
stbBtm.Text = strStatus
End Sub 'fncPicMode
Private Sub setStatus()
strStatus = "The Image is rotated "
If Not (strRot Is Nothing) And strRot <> "None" Then
strStatus = strStatus + strRot + " degrees"
End If
If
Not (strFlip Is Nothing) And strFlip <> "None" Then
strStatus = strStatus + " around " + strFlip + " axis."
End If
stbBtm.Text = strStatus
End Sub 'setStatus
Private Sub fncExit(ByVal obj As Object, ByVal ea As EventArgs)
Application.Exit()
End Sub 'fncExit
End Class 'win


Similar Articles