Draw a rectangle in graphics on a picturebox and save to bitmap and save as tif - vb.net

I draw a rectangle on a picture and want to save the picture it does not keep me well. The rectangle looks smaller and in the wrong position.
Here is the code:
Public Class Form10
Dim G_RegistInfoPathSinglePage = "D:\0_0_1.TIF"
Dim SelectRect As Rectangle = New Rectangle()
Dim _pen As Pen = New Pen(Color.Purple, 4)
Dim _brush As SolidBrush = New SolidBrush(Color.Pink)
Dim _ControlPressed As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles picOriginal.MouseDown
SelectRect.Location = e.Location
SelectRect.Size = New Size(0, 0)
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles picOriginal.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(picOriginal.RectangleToScreen(SelectRect), picOriginal.BackColor, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(picOriginal.RectangleToScreen(SelectRect), picOriginal.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles picOriginal.MouseUp
If (e.Y < SelectRect.Y) Then
SelectRect.Location = If(SelectRect.Location.X > e.X,
New Point(e.X, e.Y), New Point(SelectRect.X, e.Y))
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
Else
If SelectRect.Location.X > SelectRect.Right Then
SelectRect.Location = New Point(e.X, SelectRect.Y)
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
End If
End If
If _ControlPressed Then
Dim _InflatedRect As Rectangle = New Rectangle(SelectRect.Location, SelectRect.Size)
_InflatedRect.Inflate(CInt(_pen.Width / 2), CInt(_pen.Width / 2))
picOriginal.Invalidate(_InflatedRect)
Else
picOriginal.Invalidate()
End If
End Sub
'paint rectangle
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles picOriginal.Paint
e.Graphics.DrawRectangle(_pen, SelectRect)
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
End Sub
'if Pressing ctrl + S switches to a function that saves the image
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If (e.KeyCode = Keys.S AndAlso e.Modifiers = Keys.Control) Then
SaveDrawRectengle()
MsgBox("Save successfully")
End If
End Sub
'save to image as tif
Public Sub SaveDrawRectengle()
Dim l_OriginalImage As New System.IO.FileStream(G_RegistInfoPathSinglePage, IO.FileMode.Open, IO.FileAccess.Read)
Dim OriginalImage = System.Drawing.Image.FromStream(l_OriginalImage)
Dim Bfr As Bitmap = New Bitmap(OriginalImage)
picOriginal.BackgroundImage = Bfr
Dim _image = picOriginal.BackgroundImage
Dim overlay As New Bitmap(_image.Width, _image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(overlay)
g.DrawRectangle(_pen, SelectRect)
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
g.FillRectangle(_brush, SelectRect)
g.Dispose()
picOriginal.Image = overlay
l_OriginalImage.Close()
Dim Tmpimg As Image = New Bitmap(300, 100)
Tmpimg = CType(Bfr, Image)
Dim Tmpg As Graphics = Graphics.FromImage(Tmpimg)
Tmpg.DrawImage(CType(overlay, Image), New Point(10, 10))
Tmpimg.Save("D:\852.tif")
End Sub
End Class

Related

How can I dispose the image from the following code?

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

Draw a rectangle with the mouse, but not when OpenfileDialog is busy

I have a Form1 and a PictureBox. I have also subscribed to the MouseDown, MouseMove and MouseUp events in order to be able to draw a rectangle on the PictureBox with the mouse. In itself, it works fine. Now I am using an OpenFileDialog. If I select the file in the window and click on 'OK', the dialog disappears, but – and this is my problem – a rectangle is drawn immediately because I moved the mouse. I don't want that to happen at the moment. I've already tried to use a Boolean variable to lock the MouseMove procedure, but unfortunately that didn't work.OpenFileDialog Here you can see the accidentally created rectangle
Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class Form1
Private Mausstartpunkt As Point ' Mouse start point
Private Mausendpunkt As Point ' Mouse end point
Public Shared Property Picture1 As Bitmap 'dann bleibt es im Anwendungs-Scope, wenn die Klasse verfällt.
Private Pfad_Bild As String = ""
Private Pfad_speichern As String = ""
Private It_is_allowed_to_draw As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If PictureBox1.Image IsNot Nothing AndAlso It_is_allowed_to_draw AndAlso Not String.IsNullOrEmpty(Pfad_Bild) Then
Dim rect As Rectangle = PointsToRectangle(Mausstartpunkt, Mausendpunkt)
AllesGrafische.Paint_the_Rectangle(e.Graphics, rect)
End If
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
Mausstartpunkt = System.Windows.Forms.Control.MousePosition
Mausstartpunkt = New Point(Mausstartpunkt.X - 8, Mausstartpunkt.Y - 31)
End If
If e.Button = MouseButtons.Right Then ' clears the rectangle
Mausstartpunkt = New Point(0, 0)
Mausendpunkt = New Point(0, 0)
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
Mausendpunkt = System.Windows.Forms.Control.MousePosition
Mausendpunkt = New Point(Mausendpunkt.X - 8, Mausendpunkt.Y - 31)
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button = MouseButtons.Left Then
Mausendpunkt = System.Windows.Forms.Control.MousePosition
Mausendpunkt = New Point(Mausendpunkt.X - 8, Mausendpunkt.Y - 31)
PictureBox1.Invalidate()
End If
End Sub
Public Shared Function PointsToRectangle(ByVal p1 As Point, ByVal p2 As Point) As Rectangle 'https://www.vb-paradise.de/index.php/Thread/20037-Rechteck-mit-Maus-zeichnen-Erledigt/?postID=124893#post124893
Dim r As New Rectangle With {
.Width = Math.Abs(p1.X - p2.X),
.Height = Math.Abs(p1.Y - p2.Y),
.X = Math.Min(p1.X, p2.X),
.Y = Math.Min(p1.Y, p2.Y)
}
Return r
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Nothing
Using OFD As New CommonOpenFileDialog
OFD.Title = "Datei zum Öffnen auswählen"
OFD.Filters.Add(New CommonFileDialogFilter("Bilder", ".jpg;.jpeg;.bmp"))
OFD.Filters.Add(New CommonFileDialogFilter("PNG", ".png"))
OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD.ShowDialog() = CommonFileDialogResult.Ok Then
Pfad_Bild = OFD.FileName
Pfad_speichern = Pfad_Bild.Substring(0, Pfad_Bild.LastIndexOf("\", StringComparison.Ordinal) + 1) ' for example "C:\Users\myName\Pictures\", or "C:\Users\myName\Desktop\"
Else
Return
End If
End Using
Picture1 = New Bitmap(Pfad_Bild)
PictureBox1.Image = Picture1
It_is_allowed_to_draw = True
End Sub
And this is the code for my graphics class
Imports System.Drawing.Drawing2D
Public NotInheritable Class AllesGrafische
Public Shared Sub Paint_the_Rectangle(ByVal g As Graphics, ByVal recta As Rectangle)
If g IsNot Nothing Then
g.SmoothingMode = SmoothingMode.AntiAlias
g.CompositingQuality = CompositingQuality.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBilinear
Using Pen_Hellblau As Pen = New Pen(Color.FromArgb(0, 200, 255), 1.0F)
g.DrawRectangle(Pen_Hellblau, recta)
End Using
End If
End Sub
End Class
Set a Boolean field to True on the MouseDown event and then only act on the MouseUp if that flag is set.

Draw Rectangle over PictureBox

The next code lets you draw Rectangles in the Form with mouse clics.
Why not, or how can be draw over a PictureBox?
Public Class Form1
Dim SelectRect As Rectangle = New Rectangle()
Dim ps As Point = New Point()
Dim pe As Point = New Point()
This catch the first click, starting point or corner of the rectangle
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
SelectRect.Width = 0
SelectRect.Height = 0
SelectRect.X = e.X
SelectRect.Y = e.Y
ps.X = e.X
ps.Y = e.Y
pe = ps
End Sub
This part determine the width and height of the rectangle:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
End If
End Sub
This part determine the last coordinate, the second corner of the rectangle:
Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Dim g As Graphics = Me.CreateGraphics()
Dim p As Pen = New Pen(Color.Blue, 2)
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
g.DrawRectangle(p, SelectRect)
g.Dispose()
End Sub
End Class
Your code uses a control (a Form in this case) mouse events to enable the drawing of rectangular shapes, with the help of guidelines, provided by Control.DrawReversibleFrame().
You just have to define the same events of a different, drawable, control - like a PictureBox - and repeat, more or less, the same procedure (after a cleanup).
As many have stated, here and before, use the Graphics object that
the Paint event kindly offers, so that your drawing will persist.
The Graphics object you get from Control.CreateGraphics() is not
persistent, and it can be erase/clipped when you don't want to.
Use it only if that is really what you have planned to do for the
reasons you know.
I've adden an event handler that checks if Control Key is pressed.
If Control is pressed, you add a rectangle, if not, only one rectangle is drawn.
I've also included, as an example, a line of code that fills the rectangle. I think it's interesting, because you have to control the size of the invalidated Region.
Comment out these lines of code to draw just the frame:
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
Dim SelectRect As Rectangle = New Rectangle()
Dim _pen As Pen = New Pen(Color.Green, 4)
Dim _brush As SolidBrush = New SolidBrush(Color.Orange)
Dim _ControlPressed As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
SelectRect.Location = e.Location
SelectRect.Size = New Size(0, 0)
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If (e.Y < SelectRect.Y) Then
SelectRect.Location = If(SelectRect.Location.X > e.X,
New Point(e.X, e.Y), New Point(SelectRect.X, e.Y))
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
Else
If SelectRect.Location.X > SelectRect.Right Then
SelectRect.Location = New Point(e.X, SelectRect.Y)
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
End If
End If
If _ControlPressed Then
Dim _InflatedRect As Rectangle = New Rectangle(SelectRect.Location, SelectRect.Size)
_InflatedRect.Inflate(CInt(_pen.Width / 2), CInt(_pen.Width / 2))
PictureBox1.Invalidate(_InflatedRect)
Else
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
'Draw the outer rectangle with the color of _pen
e.Graphics.DrawRectangle(_pen, SelectRect)
'Fill the rectangle with the color of _brush
'It's half Pen.Width smaller so it doesn't erase the contour
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
End Sub

How to draw a rectangle over an image and save it in VB.Net

I have used the following code to draw a rectangle on the image buI i an not able to save the image with the highlighted rectangle. Only the original image is saved not the image with the rectangle. Please help me with this.
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
IsMouseDown = True
SelectedObjPoint = New Point(e.X, e.Y)
End If
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If IsMouseDown = True Then
If e.X < SelectionBoxObj.X Then
SelectionBoxObj.X = e.X
SelectionBoxObj.Width = SelectedObjPoint.X - e.X
Else
SelectionBoxObj.X = SelectedObjPoint.X
SelectionBoxObj.Width = e.X - SelectedObjPoint.X
End If
If e.Y < SelectedObjPoint.Y Then
SelectionBoxObj.Y = e.Y
SelectionBoxObj.Height = SelectedObjPoint.Y - e.Y
Else
SelectionBoxObj.Y = SelectedObjPoint.Y
SelectionBoxObj.Height = e.Y - SelectedObjPoint.Y
End If
Me.Refresh()
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
IsMouseDown = False
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If SelectionBoxObj.Width > 0 And SelectionBoxObj.Height > 0 Then
Dim oGradientBrush As Brush = New Drawing.Drawing2D.LinearGradientBrush(SelectionBoxObj.RectangleF, SelectionBoxObj.FillColor, SelectionBoxObj.FillColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
e.Graphics.FillRectangle(oGradientBrush, SelectionBoxObj.RectangleF)
Dim TempPen = New Pen(SelectionBoxObj.BorderLineColor, SelectionBoxObj.BorderLineWidth)
TempPen.DashStyle = SelectionBoxObj.BorderLineType
e.Graphics.DrawRectangle(TempPen, SelectionBoxObj.RectangleF.X, SelectionBoxObj.RectangleF.Y, SelectionBoxObj.RectangleF.Width, SelectionBoxObj.RectangleF.Height)
End If
End Sub
Private Sub Highlight_Ok_Click(sender As Object, e As EventArgs) Handles Highlight_Ok.Click
Dim datestring As String = Date.Now.ToString("yyyyMMddmmss")
Dim path As String = "D:\capture screenshot temp"
Dim currentsavepath As String = String.Format("{0}\capture_{1}.png", path, datestring)
PictureBox1.Image.Save(currentsavepath, Imaging.ImageFormat.Bmp)
End Sub
The rectangle you are drawing in the PictureBox is a temporary drawing. It's showing the current image with the rectangle drawn on top of it. But when you are saving the image, the rectangle isn't a part of the image.
You can remedy your situation by moving your drawing routine into a procedure that will draw the result in a graphic object you pass to it:
Private Sub DrawHighlight(g As Graphics)
If SelectionBoxObj.Width > 0 And SelectionBoxObj.Height > 0 Then
Using br As New SolidBrush(SelectionBoxObj.FillColor)
g.FillRectangle(br, SelectionBoxObj.RectangleF)
End Using
Using p As New Pen(SelectionBoxObj.BorderLineColor, SelectionBoxObj.BorderLineWidth)
g.DrawRectangle(p, SelectionBoxObj.RectangleF)
End Using
End If
End Sub
Now you can update the screen easily:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
DrawHighlight(e.Graphics)
End Sub
and you can save the final results:
Using bmp As New Bitmap(PictureBox1.Image)
Using g As Graphics = Graphics.FromImage(bmp)
DrawHighlight(g)
End Using
bmp.Save(currentsavepath, Imaging.ImageFormat.Bmp)
End Using

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