Invert an Image unsafe : Image Operation « 2D Graphics « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » 2D Graphics » Image Operation 
17.49.9.Invert an Image unsafe
Invert an Image unsafe
'Visual Basic 2005 Programmer's Reference
'by Rod Stephens (Author

'# Publisher: Wrox (October 212005)
'# Language: English
'# ISBN-100764571982
'# ISBN-13978-0764571985

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

public class InvertImageUnsafe
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class



Public Class BitmapBytesRGB24
    ' Provide public access to the picture's byte data.
    Public ImageBytes() As Byte
    Public RowSizeBytes As Integer
    Public Const PixelDataSize As Integer = 24

    ' A reference to the Bitmap.
    Private m_Bitmap As Bitmap

    ' Save a reference to the bitmap.
    Public Sub New(ByVal bm As Bitmap)
        m_Bitmap = bm
    End Sub

    ' Bitmap data.
    Private m_BitmapData As BitmapData

    ' Lock the bitmap's data.
    Public Sub LockBitmap()
        ' Lock the bitmap data.
        Dim bounds As Rectangle = New Rectangle_
            00, m_Bitmap.Width, m_Bitmap.Height)
        m_BitmapData = m_Bitmap.LockBits(bounds, _
            Imaging.ImageLockMode.ReadWrite, _
            Imaging.PixelFormat.Format24bppRgb)
        RowSizeBytes = m_BitmapData.Stride

        ' Allocate room for the data.
        Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
        ReDim ImageBytes(total_size)

        ' Copy the data into the ImageBytes array.
        Marshal.Copy(m_BitmapData.Scan0, ImageBytes, _
            0, total_size)
    End Sub

    ' Copy the data back into the Bitmap
    ' and release resources.
    Public Sub UnlockBitmap()
        ' Copy the data back into the bitmap.
        Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
        Marshal.Copy(ImageBytes, 0, _
            m_BitmapData.Scan0, total_size)

        ' Unlock the bitmap.
        m_Bitmap.UnlockBits(m_BitmapData)

        ' Release resources.
        ImageBytes = Nothing
        m_BitmapData = Nothing
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgsHandles MyBase.Load
    End Sub

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles mnuFileOpen.Click
            Dim bm As New Bitmap("yourfile.jpg")
            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)

            ' Invert the image's pixels.
            Dim start_time As DateTime
            Dim stop_time As DateTime
            Dim elapsed_time As TimeSpan
            start_time = Now
            InvertImage(dest_bm)
            stop_time = Now

            ' Display the results.
            picDest.Image = dest_bm

            elapsed_time = stop_time.Subtract(start_time)
            MessageBox.Show(elapsed_time.TotalSeconds.ToString("0.0000"" seconds")
    End Sub

    ' Invert the pixel values in this Bitmap. 
    Private Sub InvertImage(ByVal bm As Bitmap)
        ' Make a BitmapBytesRGB24 object.
        Dim bm_bytes As New BitmapBytesRGB24(bm)

        ' Lock the bitmap.
        bm_bytes.LockBitmap()

        Dim pix As Integer
        For y As Integer = To bm.Height - 1
            pix = y * bm_bytes.RowSizeBytes
            For x As Integer = To bm.Width - 1
                ' Blue component.
                bm_bytes.ImageBytes(pix= CByte(255- bm_bytes.ImageBytes(pix)
                pix += 1
                ' Green component.
                bm_bytes.ImageBytes(pix= CByte(255- bm_bytes.ImageBytes(pix)
                pix += 1
                ' Red component.
                bm_bytes.ImageBytes(pix= CByte(255- bm_bytes.ImageBytes(pix)
                pix += 1
            Next x
        Next y

        ' Unlock the bitmap.
        bm_bytes.UnlockBitmap()
    End Sub
End Class

<Global.Microsoft.VisualBasic.CompilerServices.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.ComponentModel.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.MenuStrip1 = New System.Windows.Forms.MenuStrip
        Me.mnuFile = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuFileOpen = New System.Windows.Forms.ToolStripMenuItem
        Me.dlgOpenImage = New System.Windows.Forms.OpenFileDialog
        Me.SplitContainer1.Panel1.SuspendLayout()
        Me.SplitContainer1.Panel2.SuspendLayout()
        Me.SplitContainer1.SuspendLayout()
        CType(Me.picSource, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.picDest, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.MenuStrip1.SuspendLayout()
        Me.SuspendLayout()
        '
        'SplitContainer1
        '
        Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.SplitContainer1.Location = New System.Drawing.Point(024)
        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(522249)
        Me.SplitContainer1.SplitterDistance = 270
        Me.SplitContainer1.TabIndex = 3
        Me.SplitContainer1.Text = "SplitContainer1"
        '
        'picSource
        '
        Me.picSource.AutoSize = True
        Me.picSource.Location = New System.Drawing.Point(00)
        Me.picSource.Name = "picSource"
        Me.picSource.Size = New System.Drawing.Size(208184)
        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(00)
        Me.picDest.Name = "picDest"
        Me.picDest.Size = New System.Drawing.Size(184184)
        Me.picDest.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.picDest.TabIndex = 1
        Me.picDest.TabStop = False
        '
        'MenuStrip1
        '
        Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFile})
        Me.MenuStrip1.Location = New System.Drawing.Point(00)
        Me.MenuStrip1.Name = "MenuStrip1"
        Me.MenuStrip1.Size = New System.Drawing.Size(52224)
        Me.MenuStrip1.TabIndex = 2
        Me.MenuStrip1.Text = "MenuStrip1"
        '
        'mnuFile
        '
        Me.mnuFile.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFileOpen})
        Me.mnuFile.Name = "mnuFile"
        Me.mnuFile.Text = "&File"
        '
        'mnuFileOpen
        '
        Me.mnuFileOpen.Name = "mnuFileOpen"
        Me.mnuFileOpen.ShortcutKeys = CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.O), System.Windows.Forms.Keys)
        Me.mnuFileOpen.Text = "&Open"
        '
        'dlgOpenImage
        '
        Me.dlgOpenImage.Filter = "Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(522273)
        Me.Controls.Add(Me.SplitContainer1)
        Me.Controls.Add(Me.MenuStrip1)
        Me.Name = "Form1"
        Me.Text = "InvertImageUnsafe"
        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.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.picDest, System.ComponentModel.ISupportInitialize).EndInit()
        Me.MenuStrip1.ResumeLayout(False)
        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
    Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
    Friend WithEvents mnuFile As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuFileOpen As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents dlgOpenImage As System.Windows.Forms.OpenFileDialog

End Class
17.49.Image Operation
17.49.1.Rotate an Image
17.49.2.Mirror an Image
17.49.3.Move an image and paintMove an image and paint
17.49.4.Scale an imageScale an image
17.49.5.Scale image with different Graphics UnitsScale image with different Graphics Units
17.49.6.Skew imageSkew image
17.49.7.Make an image transparentMake an image transparent
17.49.8.Invert an ImageInvert an Image
17.49.9.Invert an Image unsafeInvert an Image unsafe
17.49.10.Zoom ImageZoom Image
17.49.11.Zoom and Skew an ImageZoom and Skew an Image
17.49.12.Skew an ImageSkew an Image
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.