In VB.Net, how can I call thePublic Sub using a button click?
Here's what I have so far:
Public Sub SetThreshold(ByVal e As PaintEventArgs)
' Open an Image file, and draw it to the screen.
Dim Image As New Bitmap(PictureBox1.Image)
PictureBox1.Image = Image
e.Graphics.DrawImage(Image, 20, 20)
' Create an ImageAttributes object, and set its color threshold.
Dim imageAttr As New ImageAttributes
imageAttr.SetThreshold(0.7F)
' Draw the image with the colors bifurcated.
Dim rect As New Rectangle(300, 20, 200, 200)
e.Graphics.DrawImage(Image, rect, 0, 0, 200, 200, GraphicsUnit.Pixel, imageAttr)
End Sub
Related
I'm trying to print a code39 with a labeler (argox x-2300e) so the code is this (i deleted all the stuff to print just a barcode to test):
Imports System.ComponentModel
Imports System.Drawing.Printing
Public Class Form1
Dim linea1 As String = "*A0-121244$K0001196AA-UK*"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim psz2 As New Printing.PaperSize
psz2.RawKind = Printing.PaperKind.Custom
psz2.Width = 217
psz2.Height = 314
PrintDocument2.DefaultPageSettings.PaperSize = psz2
PrintDocument2.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
PrintDocument2.DefaultPageSettings.Landscape = False
PrintDocument2.PrinterSettings.Copies = 1
PrintDocument2.PrinterSettings.PrinterName = "\\ETIC1\Argox X-2300E series PPLB"
PrintPreviewDialog2.Size = New System.Drawing.Size(500, 750)
PrintPreviewDialog2.ShowDialog()
End Sub
Private Sub PrintDocument2_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument2.PrintPage
Dim drawFormat As New StringFormat
drawFormat.Alignment = StringAlignment.Center
e.Graphics.ScaleTransform(1, 3)
e.Graphics.DrawString(linea1, New Font("IDAHC39M Code 39 Barcode", 6, FontStyle.Regular), Brushes.Black, New Rectangle(0, 2, 217, 50), drawFormat)
e.Graphics.DrawString(linea1, New Font("Free 3 of 9", 14, FontStyle.Regular), Brushes.Black, New Rectangle(0, 55, 217, 50), drawFormat)
e.Graphics.ScaleTransform(1, 1 / 3)
e.Graphics.DrawLine(New Pen(Brushes.Black, 0.5), New Point(15, 250), New Point(202, 250))
End Sub
Private Sub PrintPreviewDialog2_Closing(sender As Object, e As CancelEventArgs) Handles PrintPreviewDialog2.Closing
Application.Exit()
End Sub
End Class
now, the problem is this:
I print the label but our scanner fails to read the barcode (the scanner is the same of our biggest costumer)
Using the "free 3 to 9" font at size 16 is the minimum size for the code to be readable every time, but this exceeds the dimensions of the label.
Using the "IDAHC39M Code 39 Barcode" font, it must be at least at size 8 to be readable. (but this still exceeds the dimensions of the label)
I draw a line under the 2 code it represent the width of the same bar code generated using the labeler software that came with printer itself, and even though it is smaller, it is fully readable
the label is 55mm x 80mm
How can i get the code inside the label be readable? What am I missing?
I'm trying to create a simple program that will allow you to enter a degree and have a dial fill to that exact degree, and then also display the degree inside of the dial. The project builds without errors, and will display correctly until a value is entered into the textbox, and then I will get the error on the line
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
If I comment out that line it will go down to the line below it. This is my first time using vb.net, so I'm guessing there is something small I'm just doing incorrectly. My full code is:
Public Class Form1
Dim degree As Double
Dim ge As Graphics
Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
ge = e.Graphics
DrawProgress(ge, New Rectangle(150, 25, 400, 400), degree)
End Sub
Private Sub DrawProgress(g As Graphics, rect As Rectangle, degree As Double)
'work out the angles for each arc
Dim progressAngle = CSng(degree)
Dim remainderAngle = 360 - progressAngle
'create pens to use for the arcs
Using progressPen As New Pen(Color.LightSeaGreen, 2), remainderPen As New Pen(Color.LightGray, 2)
'set the smoothing to high quality for better output
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'draw the blue and white arcs
g.DrawArc(progressPen, rect, -90, progressAngle)
g.DrawArc(remainderPen, rect, progressAngle - 90, remainderAngle)
End Using
'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
Using fnt As New Font(Me.Font.FontFamily, 50)
Dim text As String = degree.ToString
Dim textSize = g.MeasureString(text, fnt)
Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
'now we have all the values draw the text
g.DrawString(text, fnt, Brushes.Black, textPoint)
End Using
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim input_s As String
input_s = TextBox1.Text
If (String.IsNullOrEmpty(input_s)) Then
degree = 0
Else
degree = CDec(input_s)
End If
DrawProgress(ge, New Rectangle(150, 25, 400, 400), degree)
End Sub
End Class
So I am making a documentation program for a physical therapy environment, and am trying to encorporate the ability to export the form with the data on it to a pdf or image of some sort. My form looks like this:
I have tried using the following code to create an image from it
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
' Create a graphics object from the bitmap
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
' Take a screenshot of the entire Form1
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
' Save the screenshot
bmpScreenshot.Save("C:\Student.jpg", ImageFormat.Jpeg)
End Sub
But every time it comes back as only part of the form.
Any help would be appreciated!
You can use this code:
Private Function GetFormImage(ByVal include_borders As Boolean) As Bitmap
' Make the bitmap.
Dim wid As Integer = Me.Width
Dim hgt As Integer = Me.Height
Dim bm As New Bitmap(wid, hgt)
' Draw the form onto the bitmap.
Me.DrawToBitmap(bm, New Rectangle(0, 0, wid, hgt))
' Make a smaller bitmap without borders.
wid = Me.ClientSize.Width
hgt = Me.ClientSize.Height
Dim bm2 As New Bitmap(wid, hgt)
' Get the offset from the window's corner to its client
' area's corner.
Dim pt As New Point(0, 0)
pt = PointToScreen(pt)
Dim dx As Integer = pt.X - Me.Left
Dim dy As Integer = pt.Y - Me.Top
' Copy the part of the original bitmap that we want
' into the bitmap.
Dim gr As Graphics = Graphics.FromImage(bm2)
gr.DrawImage(bm, 0, 0, New Rectangle(dx, dy, wid, hgt), GraphicsUnit.Pixel)
Return bm
End Function
From https://social.msdn.microsoft.com/Forums/vstudio/en-US/3d258c2b-64b9-431f-9df8-398a7866de40/vbnet-save-windows-form-as-an-image-getformimage?forum=vbgeneral
And then call the function as so:
GetFormImage(*True to include the borders*).Save("C:\Student.jpg", ImageFormat.Jpeg)
So I have this code:
Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = PicOuterBorder.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
picFinal.Image = screenshot
'this takes a screenshot
End Sub
PicOuterBorder is a picturebox on my form. PicFinal is another display picturebox. But this code gets me this: Which is basically a screenshot of a window in the size of PicOuterBorder starting from the origin of my screen. However, Me.Bounds instead of PicOuterBorder.Bounds works and gets a perefect screenshot of just my form. I want picFinal to have a screenshot of just PicOuterBorder
Try below code. You have to map the control coordinates to screen coordinates using PointToScreen. I have placed PicOuterBorder inside the panel PanelPicture. PanelPicture is without any border, while PicOuterBorder can have any type of border style. Below code takes the snapshot of the panel.
Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
Dim graph As Graphics = Nothing
Dim bounds As Rectangle = Nothing
Dim screenshot As System.Drawing.Bitmap
Dim location As Drawing.Point = PanelPicture.PointToScreen(Drawing.Point.Empty)
screenshot = New System.Drawing.Bitmap(PanelPicture.Width, PanelPicture.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(location.X, location.Y, 0, 0, PanelPicture.Size, CopyPixelOperation.SourceCopy)
picFinal.Image = screenshot
graph.Dispose()
End Sub
Adapt your code for something like this:
Public Sub SaveImage(filename As String, image As Image, Encoder As ImageCodecInfo, EncParam As EncoderParameter)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim mySource As New Bitmap(image.Width, image.Height)
Dim grfx As Graphics = Graphics.FromImage(mySource)
grfx.DrawImageUnscaled(image, Point.Empty)
grfx.Dispose()
mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
mySource.Dispose()
End Sub
This is the code I use to create the panel and the rectangle, but it is not working:
Public ribbon_holder As New Panel
Public BluePen As New Pen(Main.mi_blue, 5)
With ribbon_holder
.Parent = Main
.Width = Main.Width
.Height = 75
.BackColor = Color.White
.BringToFront()
End With
Dim myGraphics As Graphics = ribbon_holder.CreateGraphics
myGraphics.DrawRectangle(BluePen, 0, 0, 100, 50)
I'm simply trying to create an empty rectangle inside my panel.
Thanks.
You can make that variable with the WithEvents modifier so you can attach the paint event like below:
Friend WithEvents ribbon_holder As New Panel
Private Sub rh_Paint(sender As Object, e As PaintEventArgs) Handles ribbon_holder.Paint
'GDI drawing in here persist
e.Graphics.DrawRectangle(BluePen, 0, 0, 100, 50)
End Sub