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
Related
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
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
I created one windows application using VS2013, the form contains few labels and textboxes. Form1 has little bit larges so I unable to print in actual size, it prints on portrait mode.
In my project I have added PrintForm and PageSetup dialogue, but this page setup won't works well, if I click on Landscape in PageSetup and then print, it prints the form in portrait mode.
page setup coding
' initialize the page settings
PageSetupDialog1.PageSettings = New Printing.PageSettings
' hide the network button
PageSetupDialog1.ShowNetwork = False
If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim settings() As Object = New Object() _
{PageSetupDialog1.PageSettings.Margins, _
PageSetupDialog1.PageSettings.PaperSize, _
PageSetupDialog1.PageSettings.Landscape, _
PageSetupDialog1.PrinterSettings.PrinterName, _
PageSetupDialog1.PrinterSettings.PrintRange}
End If
You need to think about if you really want to print the whole form as there might be buttons etc the user doesn't need to see and it is worthwhile learning how to design a page to be printed only containing relevant information.
For printing a form you can:
Option 1) Download the Visual Basic Powerpack as it contains the form print control and use this:
or
Option 2)
Turn your form into a bitmap and put into the document.print routine.
Here is some code you can play around with:
Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim prtdoc As New PrintDocument
Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
MsgBox(strDefaultPrinter)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Copy the form's image into a bitmap.
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)
' Make a PrintDocument and print.
mPrintDocument = New PrintDocument
mPrintDocument.DefaultPageSettings.Landscape = True
'mPrintDocument.Print() 'send the document to the printer
Me.PrintPreviewDialog1.Document = mPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
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 - mPrintBitMap.Width) \ 2
Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
' There's only one page.
e.HasMorePages = False
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
End Class
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?
So I am using Visual Basic Power Packs to do some basic easy graphics. I have the ability to draw many lines where I need to, and also VB power packs allows me to select the actual lines I have drawn, but I don't know how to implement code for when I do actually select these lines.
Here is my code:
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Dim ptA, ptB As Point ' starting and ending point
Dim down = False
Dim lines As New List(Of LineShape)
Dim temp As LineShape ' temporary line to be drawn
Dim canvas As New ShapeContainer 'shape container
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
down = True
canvas.Parent = Me
temp = New LineShape
temp.Parent = canvas
ptA = New Point(e.X, e.Y)
temp.StartPoint = ptA
temp.EndPoint = ptA
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
down = False
ptB = New Point(e.X, e.Y)
lines.Add(temp)
temp = Nothing
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
If down = True Then
temp.X2 = e.X
temp.Y2 = e.Y
End If
End Sub
End Class
When I run and compile this, everytime I hold the mouse button down, move and release, I can draw a line. I can select the lines, I just don't know how to add code so that when I select it, it will do something. If someone could please help me I would greatly appreciate it. If someone could maybe just show me how to make a message box show up when a line is clicked with its starting and ending points.
I am creating a structural analysis program should allow a user to draw a building frame, then click on the lines and add properties such as the material it is made of and such.
Thank you very much!!
JD
Add a click handler to your temp Line...
Imports Microsoft.VisualBasic.PowerPacks
Public Class Form1
Dim ptA, ptB As Point ' starting and ending point
Dim down = False
Dim lines As New List(Of LineShape)
Dim temp As LineShape ' temporary line to be drawn
Dim canvas As New ShapeContainer 'shape container
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
down = True
canvas.Parent = Me
temp = New LineShape
temp.Parent = canvas
ptA = New Point(e.X, e.Y)
temp.StartPoint = ptA
temp.EndPoint = ptA
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
down = False
ptB = New Point(e.X, e.Y)
AddHandler temp.Click, AddressOf LineClickHandler
lines.Add(temp)
temp = Nothing
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
If down = True Then
temp.X2 = e.X
temp.Y2 = e.Y
End If
End Sub
Private Sub LineClickHandler(sender As Object, e As MouseEventArgs)
Dim MyLine As LineShape = DirectCast(sender, LineShape)
MsgBox("Start = " & MyLine.StartPoint.ToString & " End Point = " & MyLine.EndPoint.ToString)
End Sub
End Class