VB.Net/2D/Draw Image

Материал из VB Эксперт
Перейти к: навигация, поиск

Displaying and resizing an image

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing.Text
Public Class MainClass
   Shared Sub Main()
        Dim myform As Form = New FrmDisplayLogo()
        Application.Run(myform)
   End Sub " Main
End Class


Public Class FrmDisplayLogo
   Inherits System.Windows.Forms.Form
   " width controls
   Friend WithEvents txtWidth As TextBox
   Friend WithEvents lblWidth As Label
   " height controls
   Friend WithEvents lblHeight As Label
   Friend WithEvents txtHeight As TextBox
   Private mGraphicsObject As Graphics
   Private mImage As Image
   " sets member variables on form load
   Private Sub FrmDisplayLogo_Load(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load
      " get Form"s graphics object
      mGraphicsObject = Me.CreateGraphics
      " load image
      mImage = Image.FromFile("figure2.bmp")
   End Sub " FrmDisplayLogo_Load
#Region " Windows Form Designer generated code "
   Public Sub New()
      MyBase.New()
      " This call is required by Windows Form Designer.
      InitializeComponent()
      " Add any initialization after InitializeComponent() call
   End Sub " new
   "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 cmdSetButton As System.Windows.Forms.Button
   "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.lblWidth = New System.Windows.Forms.Label()
      Me.lblHeight = New System.Windows.Forms.Label()
      Me.txtWidth = New System.Windows.Forms.TextBox()
      Me.txtHeight = New System.Windows.Forms.TextBox()
      Me.cmdSetButton = New System.Windows.Forms.Button()
      Me.SuspendLayout()
      "
      "lblWidth
      "
      Me.lblWidth.Location = New System.Drawing.Point(440, 56)
      Me.lblWidth.Name = "lblWidth"
      Me.lblWidth.TabIndex = 0
      Me.lblWidth.Text = "Width"
      "
      "lblHeight
      "
      Me.lblHeight.Location = New System.Drawing.Point(440, 128)
      Me.lblHeight.Name = "lblHeight"
      Me.lblHeight.TabIndex = 2
      Me.lblHeight.Text = "Height"
      "
      "txtWidth
      "
      Me.txtWidth.Location = New System.Drawing.Point(440, 88)
      Me.txtWidth.Name = "txtWidth"
      Me.txtWidth.TabIndex = 1
      Me.txtWidth.Text = ""
      "
      "txtHeight
      "
      Me.txtHeight.Location = New System.Drawing.Point(440, 160)
      Me.txtHeight.Name = "txtHeight"
      Me.txtHeight.TabIndex = 3
      Me.txtHeight.Text = ""
      "
      "cmdSetButton
      "
      Me.cmdSetButton.Location = New System.Drawing.Point(456, 200)
      Me.cmdSetButton.Name = "cmdSetButton"
      Me.cmdSetButton.TabIndex = 4
      Me.cmdSetButton.Text = "Set"
      "
      "FrmDisplayLogo
      "
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(568, 273)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdSetButton, Me.txtHeight, Me.lblHeight, Me.txtWidth, Me.lblWidth})
      Me.Name = "FrmDisplayLogo"
      Me.Text = "Display Logo"
      Me.ResumeLayout(False)
   End Sub
#End Region
   Private Sub cmdSetButton_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles cmdSetButton.Click

      Dim width As Integer = Convert.ToInt32(txtWidth.Text)
      Dim height As Integer = Convert.ToInt32(txtHeight.Text)

      If (width > 375 OrElse height > 225) Then
         MessageBox.Show("Height or Width too large")
         Return
      End If
      mGraphicsObject.Clear(Me.BackColor) 

      mGraphicsObject.DrawImage(mImage, 5, 5, width, height)
   End Sub 
End Class


Draw Image on a Frame

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.IO
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class
Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim bm As Bitmap = new Bitmap("figure2.bmp")
        " Make the bitmap"s "background" pixels transparent.
        bm.MakeTransparent(bm.GetPixel(1, 1))
        " Draw the bitmap onto the form.
        e.Graphics.DrawImage(bm, 0, 0)
    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 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.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(702, 303)
        Me.Name = "Form1"
        Me.Text = "ShowCursors"
        Me.ResumeLayout(False)
    End Sub
End Class


Process an Image: invert Pixel

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 System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bm As New Bitmap("figure2.bmp")
        Dim source_bm As New Bitmap(bm)
    
        bm.Dispose()
        picSource.Image = source_bm
        " Arrange the controls.
        picDest.Size = picSource.Size
        " Make the result Bitmap.
         Dim dest_bm As New Bitmap(source_bm)
        " Process the image"s pictures.
        For y As Integer = 0 To dest_bm.Height - 1
            For x As Integer = 0 To dest_bm.Width - 1
                " Get this pixel"s color.
                Dim clr As Color = dest_bm.GetPixel(x, y)
                " Invert the color"s components.
                clr = Color.FromArgb(255, _
                    255 - clr.R, _
                    255 - clr.G, _
                    255 - clr.B)
                " Set the result pixel"s color.
                dest_bm.SetPixel(x, y, clr)
            Next x
        Next y
        " Display the results.
        picDest.Image = dest_bm
    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.SplitContainer1 = New System.Windows.Forms.SplitContainer
        Me.picSource = New System.Windows.Forms.PictureBox
        Me.picDest = New System.Windows.Forms.PictureBox
        Me.SplitContainer1.Panel1.SuspendLayout()
        Me.SplitContainer1.Panel2.SuspendLayout()
        Me.SplitContainer1.SuspendLayout()
        CType(Me.picSource, System.ruponentModel.ISupportInitialize).BeginInit()
        CType(Me.picDest, System.ruponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        "
        "SplitContainer1
        "
        Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.SplitContainer1.Location = New System.Drawing.Point(0, 24)
        Me.SplitContainer1.Name = "SplitContainer1"
        "
        "SplitContainer1.Panel1
        "
        Me.SplitContainer1.Panel1.AutoScroll = True
        Me.SplitContainer1.Panel1.Controls.Add(Me.picSource)
        "
        "SplitContainer1.Panel2
        "
        Me.SplitContainer1.Panel2.AutoScroll = True
        Me.SplitContainer1.Panel2.Controls.Add(Me.picDest)
        Me.SplitContainer1.Size = New System.Drawing.Size(522, 249)
        Me.SplitContainer1.SplitterDistance = 270
        Me.SplitContainer1.TabIndex = 1
        Me.SplitContainer1.Text = "SplitContainer1"
        "
        "picSource
        "
        Me.picSource.AutoSize = True
        Me.picSource.Location = New System.Drawing.Point(0, 0)
        Me.picSource.Name = "picSource"
        Me.picSource.Size = New System.Drawing.Size(208, 184)
        Me.picSource.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.picSource.TabIndex = 0
        Me.picSource.TabStop = False
        "
        "picDest
        "
        Me.picDest.AutoSize = True
        Me.picDest.Location = New System.Drawing.Point(0, 0)
        Me.picDest.Name = "picDest"
        Me.picDest.Size = New System.Drawing.Size(184, 184)
        Me.picDest.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.picDest.TabIndex = 1
        Me.picDest.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(522, 273)
        Me.Controls.Add(Me.SplitContainer1)
        Me.Name = "Form1"
        Me.Text = "InvertImageGetSetPixels"
        Me.SplitContainer1.Panel1.ResumeLayout(False)
        Me.SplitContainer1.Panel1.PerformLayout()
        Me.SplitContainer1.Panel2.ResumeLayout(False)
        Me.SplitContainer1.Panel2.PerformLayout()
        Me.SplitContainer1.ResumeLayout(False)
        CType(Me.picSource, System.ruponentModel.ISupportInitialize).EndInit()
        CType(Me.picDest, System.ruponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer
    Friend WithEvents picSource As System.Windows.Forms.PictureBox
    Friend WithEvents picDest As System.Windows.Forms.PictureBox
End Class