VB.Net/GUI/PictureBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:43, 26 мая 2010
Содержание
Create Graphics from Bitmap and paint on a Picture Box
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
DrawDiamond()
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Resize
DrawDiamond()
End Sub
Private Sub DrawDiamond()
Dim wid As Integer = picCanvas.ClientSize.Width
Dim hgt As Integer = picCanvas.ClientSize.Height
" Do nothing if we have no size.
" This happens, for example, if the form is minimized.
If wid < 1 Or hgt < 1 Then Exit Sub
" Make a Bitmap and Graphics to fit.
Dim bm As New Bitmap(wid, hgt)
Dim gr As Graphics = Graphics.FromImage(bm)
Dim bg_brush As New HatchBrush(HatchStyle.HorizontalBrick, _
Color.Blue, Color.Aqua)
gr.FillRectangle(bg_brush, picCanvas.ClientRectangle)
" Display the result.
picCanvas.Image = bm
" Free resources.
bg_brush.Dispose()
gr.Dispose()
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads 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.picCanvas = New System.Windows.Forms.PictureBox
CType(Me.picCanvas, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"picCanvas
"
Me.picCanvas.Dock = System.Windows.Forms.DockStyle.Fill
Me.picCanvas.Location = New System.Drawing.Point(0, 0)
Me.picCanvas.Name = "picCanvas"
Me.picCanvas.Size = New System.Drawing.Size(292, 273)
Me.picCanvas.TabIndex = 0
Me.picCanvas.TabStop = False
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.picCanvas)
Me.Name = "Form1"
Me.Text = "AutoRedraw"
CType(Me.picCanvas, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents picCanvas As System.Windows.Forms.PictureBox
End Class
Display BMP, Ppeg and Gif Image in PictureBox
Imports System.Windows.Forms
Imports System.Drawing
Imports System.IO
Module Module1
Sub Main()
Application.Run(New Form1)
End Sub
End Module
Public Class Form1
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
"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.
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.Location = New System.Drawing.Point(32, 24)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(224, 216)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(104, 264)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Start"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 309)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "MyPhotos"
Me.ResumeLayout(False)
End Sub
#End Region
Public ImageFiles() As String
Public FileIndex As Integer
Public Sub ShowImages()
Dim FileDB As New OpenFileDialog()
FileDB.Multiselect = True
If (FileDB.ShowDialog() = DialogResult.OK) Then
ImageFiles = FileDB.FileNames
Me.Refresh()
If ImageFiles.Length = 0 Then
MsgBox("No files selected")
Me.Close()
Else
Dim IsImageFile As Boolean
Button1.Text = "End"
Dim MyImage As Image
FileIndex = 0
Do
IsImageFile = False
If (UCase(ImageFiles(FileIndex)).EndsWith("JPG")) Then
IsImageFile = True
End If
If (UCase(ImageFiles(FileIndex)).EndsWith("BMP")) Then
IsImageFile = True
End If
If (UCase(ImageFiles(FileIndex)).EndsWith("GIF")) Then
IsImageFile = True
End If
If (IsImageFile) Then
Me.Text = ImageFiles(FileIndex)
Try
MyImage = Image.FromFile(ImageFiles(FileIndex))
PictureBox1.Image = Image.FromFile(ImageFiles(FileIndex))
Display.Sleep(5000)
Catch Ex As Exception
End Try
End If
FileIndex = FileIndex + 1
If (FileIndex = ImageFiles.Length) Then
FileIndex = 0
End If
Loop While (True)
End If
Else
MsgBox("User selected Cancel")
Me.Close()
End If
End Sub
Dim Display As System.Threading.Thread
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FileDB As New OpenFileDialog()
FileDB.Multiselect = True
If (Button1.Text = "End") Then
Display.Abort()
Me.Close()
Else
Button1.Text = "End"
Display = New System.Threading.Thread(AddressOf ShowImages)
Display.Start()
End If
End Sub
End Class
Load Image into PictureBox
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)
End Sub
End Class
Public Class Form1
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 PictureBox1 As System.Windows.Forms.PictureBox
"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()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.SuspendLayout()
"
"PictureBox1
"
Me.PictureBox1.Location = New System.Drawing.Point(16, 8)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(264, 200)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 1
Me.PictureBox1.TabStop = False
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(292, 268)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "PictureBox demo"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PictureBox1.Image = New System.Drawing.Bitmap("figure2.bmp")
End Sub
End Class
Paint on a PictureBox
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class
Public Class Form1
Private Sub picCanvas3_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas3.Paint
e.Graphics.DrawEllipse(Pens.Red, _
picCanvas3.DisplayRectangle.X, _
picCanvas3.DisplayRectangle.Y, _
picCanvas3.DisplayRectangle.Width - 1, _
picCanvas3.DisplayRectangle.Height - 1)
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads 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.picCanvas3 = New System.Windows.Forms.PictureBox
CType(Me.picCanvas3, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"picCanvas3
"
Me.picCanvas3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.picCanvas3.Location = New System.Drawing.Point(360, 8)
Me.picCanvas3.Name = "picCanvas3"
Me.picCanvas3.Size = New System.Drawing.Size(168, 168)
Me.picCanvas3.TabIndex = 3
Me.picCanvas3.TabStop = False
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(536, 214)
Me.Controls.Add(Me.picCanvas3)
Me.Name = "Form1"
Me.Text = "UsePictureBox"
CType(Me.picCanvas3, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents picCanvas3 As System.Windows.Forms.PictureBox
End Class
Using a PictureBox to display images
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim myform As Form = New FrmPictureBox()
Application.Run(myform)
End Sub " Main
End Class
Public Class FrmPictureBox
Inherits System.Windows.Forms.Form
Private imageNumber As Integer = -1
#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
" create Image object from file, display in PictureBox
picImage.Image = Image.FromFile("figure2.bmp")
End Sub
End Class