How can I able to see the image in my picturebox? - vb.net-2010

I have used the following code to draw what I want but when I used this code I cannot see the image I set on my picturebox. What should I do to draw on the picture box with an image? Please help me with this.
Public Class Form1
Dim draw As Boolean
Dim DrawColor As Color = Color.Black
Dim DrawSize As Integer = 6
Dim bmp As Bitmap
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
cbxSize.SelectedIndex = 2
bmp = New Bitmap(pbDraw.Width, pbDraw.Height)
pbDraw.Image = bmp
Dim down = False
End Sub
Private Sub PaintBrush(X As Integer, Y As Integer)
Using g As Graphics = Graphics.FromImage(pbDraw.Image)
g.FillRectangle(New SolidBrush(DrawColor), New Rectangle(X, Y, DrawSize, DrawSize))
End Using
pbDraw.Refresh()
End Sub
Drawing event
Private Sub pbtest_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbDraw.MouseDown
draw = True
PaintBrush(e.X, e.Y)
End Sub
Private Sub pbtest_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbDraw.MouseMove
If draw = True Then
PaintBrush(e.X, e.Y)
End If
End Sub
Private Sub pbtest_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbDraw.MouseUp
draw = False
End Sub
Please help me

Related

Remove Or Delete the Rectangle drawn on the PictureBox

I am currently solving a bug that will remove the created rectangle on the PictureBox. The problem is that when I click an Item on the PictureBox and Resize the windows form, the rectangle does not move on with the item selected. This is the code creating the rectangle:
Private Sub paintRectangle(pictBox As System.Windows.Forms.PictureBox, pic As Image)
If pic Is Nothing Then Exit Sub
pictBox.Image = pic
If m_rect_x = -1 And m_rect_y = -1 Then
Return
End If
Dim graphic As System.Drawing.Graphics
Dim redselpen As System.Drawing.Pen
Dim yNegative As Integer = 3
redselpen = New System.Drawing.Pen(Color.Blue)
redselpen.DashStyle = Drawing2D.DashStyle.DashDot
If pictBox.Image IsNot Nothing Then
graphic = System.Drawing.Graphics.FromImage(pictBox.Image)
graphic.DrawRectangle(redselpen, m_rect_x, m_rect_y - yNegative, SystemConfig.iRectWidth, SystemConfig.iRectHeight + 2)
pictBox.Image = pictBox.Image
End If
End Sub
After Resizing the Form, I want to remove the create a rectangle on the PictureBox.
I tried this solution but the Rectangle is still in the PictureBox.
How to remove all the drawn rectangles on the picture box? (Not on the image)
But it does not work, the rectangle is still in the picturebox.
Here's a simple example showing the Paint() event of a PictureBox being used to draw a rectangle that can be moved and turned on/off:
Public Class Form1
Private yNegative As Integer = 3
Private pt As New Nullable(Of Point)
Private drawRectangle As Boolean = False
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If drawRectangle AndAlso pt.HasValue Then
Using redselpen As New System.Drawing.Pen(Color.Blue)
redselpen.DashStyle = Drawing2D.DashStyle.DashDot
e.Graphics.DrawRectangle(redselpen, pt.Value.X, pt.Value.Y - yNegative, SystemConfig.iRectWidth, SystemConfig.iRectHeight + 2)
End Using
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
pt = New Point(25, 25)
drawRectangle = True
PictureBox1.Invalidate()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
drawRectangle = Not drawRectangle ' toggle the rectangle on/off
PictureBox1.Invalidate()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
pt = New Point(150, 25)
drawRectangle = True
PictureBox1.Invalidate()
End Sub
End Class

How to save an image from Picturebox that load from camera?

I create a simple program that getting image from camera and display it in picturebox using a Aforge library. However, when I try to save the image from picture box. It prompt an error like this " 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dll. A generic error occurred in GDI+. "
I did some research but not found any helpful solution.
Below are my following code use to save the image:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox1.Image.Save("C:\a.jpeg", ImageFormat.Jpeg)
End Sub
Updated Code :
Imports AForge
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports AForge.Imaging.Filters
Imports AForge.Imaging
Imports System.Drawing.Imaging
Public Class Form1
Dim Filter As FilterInfoCollection
Dim Camera As VideoCaptureDevice
Dim MINR As Integer = 0
Dim MING As Integer = 0
Dim MINB As Integer = 0
Dim MAXR As Integer = 255
Dim MAXG As Integer = 255
Dim MAXB As Integer = 255
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Filter = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If Filter.Count > 0 Then
For Each ITEM In Filter
ComboBox1.Items.Add(ITEM.Name.ToString())
Next
CheckForIllegalCrossThreadCalls = False
Me.Location = New System.Drawing.Point(Me.Location.X, 0)
Else
MsgBox("NO Camera Found")
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Camera = New VideoCaptureDevice(Filter(ComboBox1.SelectedIndex).MonikerString)
AddHandler Camera.NewFrame, New NewFrameEventHandler(AddressOf Video_NewFrame)
Camera.Start()
ComboBox1.Visible = False
End Sub
Private Sub Video_NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)
Dim ORIGINAL As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
Dim FILTER As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
Dim CFILTER As New ColorFiltering
CFILTER.Red = New IntRange(MINR, MAXR)
CFILTER.Green = New IntRange(MING, MAXG)
CFILTER.Blue = New IntRange(MINB, MAXB)
CFILTER.ApplyInPlace(FILTER)
Dim GRAY As Grayscale = Grayscale.CommonAlgorithms.BT709
Dim IMAGING As Bitmap = GRAY.Apply(FILTER)
Dim BLOBS As New BlobCounter()
BLOBS.MinHeight = 10
BLOBS.MinWidth = 10
BLOBS.ObjectsOrder = ObjectsOrder.Size
BLOBS.ProcessImage(IMAGING)
Dim Rectangle As Rectangle() = BLOBS.GetObjectsRectangles()
If Rectangle.Count > 0 Then
Dim Rectangle2 As Rectangle = Rectangle(0)
Dim STYLE As Graphics = Graphics.FromImage(ORIGINAL)
Dim CLR As New Pen(Color.Lime, 5)
STYLE.DrawRectangle(CLR, Rectangle2)
STYLE.Dispose()
End If
PictureBoxDefault.Image = ORIGINAL '
PictureBoxFilter.Image = FILTER
End Sub
Private Sub TrackBarMINR_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMINR.Scroll
MINR = TrackBarMINR.Value
LabelMINR.Text = "MINR: " & MINR
End Sub
Private Sub TrackBarMING_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMING.Scroll
MING = TrackBarMING.Value
LabelMING.Text = "MING: " & MING
End Sub
Private Sub TrackBarMINB_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMINB.Scroll
MINB = TrackBarMINB.Value
LabelMINB.Text = "MINB: " & MINB
End Sub
Private Sub TrackBarMAXR_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMAXR.Scroll
MAXR = TrackBarMAXR.Value
LabelMAXR.Text = "MAXR: " & MAXR
End Sub
Private Sub TrackBarMAXG_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMAXG.Scroll
MAXG = TrackBarMAXG.Value
LabelMAXG.Text = "MAXG: " & MAXG
End Sub
Private Sub TrackBarMAXB_Scroll(sender As System.Object, e As System.EventArgs) Handles TrackBarMAXB.Scroll
MAXB = TrackBarMAXB.Value
LabelMAXB.Text = "MAXB: " & MAXB
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Camera.SignalToStop()
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnSave.Click
PictureBox1.Image.Save("D:\a.png", ImageFormat.Png)
End Sub
End Class
I had the same issue.
The code below is now working for me.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim img As Bitmap = PictureBox1.Image.Clone
img.Save("Z:\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
Just change "Z:\test.bmp" to your directory, haven't tried any other image formats but this is the only way I have gotten it to work so far.

MouseEventArgs from a subroutine

I have a chart in Visual Studio 2013 and I can draw a line on it using the MouseventArgs handler. No worries.
But.... how can you have a separate subroutine that then uses the mouse events to draw a line. What I am need is if I hit a button, draw a line, if I did not hit the button use the regular mouseventargs.
So the button needs to start this:
enter code herePrivate Sub Chart1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chD.MouseDown
FirstPoint = New Point(e.X, e.Y)
TempPoint = New Point(e.X, e.Y)
End Sub
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chD.MouseMove
If Not FirstPoint = Nothing Then
Dim g As Graphics = chD.CreateGraphics
Dim ErasePen As Pen = New Pen(Me.BackColor)
g.DrawLine(ErasePen, FirstPoint, TempPoint)
TempPoint = New Point(e.X, e.Y)
Me.Refresh()
End If
End Sub
Private Sub Chart1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chD.MouseUp
Dim Line As New LineEndPoints With {.StartPoint = FirstPoint, .endPont = New Point(e.X, e.Y)}
LinesList.Add(Line)
FirstPoint = Nothing
Me.Refresh()
End Sub
Private Sub Chart1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles chD.Paint
For Each line As LineEndPoints In LinesList
e.Graphics.DrawLine(Pens.Yellow, line.StartPoint, line.endPont)
Next
If Not FirstPoint = Nothing Then
e.Graphics.DrawLine(Pens.Yellow, FirstPoint, TempPoint)
End If
End Sub`
`
Does anyone have any idea how I can achieve this?

Can't draw rectangle around textbox in Groupbox, using Visual Basic 2010.-

I have two textbox controls in a vb form.- I can draw rectangles about the two textbox if they aren't in a groupbox.- Only when the textbox got the focus the rectangle is drawed.- The code works, but somehow, when i put the textbox in a groupbox the rectangles aren't drawed.-
the rectangle only is drawed when textbox is focused
This is the code that i have been using.-
Public Class Form1
Dim curControl As TextBox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
If Not curControl Is Nothing Then
If curControl.Name = "TextBox1" Then
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Lime, 2.0)
g.DrawRectangle(pen, New Rectangle(TextBox1.Location, TextBox1.Size))
pen.Dispose()
End If
End If
If Not curControl Is Nothing Then
If curControl.Name = "TextBox2" Then
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Red, 2.0)
g.DrawRectangle(pen, New Rectangle(TextBox2.Location, TextBox2.Size))
pen.Dispose()
End If
End If
End Sub
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter
curControl = DirectCast(sender, TextBox)
Me.Invalidate()
End Sub
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Leave, TextBox2.Leave
curControl = Nothing
Me.Invalidate()
End Sub

VB.NET paint application

I'm facing two problems in my application:
The Undo Function
The Drawing Part
When i draw on the picturebox , it draws very well, when - let's say I want to undo an action, it undo's it but when I click back on the picturebox it reacts like a redo function ,all the drawings appear back on the image.
the second problem is : i want to be able to edit a picture so i load a image by clicking on a listview item but due to something i'm missing the image it is not show but instead it shows a white background in which i am able to draw.
bellow is the code i am using
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Public drawgraph, g As Graphics
Private redoBuffer As New Stack(Of Image)()
Private undoBuffer As New Stack(Of Image)()
Dim color As Color
Dim UndoStack As New Stack(Of Bitmap)()
Dim xStart, yStart, xEnd, yEnd As Integer
Public Drawbitmap As Bitmap
Dim Drawgraphics As Graphics
Dim myPen As New Pen(color.Black, 4)
Dim myColor As Color = color.Black
Dim myPenWidth As Integer
Dim myBGColor As Color = color.White
Dim Drawing As Boolean
Private Sub drawMyline()
PictureBox4.Image = Drawbitmap
Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Drawgraphics.DrawLine(myPen, xStart, yStart, xEnd, yEnd)
End Sub
Private Sub PushUndo(ByVal b As Bitmap)
UndoStack.Push(b)
End Sub
Private Function PopUndo() As Bitmap
If UndoStack.Count = 0 Then
Return Nothing
Exit Function
End If
If UndoStack.Count > 0 Then
Return UndoStack.Pop
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Drawbitmap = New Bitmap(PictureBox4.Width, PictureBox4.Height)
Drawgraphics = Graphics.FromImage(Drawbitmap)
PictureBox4.Image = Drawbitmap
Drawgraphics.Clear(color.White)
myPenWidth = NumericUpDown1.Value
xStart = -1
yStart = -1
Drawing = False
End Sub
Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
Dim bmp As Bitmap
bmp = PopUndo()
If bmp IsNot Nothing Then
PictureBox4.Image = bmp
End If
End Sub
Private Sub PictureBox4_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseDown
Drawing = True
PushUndo(PictureBox4.Image.Clone)
End Sub
Private Sub PictureBox4_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseUp
Drawing = False
End Sub
Private Sub PictureBox4_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseMove
If Drawing Then
xStart = e.X
yStart = e.Y
drawMyline()
End If
xEnd = e.X
yEnd = e.Y
End Sub
End Class
I tried making changes but i can't load the image i want into the picturebox4 and draw on it , it always loads a white background as for the undo function it works until a click again on picturebox4 and all the undone drawings appear back. Can someone help me fix this 2 problems that I have?