VB.Net/2D/Image

Материал из VB Эксперт
Версия от 15:41, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Convert Image Format

<source lang="vbnet"> Public Class MainClass

   Public Shared Sub Main()
       ConvertImage("sample.gif", System.Drawing.Imaging.ImageFormat.Bmp, "new.bmp")
   End Sub
   Public Shared Sub ConvertImage(ByVal Filename As String, ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, ByVal NewFilename As String)
       Try
           Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromFile(Filename)
           imgFile.Save(NewFilename, DesiredFormat)
       Catch ex As Exception
           Throw ex
       End Try
   End Sub

End Class


 </source>


Image Recolor Demo

<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New RecoloringForm()
       Application.Run(myform)
   End Sub

End Class Public Class RecoloringForm

   Inherits System.Windows.Forms.Form
  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
       Me.SetStyle(ControlStyles.ResizeRedraw, True)
       Me.SetStyle(ControlStyles.DoubleBuffer, True)
       Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
   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 groupBox2 As System.Windows.Forms.GroupBox
   Friend WithEvents panel2 As System.Windows.Forms.Panel
   Friend WithEvents splitter1 As System.Windows.Forms.Splitter
   Friend WithEvents groupBox1 As System.Windows.Forms.GroupBox
   Friend WithEvents panel1 As System.Windows.Forms.Panel
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.groupBox2 = New System.Windows.Forms.GroupBox()
       Me.panel2 = New System.Windows.Forms.Panel()
       Me.splitter1 = New System.Windows.Forms.Splitter()
       Me.groupBox1 = New System.Windows.Forms.GroupBox()
       Me.panel1 = New System.Windows.Forms.Panel()
       Me.groupBox2.SuspendLayout()
       Me.groupBox1.SuspendLayout()
       Me.SuspendLayout()
       "
       "groupBox2
       "
       Me.groupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.panel2})
       Me.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill
       Me.groupBox2.Location = New System.Drawing.Point(187, 0)
       Me.groupBox2.Name = "groupBox2"
       Me.groupBox2.Size = New System.Drawing.Size(205, 182)
       Me.groupBox2.TabIndex = 5
       Me.groupBox2.TabStop = False
       Me.groupBox2.Text = "Mapped Colors"
       "
       "panel2
       "
       Me.panel2.BackColor = System.Drawing.Color.White
       Me.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.panel2.Dock = System.Windows.Forms.DockStyle.Fill
       Me.panel2.Location = New System.Drawing.Point(3, 16)
       Me.panel2.Name = "panel2"
       Me.panel2.Size = New System.Drawing.Size(199, 163)
       Me.panel2.TabIndex = 0
       "
       "splitter1
       "
       Me.splitter1.Location = New System.Drawing.Point(184, 0)
       Me.splitter1.Name = "splitter1"
       Me.splitter1.Size = New System.Drawing.Size(3, 182)
       Me.splitter1.TabIndex = 4
       Me.splitter1.TabStop = False
       "
       "groupBox1
       "
       Me.groupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.panel1})
       Me.groupBox1.Dock = System.Windows.Forms.DockStyle.Left
       Me.groupBox1.Name = "groupBox1"
       Me.groupBox1.Size = New System.Drawing.Size(184, 182)
       Me.groupBox1.TabIndex = 3
       Me.groupBox1.TabStop = False
       Me.groupBox1.Text = "Original Colors"
       "
       "panel1
       "
       Me.panel1.BackColor = System.Drawing.Color.White
       Me.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill
       Me.panel1.Location = New System.Drawing.Point(3, 16)
       Me.panel1.Name = "panel1"
       Me.panel1.Size = New System.Drawing.Size(178, 163)
       Me.panel1.TabIndex = 0
       "
       "RecoloringForm
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(392, 182)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.groupBox2, Me.splitter1, Me.groupBox1})
       Me.Name = "RecoloringForm"
       Me.Text = "RecoloringForm"
       Me.groupBox2.ResumeLayout(False)
       Me.groupBox1.ResumeLayout(False)
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles panel1.Paint
       Dim g As Graphics = e.Graphics
       Dim bmp As Bitmap = New Bitmap("figure2.BMP")
       Dim rect As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
       rect.Offset((Me.panel1.ClientRectangle.Width - rect.Width) / 2, (Me.panel1.ClientRectangle.Height - rect.Height) / 2)
       g.DrawImage(bmp, rect)
   End Sub
   Private Sub panel2_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles panel2.Paint
       Dim g As Graphics = e.Graphics
       Dim bmp As Bitmap = New Bitmap("figure2.BMP")
       Dim colorMap As ColorMap() = New ColorMap() {New ColorMap()}
       colorMap(0).OldColor = bmp.GetPixel(0, bmp.Height - 1)
       colorMap(0).NewColor = Color.White
       Dim attr As ImageAttributes = New ImageAttributes()
       attr.SetRemapTable(colorMap)
       Dim rect As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
       rect.Offset((Me.panel2.ClientRectangle.Width - rect.Width) / 2, (Me.panel2.ClientRectangle.Height - rect.Height) / 2)
       g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, g.PageUnit, attr)
   End Sub

End Class


 </source>


Image Scale Clip Form

<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New ScalingClippingForm()
       Application.Run(myform)
   End Sub

End Class Public Class ScalingClippingForm

   Inherits System.Windows.Forms.Form
  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
       Me.SetStyle(ControlStyles.ResizeRedraw, True)
       Me.SetStyle(ControlStyles.DoubleBuffer, True)
       Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
   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 splitter1 As System.Windows.Forms.Splitter
   Friend WithEvents groupBox1 As System.Windows.Forms.GroupBox
   Friend WithEvents panel1 As System.Windows.Forms.Panel
   Friend WithEvents groupBox2 As System.Windows.Forms.GroupBox
   Friend WithEvents panel2 As System.Windows.Forms.Panel
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.splitter1 = New System.Windows.Forms.Splitter()
       Me.groupBox1 = New System.Windows.Forms.GroupBox()
       Me.panel1 = New System.Windows.Forms.Panel()
       Me.groupBox2 = New System.Windows.Forms.GroupBox()
       Me.panel2 = New System.Windows.Forms.Panel()
       Me.groupBox1.SuspendLayout()
       Me.groupBox2.SuspendLayout()
       Me.SuspendLayout()
       "
       "splitter1
       "
       Me.splitter1.Location = New System.Drawing.Point(184, 0)
       Me.splitter1.Name = "splitter1"
       Me.splitter1.Size = New System.Drawing.Size(3, 182)
       Me.splitter1.TabIndex = 4
       Me.splitter1.TabStop = False
       "
       "groupBox1
       "
       Me.groupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.panel1})
       Me.groupBox1.Dock = System.Windows.Forms.DockStyle.Left
       Me.groupBox1.Name = "groupBox1"
       Me.groupBox1.Size = New System.Drawing.Size(184, 182)
       Me.groupBox1.TabIndex = 3
       Me.groupBox1.TabStop = False
       Me.groupBox1.Text = "Scaling"
       "
       "panel1
       "
       Me.panel1.BackColor = System.Drawing.Color.White
       Me.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.panel1.Dock = System.Windows.Forms.DockStyle.Fill
       Me.panel1.Location = New System.Drawing.Point(3, 16)
       Me.panel1.Name = "panel1"
       Me.panel1.Size = New System.Drawing.Size(178, 163)
       Me.panel1.TabIndex = 0
       "
       "groupBox2
       "
       Me.groupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.panel2})
       Me.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill
       Me.groupBox2.Name = "groupBox2"
       Me.groupBox2.Size = New System.Drawing.Size(392, 182)
       Me.groupBox2.TabIndex = 5
       Me.groupBox2.TabStop = False
       Me.groupBox2.Text = "Clipping"
       "
       "panel2
       "
       Me.panel2.BackColor = System.Drawing.Color.White
       Me.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
       Me.panel2.Dock = System.Windows.Forms.DockStyle.Fill
       Me.panel2.Location = New System.Drawing.Point(3, 16)
       Me.panel2.Name = "panel2"
       Me.panel2.Size = New System.Drawing.Size(386, 163)
       Me.panel2.TabIndex = 0
       "
       "ScalingClippingForm
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(392, 182)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.splitter1, Me.groupBox1, Me.groupBox2})
       Me.Name = "ScalingClippingForm"
       Me.Text = "ScalingClippingFor"
       Me.groupBox1.ResumeLayout(False)
       Me.groupBox2.ResumeLayout(False)
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles panel1.Paint
       Dim g As Graphics = e.Graphics
       Dim bmp As Bitmap = New Bitmap("figure2.bmp")
       Dim rect As Rectangle = New Rectangle(10, 10, Me.panel1.ClientRectangle.Width - 20, Me.panel1.ClientRectangle.Height - 20)
       g.DrawImage(bmp, rect)
       g.DrawRectangle(Pens.Black, rect)
   End Sub
   Private Sub panel2_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles panel2.Paint
       Dim g As Graphics = e.Graphics
       Dim bmp As Bitmap = New Bitmap("figure2.bmp")
       Dim destRect As Rectangle = New Rectangle(10, 10, Me.panel2.ClientRectangle.Width - 20, Me.panel2.ClientRectangle.Height - 20)
       Dim srcRect As Rectangle = New Rectangle(0, 0, destRect.Width, destRect.Height)
       g.DrawImage(bmp, destRect, srcRect, g.PageUnit)
       g.DrawRectangle(Pens.Black, destRect)
   End Sub

End Class


 </source>


Image Skew Demo

<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New SkewingForm()
       Application.Run(myform)
   End Sub

End Class Public Class SkewingForm

   Inherits System.Windows.Forms.Form
  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
   "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 upButton As System.Windows.Forms.Button
   Friend WithEvents panel1 As System.Windows.Forms.Panel
   Friend WithEvents downButton As System.Windows.Forms.Button
   Friend WithEvents leftButton As System.Windows.Forms.Button
   Friend WithEvents rightButton As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.upButton = New System.Windows.Forms.Button()
       Me.panel1 = New System.Windows.Forms.Panel()
       Me.downButton = New System.Windows.Forms.Button()
       Me.leftButton = New System.Windows.Forms.Button()
       Me.rightButton = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "upButton
       "
       Me.upButton.Anchor = System.Windows.Forms.AnchorStyles.Top
       Me.upButton.Location = New System.Drawing.Point(132, 10)
       Me.upButton.Name = "upButton"
       Me.upButton.Size = New System.Drawing.Size(24, 23)
       Me.upButton.TabIndex = 5
       Me.upButton.Text = "^"
       "
       "panel1
       "
       Me.panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
       Me.panel1.Location = New System.Drawing.Point(36, 43)
       Me.panel1.Name = "panel1"
       Me.panel1.Size = New System.Drawing.Size(216, 184)
       Me.panel1.TabIndex = 2
       "
       "downButton
       "
       Me.downButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom
       Me.downButton.Location = New System.Drawing.Point(132, 234)
       Me.downButton.Name = "downButton"
       Me.downButton.Size = New System.Drawing.Size(24, 23)
       Me.downButton.TabIndex = 6
       Me.downButton.Text = "v"
       "
       "leftButton
       "
       Me.leftButton.Anchor = System.Windows.Forms.AnchorStyles.Left
       Me.leftButton.Location = New System.Drawing.Point(6, 124)
       Me.leftButton.Name = "leftButton"
       Me.leftButton.Size = New System.Drawing.Size(24, 23)
       Me.leftButton.TabIndex = 3
       Me.leftButton.Text = "<"
       "
       "rightButton
       "
       Me.rightButton.Anchor = System.Windows.Forms.AnchorStyles.Right
       Me.rightButton.Location = New System.Drawing.Point(262, 124)
       Me.rightButton.Name = "rightButton"
       Me.rightButton.Size = New System.Drawing.Size(24, 23)
       Me.rightButton.TabIndex = 4
       Me.rightButton.Text = ">"
       "
       "SkewingForm
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 266)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.upButton, Me.panel1, Me.downButton, Me.leftButton, Me.rightButton})
       Me.Name = "SkewingForm"
       Me.Text = "SkewingForm"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Dim bmp As Bitmap = New Bitmap("figure2.bmp")
   Dim offset As Size = New Size(0, 0)
   Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles panel1.Paint
       Dim g As Graphics = e.Graphics
       Dim rect As Rectangle = Me.panel1.ClientRectangle
       Dim points As Point() = New Point() { _
           New Point(rect.Left + offset.Width, rect.Top + offset.Height), _
           New Point(rect.Right, rect.Top + offset.Height), _
           New Point(rect.Left, rect.Bottom - offset.Height)}
       g.DrawImage(bmp, points)
   End Sub
   Private Sub upButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles upButton.Click
       offset.Height = offset.Height + 10
       panel1.Refresh()
   End Sub
   Private Sub leftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftButton.Click
       offset.Width = offset.Width - 10
       panel1.Refresh()
   End Sub
   Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click
       offset.Height = offset.Height - 10
       panel1.Refresh()
   End Sub
   Private Sub rightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightButton.Click
       offset.Width = offset.Width + 10
       panel1.Refresh()
   End Sub

End Class


 </source>


Move an Image

<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New PanningForm()
       Application.Run(myform)
   End Sub

End Class Public Class PanningForm

   Inherits System.Windows.Forms.Form
  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
   "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 upButton As System.Windows.Forms.Button
   Friend WithEvents panel1 As System.Windows.Forms.Panel
   Friend WithEvents downButton As System.Windows.Forms.Button
   Friend WithEvents leftButton As System.Windows.Forms.Button
   Friend WithEvents rightButton As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.upButton = New System.Windows.Forms.Button()
       Me.panel1 = New System.Windows.Forms.Panel()
       Me.downButton = New System.Windows.Forms.Button()
       Me.leftButton = New System.Windows.Forms.Button()
       Me.rightButton = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "upButton
       "
       Me.upButton.Anchor = System.Windows.Forms.AnchorStyles.Top
       Me.upButton.Location = New System.Drawing.Point(132, 10)
       Me.upButton.Name = "upButton"
       Me.upButton.Size = New System.Drawing.Size(24, 23)
       Me.upButton.TabIndex = 5
       Me.upButton.Text = "^"
       "
       "panel1
       "
       Me.panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
       Me.panel1.Location = New System.Drawing.Point(36, 43)
       Me.panel1.Name = "panel1"
       Me.panel1.Size = New System.Drawing.Size(216, 184)
       Me.panel1.TabIndex = 2
       "
       "downButton
       "
       Me.downButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom
       Me.downButton.Location = New System.Drawing.Point(132, 234)
       Me.downButton.Name = "downButton"
       Me.downButton.Size = New System.Drawing.Size(24, 23)
       Me.downButton.TabIndex = 6
       Me.downButton.Text = "v"
       "
       "leftButton
       "
       Me.leftButton.Anchor = System.Windows.Forms.AnchorStyles.Left
       Me.leftButton.Location = New System.Drawing.Point(6, 124)
       Me.leftButton.Name = "leftButton"
       Me.leftButton.Size = New System.Drawing.Size(24, 23)
       Me.leftButton.TabIndex = 3
       Me.leftButton.Text = "<"
       "
       "rightButton
       "
       Me.rightButton.Anchor = System.Windows.Forms.AnchorStyles.Right
       Me.rightButton.Location = New System.Drawing.Point(262, 124)
       Me.rightButton.Name = "rightButton"
       Me.rightButton.Size = New System.Drawing.Size(24, 23)
       Me.rightButton.TabIndex = 4
       Me.rightButton.Text = ">"
       "
       "PanningForm
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(292, 266)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.upButton, Me.panel1, Me.downButton, Me.leftButton, Me.rightButton})
       Me.Name = "PanningForm"
       Me.Text = "PanningForm"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Dim bmp As Bitmap = New Bitmap("figure2.bmp")
   Dim offset As Size = New Size(0, 0)
   Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles panel1.Paint
       Dim g As Graphics = e.Graphics
       Dim destRect As Rectangle = Me.panel1.ClientRectangle
       Dim srcRect As Rectangle = New Rectangle(offset.Width, offset.Height, destRect.Width, destRect.Height)
       g.DrawImage(bmp, destRect, srcRect, g.PageUnit)
   End Sub
   Private Sub upButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles upButton.Click
       offset.Height = offset.Height + 10
       panel1.Refresh()
   End Sub
   Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click
       offset.Height = offset.Height - 10
       panel1.Refresh()
   End Sub
   Private Sub rightButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rightButton.Click
       offset.Width = offset.Width + 10
       panel1.Refresh()
   End Sub
   Private Sub leftButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles leftButton.Click
       offset.Width = offset.Width - 10
       panel1.Refresh()
   End Sub

End Class


 </source>


Print Image

<source lang="vbnet"> 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
 Private MyImage As Image
 Private pd As PrintDocument
 Private Preview As PrintPreviewDialog
  1. Region " Windows Form Designer generated code "
 Public Sub New()
   MyBase.New()
   InitializeComponent()
   MyImage = Bitmap.FromFile("figure2.bmp")
   Preview = New PrintPreviewDialog()
   Preview.UseAntiAlias = True
 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 lblPrint As System.Windows.Forms.Label
 Friend WithEvents cmdShow As System.Windows.Forms.Button
 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
   Me.lblPrint = New System.Windows.Forms.Label()
   Me.cmdShow = New System.Windows.Forms.Button()
   Me.SuspendLayout()
   "
   "lblPrint
   "
   Me.lblPrint.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
   Me.lblPrint.Location = New System.Drawing.Point(152, 16)
   Me.lblPrint.Name = "lblPrint"
   Me.lblPrint.Size = New System.Drawing.Size(280, 136)
   Me.lblPrint.TabIndex = 3
   "
   "cmdShow
   "
   Me.cmdShow.Location = New System.Drawing.Point(360, 328)
   Me.cmdShow.Name = "cmdShow"
   Me.cmdShow.Size = New System.Drawing.Size(72, 24)
   Me.cmdShow.TabIndex = 2
   Me.cmdShow.Text = "Show"
   "
   "Form1
   "
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(442, 373)
   Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblPrint, Me.cmdShow})
   Me.MaximizeBox = False
   Me.MinimizeBox = False
   Me.Name = "Form1"
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
   Me.Text = "Form1"
   Me.ResumeLayout(False)
 End Sub
  1. End Region
 Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
   pd = New PrintDocument()
   AddHandler pd.PrintPage, New PrintPageEventHandler(AddressOf Me.pd_Print)
   Preview.Document = pd
 End Sub
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
   DrawIt(e.Graphics)
 End Sub
 Private Sub pd_Print(ByVal sender As Object, ByVal e As PrintPageEventArgs)
   lblPrint.Text += sender.ToString() 
   DrawIt(e.Graphics)
 End Sub
 Private Sub DrawIt(ByVal G As Graphics)
   G.SmoothingMode = SmoothingMode.AntiAlias
   G.DrawImage(MyImage, 10, 10)
 End Sub
 Private Sub cmdShow_Click(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles cmdShow.Click
   Preview.WindowState = FormWindowState.Maximized
   pd.DocumentName = DateTime.Now.Ticks.ToString()
   Preview.ShowDialog()
 End Sub

End Class


 </source>