VB.Net Tutorial/2D Graphics/Bitmap

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

Create an Bitmap and paint on it

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class CreateBitmap
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
public class Form1
  Inherits System.Windows.Forms.Form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim bm As New Bitmap("yourfile.jpg")
        Dim new_bm As New Bitmap(bm)
        bm.Dispose()
        Dim gr As Graphics = Graphics.FromImage(new_bm)
        gr.DrawEllipse(Pens.White, 0, 0, new_bm.Width - 1, new_bm.Height - 1)
  
        e.Graphics.DrawImage(new_bm,0,0,400,400)
  End Sub
    Private Sub InvertImage(ByVal bm As Bitmap)
        For y As Integer = 0 To bm.Height - 1
            For x As Integer = 0 To bm.Width - 1
                Dim clr As Color = bm.GetPixel(x, y)
                clr = Color.FromArgb(255,255 - clr.R, 255 - clr.G, 255 - clr.B)
                bm.SetPixel(x, y, clr)
            Next x
        Next y
    End Sub
  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  End Sub
End Class

Create Bitmap from Icon

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawIcons
   public Shared Sub Main
        Application.Run(New 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
    "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()
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(344, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"
    End Sub
#End Region

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        " Create an Icon
        Dim curIcon As New Icon("1.ico")
        " Create a Bitmap from Icon
        Dim bmp As Bitmap = curIcon.ToBitmap()
        " Draw Bitmap
        Dim g As Graphics = e.Graphics
        g.Clear(Me.BackColor)
        g.DrawImage(bmp, 10, 10)
        g.Dispose()
    End Sub
End Class

Make Bitmap transparent

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
public class MakeTransparentBitmap
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Me.Close()
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim bm As Bitmap = new Bitmap("yourfile.jpg")
        bm.MakeTransparent(bm.GetPixel(1, 1))
        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(203, 201)
        Me.ControlBox = False
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Form1"
        Me.TransparencyKey = System.Drawing.SystemColors.Control
        Me.ResumeLayout(False)
    End Sub
End Class

Set Pixel for Bitmap

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class SetPixelImage
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
public class Form1
  Inherits System.Windows.Forms.Form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim newImage As Bitmap = Bitmap.FromFile("yourfile.jpg")
        Dim i As Integer
        For i = 0 To 59
            Dim j As Integer
            For j = 0 To 59
                newImage.SetPixel(i, j, Color.Red)
            Next j
        Next i
        
        e.Graphics.DrawImage(newImage, Me.ClientRectangle)
  End Sub
  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  End Sub
End Class

Set Resolution for Bitmap

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class SetResolutionBitmap
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
public class Form1
  Inherits System.Windows.Forms.Form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim newImage As Bitmap = Bitmap.FromFile("yourfile.jpg")
        Dim hDpi As Single = 90
        Dim vDpi As Single = 90
        newImage.SetResolution(hDpi, vDpi)
        
        e.Graphics.DrawImage(newImage, Me.ClientRectangle)
  End Sub
  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  End Sub
End Class