VB.Net Tutorial/GUI/PictureBox
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Draw on a PictureBox
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawOnaPictureBox
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
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
Private components As System.ruponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(292, 168)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(104, 184)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 32)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Draw"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 238)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.ResumeLayout(False)
End Sub
Dim pen1 As New System.Drawing.Pen(Color.Green, 4)
Dim g As System.Drawing.Graphics
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Refresh()
g = PictureBox1.CreateGraphics
g.DrawEllipse(pen1, 70, 10, 100, 150)
End Sub
End Class
Draw on PictureBox border
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawOnPictureBorder
public Shared Sub Main
Application.Run(New frmViewer)
End Sub
End Class
Public Class frmViewer
Private Sub btnSelectPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPicture.Click
picShowPicture.Image = Image.FromFile("yourfile.jpg")
End Sub
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnDrawBorder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawBorder.Click
Dim objGraphics As Graphics
objGraphics = Me.CreateGraphics
objGraphics.Clear(SystemColors.Control)
objGraphics.DrawRectangle(Pens.Red, _
picShowPicture.Left - 1, picShowPicture.Top - 1, _
picShowPicture.Width + 1, picShowPicture.Height + 1)
objGraphics.Dispose()
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class frmViewer
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnSelectPicture = New System.Windows.Forms.Button
Me.btnQuit = New System.Windows.Forms.Button
Me.picShowPicture = New System.Windows.Forms.PictureBox
Me.ofdSelectPicture = New System.Windows.Forms.OpenFileDialog
Me.btnDrawBorder = New System.Windows.Forms.Button
CType(Me.picShowPicture, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"btnSelectPicture
"
Me.btnSelectPicture.Location = New System.Drawing.Point(301, 10)
Me.btnSelectPicture.Name = "btnSelectPicture"
Me.btnSelectPicture.Size = New System.Drawing.Size(85, 23)
Me.btnSelectPicture.TabIndex = 0
Me.btnSelectPicture.Text = "Select Picture"
Me.btnSelectPicture.UseVisualStyleBackColor = True
"
"btnQuit
"
Me.btnQuit.Location = New System.Drawing.Point(301, 40)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.Size = New System.Drawing.Size(85, 23)
Me.btnQuit.TabIndex = 1
Me.btnQuit.Text = "Quit"
Me.btnQuit.UseVisualStyleBackColor = True
"
"picShowPicture
"
Me.picShowPicture.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picShowPicture.Location = New System.Drawing.Point(8, 8)
Me.picShowPicture.Name = "picShowPicture"
Me.picShowPicture.Size = New System.Drawing.Size(282, 275)
Me.picShowPicture.TabIndex = 2
Me.picShowPicture.TabStop = False
"
"ofdSelectPicture
"
Me.ofdSelectPicture.Filter = "Windows Bitmaps|*.BMP|JPEG Files|*.JPG"
Me.ofdSelectPicture.Title = "Select Picture"
"
"btnDrawBorder
"
Me.btnDrawBorder.Location = New System.Drawing.Point(301, 69)
Me.btnDrawBorder.Name = "btnDrawBorder"
Me.btnDrawBorder.Size = New System.Drawing.Size(85, 23)
Me.btnDrawBorder.TabIndex = 5
Me.btnDrawBorder.Text = "Draw Border"
Me.btnDrawBorder.UseVisualStyleBackColor = True
"
"frmViewer
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(392, 291)
Me.Controls.Add(Me.btnDrawBorder)
Me.Controls.Add(Me.picShowPicture)
Me.Controls.Add(Me.btnQuit)
Me.Controls.Add(Me.btnSelectPicture)
Me.Name = "frmViewer"
Me.Text = "Picture Viewer"
CType(Me.picShowPicture, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents btnSelectPicture As System.Windows.Forms.Button
Friend WithEvents btnQuit As System.Windows.Forms.Button
Friend WithEvents picShowPicture As System.Windows.Forms.PictureBox
Friend WithEvents ofdSelectPicture As System.Windows.Forms.OpenFileDialog
Friend WithEvents btnEnlarge As System.Windows.Forms.Button
Friend WithEvents btnShrink As System.Windows.Forms.Button
Friend WithEvents btnDrawBorder As System.Windows.Forms.Button
End Class
Get Graphics from PictureBox
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawFilledShape
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
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
Private components As System.ruponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(292, 168)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(16, 184)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 32)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Ellipse"
"
"Button2
"
Me.Button2.Location = New System.Drawing.Point(112, 184)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Rectangle"
"
"Button3
"
Me.Button3.Location = New System.Drawing.Point(208, 184)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(72, 32)
Me.Button3.TabIndex = 3
Me.Button3.Text = "Pie"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 238)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.ResumeLayout(False)
End Sub
Dim brush1 As System.Drawing.Brush
Dim g As System.Drawing.Graphics
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
brush1 = New SolidBrush(Color.Green)
g = PictureBox1.CreateGraphics
g.FillEllipse(brush1, 30, 30, 100, 150)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
brush1 = New SolidBrush(Color.Green)
g = PictureBox1.CreateGraphics
g.FillRectangle(brush1, 30, 30, 100, 100)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
brush1 = New SolidBrush(Color.Green)
g = PictureBox1.CreateGraphics
g.FillPie(brush1, 30, 30, 120, 120, 170, 200)
End Sub
End Class
Load image to PictureBox
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class PictureBoxImageFromOpenFileDialog
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
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
Private components As System.ruponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.Location = New System.Drawing.Point(16, 16)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(248, 176)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(96, 208)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Open"
"
"OpenFileDialog1
"
Me.OpenFileDialog1.Filter = "bmp(*.bmp)|*.bmp"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.ResumeLayout(False)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pic As Image
OpenFileDialog1.ShowDialog()
pic = New Bitmap(OpenFileDialog1.FileName)
PictureBox1.Image = pic
End Sub
End Class
PictureBox Mouse down event
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ColorPicker
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
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
Private components As System.ruponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
Me.PictureBox1.Image = Image.FromFile("YourFile.bmp")
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(292, 216)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(24, 232)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(56, 24)
Me.Label1.TabIndex = 1
Me.Label1.Text = "R:"
"
"Label2
"
Me.Label2.Location = New System.Drawing.Point(112, 232)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(64, 24)
Me.Label2.TabIndex = 2
Me.Label2.Text = "G:"
"
"Label3
"
Me.Label3.Location = New System.Drawing.Point(208, 232)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(64, 24)
Me.Label3.TabIndex = 3
Me.Label3.Text = "B:"
"
"MainMenu1
"
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
"
"MenuItem1
"
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3})
Me.MenuItem1.Text = "File"
"
"MenuItem2
"
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "Open"
"
"MenuItem3
"
Me.MenuItem3.Index = 1
Me.MenuItem3.Text = "Exit"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 270)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.ResumeLayout(False)
End Sub
Dim pic As Bitmap
Dim c As System.Drawing.Color
Dim xwidth, yheight As Integer
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
pic = PictureBox1.Image
xwidth = pic.Width
yheight = pic.Height
If e.X < xwidth And e.Y < yheight Then
c = pic.GetPixel(e.X, e.Y)
Label1.Text = "R:" + c.R.ToString
Label2.Text = "G:" + c.G.ToString
Label3.Text = "B:" + c.B.ToString
End If
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
OpenFileDialog1.ShowDialog()
PictureBox1.Image = New Bitmap(OpenFileDialog1.FileName)
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
End
End Sub
End Class
PictureBox: Save an Image
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ImagePictureBoxResizeMode
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
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
Private components As System.ruponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.MenuItem5 = New System.Windows.Forms.MenuItem
Me.MenuItem6 = New System.Windows.Forms.MenuItem
Me.MenuItem7 = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
Me.MenuItem4 = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(292, 266)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"OpenFileDialog1
"
Me.OpenFileDialog1.Filter = "BMP(*.bmp)|*.bmp"
"
"SaveFileDialog1
"
Me.SaveFileDialog1.Filter = "BMP(*.bmp)|*.bmp"
"
"MainMenu1
"
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
"
"MenuItem1
"
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem5, Me.MenuItem6, Me.MenuItem7, Me.MenuItem3, Me.MenuItem4})
Me.MenuItem1.Text = "File"
"
"MenuItem2
"
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "Open"
"
"MenuItem5
"
Me.MenuItem5.Index = 1
Me.MenuItem5.Text = "Resize"
"
"MenuItem6
"
Me.MenuItem6.Index = 2
Me.MenuItem6.Text = "Center"
"
"MenuItem7
"
Me.MenuItem7.Index = 3
Me.MenuItem7.Text = "Restore"
"
"MenuItem3
"
Me.MenuItem3.Index = 4
Me.MenuItem3.Text = "Save"
"
"MenuItem4
"
Me.MenuItem4.Index = 5
Me.MenuItem4.Text = "Exit"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.ResumeLayout(False)
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim path As String
Dim pic As Image
OpenFileDialog1.ShowDialog()
pic = New Bitmap(OpenFileDialog1.FileName)
PictureBox1.Image = pic
End Sub
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim path As String
Dim pic As Image
pic = PictureBox1.Image
SaveFileDialog1.ShowDialog()
pic.Save(SaveFileDialog1.FileName)
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
End
End Sub
End Class
PictureBox: StretchImage, CenterImage
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ImagePictureBoxResizeMode
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
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
Private components As System.ruponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.MenuItem5 = New System.Windows.Forms.MenuItem
Me.MenuItem6 = New System.Windows.Forms.MenuItem
Me.MenuItem7 = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
Me.MenuItem4 = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(292, 266)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"OpenFileDialog1
"
Me.OpenFileDialog1.Filter = "BMP(*.bmp)|*.bmp"
"
"SaveFileDialog1
"
Me.SaveFileDialog1.Filter = "BMP(*.bmp)|*.bmp"
"
"MainMenu1
"
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
"
"MenuItem1
"
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem5, Me.MenuItem6, Me.MenuItem7, Me.MenuItem3, Me.MenuItem4})
Me.MenuItem1.Text = "File"
"
"MenuItem2
"
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "Open"
"
"MenuItem5
"
Me.MenuItem5.Index = 1
Me.MenuItem5.Text = "Resize"
"
"MenuItem6
"
Me.MenuItem6.Index = 2
Me.MenuItem6.Text = "Center"
"
"MenuItem7
"
Me.MenuItem7.Index = 3
Me.MenuItem7.Text = "Restore"
"
"MenuItem3
"
Me.MenuItem3.Index = 4
Me.MenuItem3.Text = "Save"
"
"MenuItem4
"
Me.MenuItem4.Index = 5
Me.MenuItem4.Text = "Exit"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.ResumeLayout(False)
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim path As String
Dim pic As Image
OpenFileDialog1.ShowDialog()
pic = New Bitmap(OpenFileDialog1.FileName)
PictureBox1.Image = pic
End Sub
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim path As String
Dim pic As Image
pic = PictureBox1.Image
SaveFileDialog1.ShowDialog()
pic.Save(SaveFileDialog1.FileName)
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
End
End Sub
End Class
Using a PictureBox to display images
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DisplayImageInPictureBox
public Shared Sub Main
Application.Run(New FrmPictureBox)
End Sub
End class
Public Class FrmPictureBox
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
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
Friend WithEvents picImage As System.Windows.Forms.Label
Friend WithEvents lblPrompt As System.Windows.Forms.Label
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.picImage = New System.Windows.Forms.Label()
Me.lblPrompt = New System.Windows.Forms.Label()
Me.SuspendLayout()
"
"picImage
"
Me.picImage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.picImage.Location = New System.Drawing.Point(16, 48)
Me.picImage.Name = "picImage"
Me.picImage.Size = New System.Drawing.Size(264, 208)
Me.picImage.TabIndex = 1
"
"lblPrompt
"
Me.lblPrompt.Location = New System.Drawing.Point(16, 16)
Me.lblPrompt.Name = "lblPrompt"
Me.lblPrompt.Size = New System.Drawing.Size(264, 23)
Me.lblPrompt.TabIndex = 0
Me.lblPrompt.Text = "Click On PictureBox to View Images"
"
"FrmPictureBox
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.picImage, Me.lblPrompt})
Me.Name = "FrmPictureBox"
Me.Text = "PictureBoxTest"
Me.ResumeLayout(False)
End Sub
#End Region
" replace image in picImage
Private Sub picImage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles picImage.Click
picImage.Image = Image.FromFile("YourFile.bmp")
End Sub
End Class