VB.NET Winforms: Overlay two images and move overlay with timer - vb.net

I would like to overlay 2 images and then move the top layer using a timer.
this is the code i'm trying to implement from this anwser: here
Imports System.Drawing
Dim OverlayImage As New Bitmap("Some Path", True)
Dim BackImage As New Bitmap("Some Path", True)
g As Graphics = Graphics.FromImage(BackImage)
g.DrawImage(OverlayImage, 0, 0)
pictureBox1.Image = BackImage
If you want to have the timer move the overlayed image, then first, make a variable
Dim posX As Integer = 0
then use
g.DrawImage(OverlayImage, posX, 0)
Now when your timer ticks, increment posX by 10
This is what i got but the overlay isn't moving.
What am i doing wrong? Can anybody help me?
Imports System.Drawing
Public Class Form1
Dim OverlayImage As New Bitmap("D:/white.png", True)
Dim BackImage As New Bitmap("D:/bg.png", True)
Dim g As Graphics = Graphics.FromImage(BackImage)
Dim posX As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Picturebox1.Image = BackImage
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
g.DrawImage(OverlayImage, posX, 0)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
posX = +10
End Sub
End Class

Related

Unwanted cursor flicker when cursor moves inside regions

The following code example paints three rectangles and changes the cursor type to hand/default if the mouse is inside/outside the rectangular regions respectively. The program executes as intended with one exception: when located inside a region, the mouse flickers while being moved.
Having investigated, it seems the issue may relate to a contradiction between my code and an underlying control, with respect to which mouse type to display, resulting in flicker. Whether correct or not, unfortunately I've been unable to resolve the issue. Please can you help?
Imports System.Drawing.Drawing2D
Public Class Form1
Private myRectangles() As Rectangle
Private myRegions(2) As Region
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Write an array of rectangles.
myRectangles = {New Rectangle(100, 100, 50, 50), New Rectangle(200, 200, 50, 50), New Rectangle(300, 300, 50, 50)}
' Write an array of rectangle regions.
For i As Integer = 0 To myRegions.length - 1
Dim myPath As New GraphicsPath
myPath.AddRectangle(myRectangles(i))
Dim myRegion As New Region(myPath)
myRegions(i) = myRegion
Next
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
' Iterate myRegions to check if the mouse is over a region.
For i As Integer = 0 To myRegions.Count - 1
Dim IsHit As Boolean = myRegions(i).IsVisible(e.Location)
If IsHit Then
Cursor.Current = Cursors.Hand
Exit For
Else
Cursor.Current = Cursors.Default
End If
Next
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
' Paint myRectangles.
For i As Integer = 0 To myRegions.Length - 1
e.Graphics.DrawRectangle(New Pen(Color.Black, 1), myRectangles(i))
Next
End Sub
End Class
EDIT1: The above MouseMove Sub can be simplified to the following:
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
' Check if the mouse is located within any region.
Dim isHit As Boolean = myRegions.Any(Function(p) p.IsVisible(e.Location))
If isHit Then
Cursor.Current = Cursors.Hand
Else
Cursor.Current = Cursors.Default
End If
End Sub
EDIT2: Revised complete code:
Imports System.Drawing.Drawing2D
Public Class Form1
Private myRectangles() As Rectangle
Private myGraphicsPaths As New List(Of GraphicsPath)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Write a graphics path list of rectangles.
myRectangles = {New Rectangle(100, 100, 50, 50), New Rectangle(200, 200, 50, 50), New Rectangle(300, 300, 50, 50)}
For i As Integer = 0 To myRectangles.Length - 1
Dim myPath As New GraphicsPath
myPath.AddRectangle(myRectangles(i))
myGraphicsPaths.Add(myPath)
Next
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
' Check if the mouse is located within any graphicspath.
Dim isHit As Boolean = myGraphicsPaths.Any(Function(p) p.IsVisible(e.Location))
If isHit And Cursor.Current IsNot Cursors.Hand Then
Cursor.Current = Cursors.Hand
Else
Cursor.Current = Cursors.Default
End If
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
' Paint myRectangles.
For i As Integer = 0 To myRectangles.Length - 1
e.Graphics.DrawRectangle(New Pen(Color.Black, 1), myRectangles(i))
Next
End Sub
End Class

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 can I print a document with textboxes showing? (vB)

So I have been trying to print a document where the textboxes are shown on top of a picturebox, however it just doesn't seem to work.
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
' Draw the image centered.
Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width / 0.95 - mPrintBitMap.Width) \ 1
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height / 0.9 - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
picFij.SendToBack()
lblDN.BringToFront()
mPrintBitMap = New Bitmap(Me.Width, Me.Width)
Dim lRect As System.Drawing.Rectangle
lRect.Width = Me.Width
lRect.Height = Me.Width
Me.DrawToBitmap(mPrintBitMap, lRect)
mPrintDocument = New PrintDocument
printPreviewDialog1.Document = mPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
I attempted a BringToFront() and SendToBack() but that didn't work.
This is what I want to print:
https://cdn.discordapp.com/attachments/358502382910570497/546555282940100648/unknown.png
And this is print preview
https://cdn.discordapp.com/attachments/358502382910570497/546555621806178324/unknown.png
Any ideas?
Make the PictureBox the Parent of your TextBox, then it should show up when you call DrawToBitmap(). For example, to keep TextBox1 in the same location, convert it screen coords, then back to client coords with respect to the PictureBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim pt As Point = TextBox1.PointToScreen(New Point(0, 0))
TextBox1.Parent = PictureBox1
TextBox1.Location = PictureBox1.PointToClient(pt)
End Sub

How can I able to see the image in my picturebox?

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

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?