'Visual Basic 2005 Programmer's Reference
'by Rod Stephens (Author)
'# Publisher: Wrox (October 21, 2005)
'# Language: English
'# ISBN-10: 0764571982
'# ISBN-13: 978-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( _
0, 0, 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.EventArgs) Handles MyBase.Load
End Sub
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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 = 0 To bm.Height - 1
pix = y * bm_bytes.RowSizeBytes
For x As Integer = 0 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
_
Partial Public Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
_
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.
_
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(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 = 3
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
'
'MenuStrip1
'
Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFile})
Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip1.Name = "MenuStrip1"
Me.MenuStrip1.Size = New System.Drawing.Size(522, 24)
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(522, 273)
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