How can I dispose the image from the following code? - vb.net

I am using VS 2019.
The presented code should display an animated GIF each time the 'btn_Display' is clicked.
If the image is disposed I've got an error at ImageAnimator.UpdateFrames() in PictureBox.Paint after the second click.
Public Class Form1
Dim animatedImage As Bitmap
Dim currentlyAnimating As Boolean = False
Dim framecounter As Integer
Dim framecount As Integer
Dim framdim As Imaging.FrameDimension
Dim gifFile As String
Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
PictureBox1.Invalidate()
End Sub
Sub AnimateImage()
If Not currentlyAnimating Then
ImageAnimator.Animate(animatedImage, New EventHandler(AddressOf Me.OnFrameChanged))
currentlyAnimating = True
End If
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If framecounter < framecount Then
AnimateImage()
ImageAnimator.UpdateFrames()
e.Graphics.DrawImage(Me.animatedImage, New Point(0, 0))
framecounter += 1
Application.DoEvents()
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
PictureBox1.Visible = False
gifFile = "E:\1.gif"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_Display.Click
startGif(gifFile)
End Sub
Sub startGif(ByVal FlName As String)
currentlyAnimating = False
animatedImage = New Bitmap(FlName)
framecounter = 0
PictureBox1.Size = animatedImage.Size
PictureBox1.Visible = True
framdim = New Imaging.FrameDimension(animatedImage.FrameDimensionsList(0))
framecount = animatedImage.GetFrameCount(framdim)
WaitFor_eoAnimation()
animatedImage.Dispose() ' <-----
PictureBox1.Visible = False
End Sub
Private Sub WaitFor_eoAnimation()
' Wait for end of animation
Do
Application.DoEvents()
Loop While framecounter < framecount 'currentlyAnimating
End Sub
End Class
Thanks in advance ... Eitan

Related

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.

Click and drag implementation on a rectangle VB.NET

So my problem is that I want to have multiple rectangles on a form at a time. However I also want these rectangles to be able to be clicked and dragged across the form.
This is my current code for clicking and dragging a rectangle that was drawn onto the form using the toolbox.
Public Class DragRectangle
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean
Dim HoldLeft As Integer
Dim HoldTop As Integer
Dim OffLeft As Integer
Dim OffTop As Integer
Private Sub obj1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseUp
Go = False
LeftSet = False
TopSet = False
End Sub
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseDown
Go = True
End Sub
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseMove
If Go = True Then
HoldLeft = (Control.MousePosition.X - Me.Left)
HoldTop = (Control.MousePosition.Y - Me.Top)
If TopSet = False Then
OffTop = HoldTop - sender.Top
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft - sender.Left
LeftSet = True
End If
sender.Left = HoldLeft - OffLeft
sender.Top = HoldTop - OffTop
End If
End Sub
End Class
This works fine for one rectangle, although this requires the rectangles to be pre-drawn onto the form using the toolbox.
What I would like is a rectangle gets drawn by clicking a button on the form, and the newly drawn rectangle can also be clicked and dragged into a new location.
Is this possible?
Thanks for any help
Working example:
Public Class Form1
Private Property Rectangles As New List(Of DrgRectangle)
Private Property curRect As DrgRectangle
Private _x As Integer
Private _y As Integer
Private Sub loadme() Handles Me.Load
'load the rectangle in list
Rectangles.Add(New DrgRectangle With {.Rect = New Rectangle(20, 20, 20, 20)})
End Sub
Private Sub FormMouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
_x = e.X
_y = e.Y
For Each rect In Rectangles
If rect.Rect.Contains(e.X, e.Y) Then
curRect = rect
Exit For
End If
Next
End Sub
Private Sub FormMouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If curRect IsNot Nothing Then
curRect.Rect = New Rectangle(New Point(curRect.Rect.Location.X + (e.X - _x), curRect.Rect.Location.Y + (e.Y - _y)), curRect.Rect.Size)
Me.Refresh()
End If
End If
_x = e.X
_y = e.Y
End Sub
Private Sub FormPaint(sender As Object, e As PaintEventArgs) Handles me.Paint
For Each rect In Rectangles
e.Graphics.DrawRectangle(Pens.Black, rect.Rect)
Next
End Sub
End Class
Public Class DrgRectangle
Public Rect As New Rectangle
'add more properties as needed
End Class

Is it possible to print or save the picturebox with other picture box within it

I am a vb.net beginner. I got a project that I need to do function that enabled user to add new picture(which is a new picturebox) and move it in a picture box. I have made these two happened but I don`t know how to make the picturebox(that allow new picturebox move inside) save as bitmap/jpg into database. Is that possible to do that.If yes, how?
Public Class Form1
Private btn As Button ' this is a reference object
Private pic As PictureBox
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodepic_MouseDown(ByVal senderPic As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Left Then
drag = True
pic = CType(senderPic, PictureBox)
ptX = e.X : ptY = e.Y
End If
If pic.Focused Then
clearButton.Enabled = True
End If
End Sub
Private Sub nodepic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
If pic.Location.X >= 1 AndAlso pic.Location.Y >= 1 AndAlso
(pic.Location.X + pic.Width) <= panelPictureBox.Width - 5 AndAlso
(pic.Location.Y + pic.Height) <= panelPictureBox.Height - 5 Then
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
Me.Refresh()
Else
drag = False
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
End If
End If
End Sub
Private Sub node_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub deleteButton(senderPic As Object, e As EventArgs)
Dim delete As DialogResult
delete = MessageBox.Show("Are you sure to delete this icon?", "Delete Icon", MessageBoxButtons.YesNo)
If delete = Windows.Forms.DialogResult.Yes Then
senderPic.Dispose()
locationLabel.Text = String.Empty
End If
End Sub
Private Sub locationButton(senderPic As Object, e As System.Windows.Forms.MouseEventArgs)
pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
locationLabel.Text = pic.Location.ToString()
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim picBox As New PictureBox
If e.Node.Name.Equals("red") Then
picBox.Image = ImageList1.Images(0)
End If
If e.Node.Name.Equals("orange") Then
picBox.Image = ImageList1.Images(1)
End If
picBox.Location = New Point(10, 10)
panelPictureBox.Controls.Add(picBox)
action(picBox)
End Sub
Private Sub action(sender As PictureBox)
AddHandler sender.MouseDown, AddressOf nodepic_MouseDown
AddHandler sender.MouseMove, AddressOf nodepic_MouseMove
AddHandler sender.MouseUp, AddressOf node_MouseUp
AddHandler sender.MouseDoubleClick, AddressOf deleteButton
AddHandler sender.MouseClick, AddressOf locationButton
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
panelPictureBox.Enabled = True
panelPictureBox.BackColor = Color.White
End Sub
Private Sub clearButton_Click(senderPic As Object, e As EventArgs) Handles clearButton.Click
pic.Dispose()
End Sub**strong text**
End Class
You can save the PictureBox, along with its "child" PictureBoxes, to a Bitmap using code like this:
Dim bmp As New Bitmap(panelPictureBox.Width, panelPictureBox.Height)
panelPictureBox.DrawToBitmap(bmp, panelPictureBox.ClientRectangle)
Next you save it to memory by writing it out to a MemoryStream. Note that you can specify the format in the second parameter:
Dim ms As New System.IO.MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Finally, you can obtain a byte array from the resulting MemoryStream:
Dim bytes() As Byte = ms.ToArray
' ... save "bytes" to your database ...
if you need to print the image inside the picturebox then you should insert printdialog ,printdocument in side the design of the form then copy the code below
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
PrintDialog1 = New PrintDialog
PrintDialog1.Document = PrintDocument1 'pbxLogo.Image
Dim r As DialogResult = PrintDialog1.ShowDialog
If r = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
End Sub

How to remove a line which is drawn on a form

I'm trying to draw a triangle like this:
Dim triangle As Graphics
Dim pen1 As New Pen(Color.LimeGreen, 2)
Dim lside As Integer
Dim wside As Integer
Dim dside As Integer
triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)
lside, wside and dside stand for length side, width side and diagonal side.
I've got 4 textboxes, for the length, width, diagonal side and one for the angle.
The purpose is to fill in 2 of the values, and then a rectangular triangle gets drawn following Pythagoras' theorem. I want to draw a line for Angle as well later on. But I first want to get this to work.
But every time I click the button to draw a new triangle, the previous one should get deleted. And that's the problem.
I've tried multiple methods, like triangle.Dispose triangle.Restore triangle.Clear and more. None of them work.
Why am I not drawing them in a picturebox you might ask. Well, when I drew a line in a picturebox, the picturebox sort of went in front of the line, making the line invisible. And I didn't know how to fix that.
Try using Me.Invalidate(), it basically clears, then draws the shape in the area you're painting on. Reference.
Private Sub ClearCanvas_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Invalidate()
End Sub
Priavte DrawTriangle_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim triangle As Graphics
Dim pen1 As New Pen(Color.LimeGreen, 2)
Dim lside As Integer
Dim wside As Integer
Dim dside As Integer
triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)
End Sub
‘Draw select delete multiple lines on Picturebox
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim drawrec, undo_delete As Boolean
Dim index_arrary_line_tobe_deleted(10000) As Integer
Dim ptA, ptB As Point ' starting and ending point
Dim down As Boolean
Dim k, Last_index_line_tobe_selected As Integer
Private temp As line
Dim List_of_line_tobe_deleted As New List(Of line)
Dim List_of_line_to_Undo As New List(Of line)
Private m_Lines As New List(Of line)
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub B2_index_arrary_line_tobe_deletedete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2_Delete.Click
Try
m_Lines.RemoveAll(AddressOf List_of_line_tobe_deleted.Contains)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub B3_Undodelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3_Undodelete.Click
undo_delete = True
Try
m_Lines.AddRange(List_of_line_tobe_deleted)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub B1_Select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1_Select.Click
drawrec = True
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
If m_tracking = True And drawrec = True Then
k = -1
For i As Integer = 0 To m_Lines.Count - 1
If m_Lines(i).ContainsCompletely(r) = True Then
k = k + 1
index_arrary_line_tobe_deleted(i) = k
Debug.Print("Index of NOT selected lines " + i.ToString + "Index of selected lines " + Last_index_line_tobe_selected.ToString) 'to compare idex of two lists !!!!
index_arrary_line_tobe_deleted(k) = i
List_of_line_tobe_deleted.Add(m_Lines(i))
End If
Next
Last_index_line_tobe_selected = k 'so far no use, just to know
e.Graphics.DrawRectangle(Pens.Cyan, r)
End If
If undo_delete = False Then
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Draw(e.Graphics, r)
Debug.Print("Index of remaining lines " + i.ToString)
Next
End If
If undo_delete = True Then
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Re_Draw(e.Graphics)
Debug.Print("Index of remaining lines " + i.ToString)
Next
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
drawrec = False
down = False
undo_delete = False
For i As Integer = 0 To index_arrary_line_tobe_deleted.Length - 1
index_arrary_line_tobe_deleted(0) = -1
Next i
k = -1
Last_index_line_tobe_selected = -1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
down = True
If down = True And drawrec = False Then
ptA = e.Location
temp = New line
temp.StartPoint = e.Location
End If
If e.Button = MouseButtons.Left Then
ResetSelected(Me.m_Lines)
m_Pt = e.Location
End If
End Sub
Private Sub ResetSelected(ByVal m_Lines As List(Of line))
For i As Integer = 0 To m_Lines.Count - 1
m_Lines(i).Selected = False
Next
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
End If
If e.Button = MouseButtons.Left Then
If down = True And drawrec = False Then
ptB = e.Location
temp.EndPoint = e.Location
End If
m_Pt2 = e.Location
m_tracking = True
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
down = False
If drawrec = False Then
temp.EndPoint = e.Location
m_Lines.Add(temp)
temp = Nothing
Me.PictureBox1.Invalidate()
End If
m_tracking = False
Me.PictureBox1.Invalidate()
End Sub
End Class
Public Class line
Public StartPoint As Point
Public EndPoint As Point
Private m_selected As Boolean
Public Property Selected() As Boolean
Get
Return m_selected
End Get
Set(ByVal value As Boolean)
m_selected = value
End Set
End Property
Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
Dim myPen1 As New Pen(Color.Red, 1)
g.SmoothingMode = SmoothingMode.AntiAlias
If Me.ContainsCompletely(r) OrElse Me.Selected Then
Me.Selected = True
g.DrawLine(myPen1, Me.StartPoint, Me.EndPoint)
Else
Dim myPen2 As New Pen(Color.Blue, 1)
g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
End If
End Sub
Public Sub Re_Draw(ByVal g As Graphics)
g.SmoothingMode = SmoothingMode.AntiAlias
Dim myPen2 As New Pen(Color.Blue, 1)
g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
End Sub
Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean
If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
Return True
End If
Return False
End Function
End Class
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim drawrec As Boolean
Dim del As Integer
Dim ptA, ptB As Point ' starting and ending point
Dim down As Boolean
Private temp As line
Private m_Lines As New List(Of line)
Private m_rnd As New Random
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Delete.Click
Try
m_Lines.RemoveAt(del)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
drawrec = False
down = False
del = -1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
down = True
If down = True And drawrec = False Then
ptA = e.Location
temp = New line
temp.StartPoint = e.Location
End If
If e.Button = MouseButtons.Left Then
ResetSelected(Me.m_Lines)
m_Pt = e.Location
End If
End Sub
Private Sub ResetSelected(ByVal m_Lines As List(Of line))
For i As Integer = 0 To m_Lines.Count - 1
m_Lines(i).Selected = False
Next
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
End If
If e.Button = MouseButtons.Left Then
If down = True And drawrec = False Then
ptB = e.Location
temp.EndPoint = e.Location
End If
m_Pt2 = e.Location
m_tracking = True
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
down = False
If drawrec = False Then
temp.EndPoint = e.Location
m_Lines.Add(temp)
temp = Nothing
Me.PictureBox1.Invalidate()
End If
m_tracking = False
Me.PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
If m_tracking And drawrec = True Then
For i As Integer = 0 To m_Lines.Count - 1
If m_Lines(i).ContainsCompletely(r) = True Then
Debug.Print("KKKKKKKKKKKKKKK " + i.ToString)
del = i
End If
Next
e.Graphics.DrawRectangle(Pens.Cyan, r)
End If
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Draw(e.Graphics, r)
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Select.Click
drawrec = True
End Sub
End Class
Public Class line
Public StartPoint As Point
Public EndPoint As Point
Public Filled As Boolean
Public ShapeColor As Color
Public PenWidth As Integer
Private m_selected As Boolean
Public Property Selected() As Boolean
Get
Return m_selected
End Get
Set(ByVal value As Boolean)
m_selected = value
End Set
End Property
Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
g.SmoothingMode = SmoothingMode.AntiAlias
If Me.ContainsCompletely(r) OrElse Me.Selected Then
Me.Selected = True
g.DrawLine(Pens.Red, Me.StartPoint, Me.EndPoint)
Else
g.DrawLine(Pens.Blue, Me.StartPoint, Me.EndPoint)
End If
End Sub
Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean
If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
Return True
End If
Return False
End Function
End Class

Form with custom title bar & formborderstyle = None remains draggable when maximized

I used this code to maximize and restore my custom form. But when the form is maximized, it still remains draggable, I use a timer to drag the form.
Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click, lblTitle.MouseDoubleClick
Dim maximizeHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
Dim maximizeWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Dim maximizeLocation As Point = New Point(0, 0)
Dim fullscreen As Boolean = False
If Me.Height = maximizeHeight Or Me.Width = maximizeWidth Or Me.Location = maximizeLocation Then
fullscreen = True
Else
fullscreen = False
End If
If fullscreen = True Then
Me.Size = New Size(1000, 500)
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
ElseIf fullscreen = False Then
Me.Location = New Point(0, 0)
Me.Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End If
End Sub
Private Sub pnlBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseDown
MoveTmr.Start()
refpositions()
End Sub
Private Sub MoveTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MoveTmr.Tick
Me.Location = oloc - ocur + System.Windows.Forms.Cursor.Position
refpositions()
End Sub
Private Sub pnlBar_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseUp
MoveTmr.Stop()
refpositions()
End Sub
Private Sub RszTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles RszTmr.Tick
Me.Size = appSize - curPos + Cursor.Position
refpositions()
End Sub
make:
Dim fullscreen As Boolean = False
a class variable.
Then edit this code to fit your variables:
Private Sub Mover_Tick(sender As System.Object, e As System.EventArgs) Handles Mover.Tick
If fullscreen = false Then
Dim pt As New Point((Me.Location.X + (MousePosition.X - mPosX)), (Me.Location.Y + (MousePosition.Y - mPosY)))
Me.Location = pt
mPosX = MousePosition.X
mPosY = MousePosition.Y
End If
End Sub
EDIT:
Also implement this:
Private Sub Title_StartDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseDown
mPosX = MousePosition.X
mPosY = MousePosition.Y
If e.Button = Windows.Forms.MouseButtons.Left Then
Mover.Start()
End If
End Sub
Private Sub Title_StopDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseUp
Mover.Stop()
End Sub
also you may want to make it much simpler by saying
Me.WindowState = FormWindowState.Maximized
I use the Mousedown, Mouseup and Mousemove events to move my forms.
Public Class Form1
Private Is_Dragged As Boolean = False
Private M_DownX As Integer
Private M_DownY As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
Is_Dragged = True
M_DownX = e.X
M_DownY = e.Y
End If
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Left Then
Is_Dragged = False
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If Is_Dragged Then
Dim tmp_pnt As Point = New Point()
tmp_pnt.X = Me.Location.X + (e.X - M_DownX)
tmp_pnt.Y = Me.Location.Y + (e.Y - M_DownY)
Me.Location = tmp_pnt
tmp_pnt = Nothing
End If
End Sub
End Class
and to maximize my form.
Private Sub Btn_Main_Max_Click(sender As Object, e As EventArgs) Handles Btn_Main_Max.Click
Static IsAlreadyResized As Boolean
If Not IsAlreadyResized Then
Me.WindowState = FormWindowState.Maximized
IsAlreadyResized = True
Exit Sub
End If
If IsAlreadyResized Then
Me.WindowState = FormWindowState.Normal
IsAlreadyResized = False
End If
End Sub
You can create a panel in code or visually and place dock.top then you can put a label on the side or in the middle and a picturebox on the left side of the window create the buttons to close minimize and maximize or others and add the friend code above to move the form by the titl bar as in windows