Cropping a picturebox (That is capturing from webcam)
Ok, so I have a working solution, which could use some love, but really I kinda want to dump it and find something better. I want something where after I crop it, I can manipulate the cropped box, something with handles on it, in-case I don't get the crop exactly how I want. It needs BIG handles because this whole thing is going to be on a touch screen =-0 So what I am looking for is some love for this code, in case I don't find a fitting alternative solution. Or something with handles on it, maybe better coded for 2010 :D
Code:
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class frmMain
Inherits System.Windows.Forms.Form
'bitmap to contain the cropped image
Dim cropBitmap As Bitmap
'the position and size to crop the image file
Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight As Integer
'create a pen object
Public cropPen As Pen
'select a default crop line size
Public cropPenSize As Integer = 1 '2
'will contain the dashStyle of the pen
Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid
'set a default crop line color
Public cropPenColor As Color = Color.Aquamarine
'this is for changing the mouseicon.
'being used for setting cropping points.
'makes it more user friendly
Public c As Cursors
Dim Touchless1 As New TouchlessLib.TouchlessMgr
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartCapture()
End Sub
Public Sub StartCapture()
If Touchless1.Cameras.Count = 0 Then MessageBox.Show("This application needs a webcam") : Return
Touchless1.CurrentCamera = Touchless1.Cameras.First
AddHandler Touchless1.CurrentCamera.OnImageCaptured, AddressOf CaptureInvoke
End Sub
Private Sub CaptureInvoke()
picCapture.Image = Touchless1.CurrentCamera.GetCurrentImage()
DrawBox()
End Sub
Private Sub DrawBox()
Try 'This is the Crop and Draw Box on changing picturebox
cropX = Convert.ToInt32(txtX.Text)
cropY = Convert.ToInt32(txtY.Text)
cropWidth = Convert.ToInt32(txtWidth.Text)
cropHeight = Convert.ToInt32(txtHeight.Text)
Dim g As Graphics = picCapture.CreateGraphics
cropPen = New Pen(cropPenColor, cropPenSize)
cropPen.DashStyle = cropDashStyle
g.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnCrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrop.Click
Try
If cropWidth < 1 Then
'if there are not proper cropping cordinates, then throw a message and exit code
MessageBox.Show("You need to first select what portion of the image to crop.", " No cropping Cordinates!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
'a rectangle to set the location and size from the source image
Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)
'You will need to remember that you will want to decide the width and height of the
'bitmap according to what the sizemode of the picturebox that contains the image is.
'If the sizemode = Stretch, then you will want the width/height of the control itself.
'IF the sizemode = Normal, then you will want the width/height of the IMAGE. So on,
'and so forth depending on the picturebox controls sizemode. If you do not do this,
'then you will end up with a completely different cropping image then what you gave
'cordinates for.
Dim bit As Bitmap = New Bitmap(picCapture.Image, picCapture.Width, picCapture.Height)
'create a new bitmap with the width/height values that were specified in the textboxes.
cropBitmap = New Bitmap(cropWidth, cropHeight)
'a new Graphics object that will draw on the cropBitmap
Dim g As Graphics = Graphics.FromImage(cropBitmap)
'draw the portion of the image that you supplied cropping values for.
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
'go ahead and make the new portion of the image a PART of the picturebox instead of
'just drawing on it. Because if you draw the image through the Graphic class, then
'you will lose the image whenever the application minimized or got covered up since
'the image is not persisted on the control.
picCrop.Image = cropBitmap
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
txtX.Text = "0"
txtY.Text = "0"
txtWidth.Text = "0"
txtHeight.Text = "0"
End Sub
Private Sub picCapture_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCapture.MouseDown
Try
If e.Button = MouseButtons.Left Then
cropX = e.X
cropY = e.Y
cropPen = New Pen(cropPenColor, cropPenSize)
cropPen.DashStyle = cropDashStyle
'change the mouse pointer to the cross cursor which kinda helps when
'sizing for the crop
Cursor = c.Cross
End If
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub picCapture_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCapture.MouseMove
Try
If picCapture.Image Is Nothing Then Exit Sub
If e.Button = MouseButtons.Left Then
'clear the previous drawn crop lines
picCapture.Refresh()
'need to take into count where the mouse started at
'and calculate the new position
cropWidth = e.X - cropX
cropHeight = e.Y - cropY
'draw the outline for the cropping
picCapture.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
'update the coordinates in the textboxes
txtX.Text = cropX
txtY.Text = cropY
txtWidth.Text = cropWidth
txtHeight.Text = cropHeight
End If
'release as much memory as possible
GC.Collect()
Catch exc As Exception
'this is a error when the cropping parameters are less than 1
If Err.Number = 5 Then Exit Sub
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub picCapture_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCapture.MouseUp
Try
'change the mouse pointer back to the default cursor
Cursor = c.Default
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
The crop code originates from this: http://www.planet-source-code.com/vb...3437&lngWId=10