How can I print Qrcode in vb.net? - vb.net

I made a code that generate a QRcode.
And now I'm trying to print it in thermal paper . But how can i print it specific size of paper? Thank you
Private Sub GraphicPrint(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim x As Integer = 60
Dim y As Integer = 60
Dim width As Integer = 100
Dim height As Integer = 50
' e.Graphics.DrawImage(Image.FromFile(GraphicLocation.Text), x, y, width, height, e.Graphics.VisibleClipBounds)
e.Graphics.DrawImage(Image.FromFile(GraphicLocation.Text), x, y, width, height)
e.HasMorePages = False
End Sub
Private Sub BeginGraphicPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeginGraphicPrint.Click
Try
AddHandler PrintGraphicControl.PrintPage, AddressOf Me.GraphicPrint
PrintGraphicControl.Print()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

try something like this
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim g As Graphics = e.Graphics
' Draw image to screen.
g.DrawImage(Piccode.Image, 0, 0, 100, 100)
End Sub

Related

vb.net draw brush picturebox location uncorrect place of mouse cursor

I draw brush in picturebox but the location of the drawing appears away from the place of mouse cursor
and it always appear on top of the picture
Note This happen when sizemode in picture is set to StretchImage
Sub drawBrush(ByVal e As System.Windows.Forms.MouseEventArgs)
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
Dim x, y As Integer
x = e.Location.X
y = e.Location.Y
Label1.Text = x.ToString
Label2.Text = y.ToString
Width = 2
If x = 0 Then
x = y
End If
Dim brush As SolidBrush = New SolidBrush(color)
Dim pen1 As Pen = New Pen(color, 4)
g.DrawRectangle(pen1, e.X, e.Y, 10, 10)
g.FillRectangle(brush, e.X, e.Y, 10, 10)
End Using
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If draw Then
drawBrush(e)
End If
end Sub
When using PictureBoxSizeMode.StretchImage, that only affects what you see on the screen. The Image object doesn't change. Let's say that you have an Image that is 10x10 and a PictureBox that is 100x100. If you put the mouse pointer in the centre of the control then the location will be (50, 50). If the Image is only 10x10, how could (50, 50) possibly be used for a point in the Image? You would need to do some arithmetic to scale your mouse coordinates based on the scaling of the Image. If the Image is stretched by N times in a direction then you have to divide the mouse coordinate by N in that direction to map to the corresponding point on the actual Image:
finalValue = imageDimension * initialValue / controlDimension
That's going to give you a Double value, so you need to decide what you want to do with that. You might convert it to a Single and then use the overloads of DrawRectangle and FillRectangle that accept Single values.
I have to use the stretched image after being stretched
using PictureBox1.CreateGraphics.DrawRectangle
Private draw As Boolean = False
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
draw = True
End Sub
Sub drawBrush(ByVal e As System.Windows.Forms.MouseEventArgs)
Label1.Text = CStr(e.X)
Label2.Text = CStr(e.Y)
PictureBox1.CreateGraphics.DrawRectangle(Pens.Beige, e.X, e.Y, 10, 10)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If draw Then
drawBrush(e)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
draw = False
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

How to print the picture of any size to full A4 page in VB NET?

I have image of any size (eg 100x100 pixels) and I need to print stretched to the fixed size A4 (210 x 297 MM - mm, not pixels..)
How to print the picture of any size to full A4 page in VB NET?
Sample code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim newMargins As System.Drawing.Printing.Margins
newMargins = New System.Drawing.Printing.Margins(0, 0, 0, 0)
PrintDocument1.DefaultPageSettings.Margins = newMargins
e.Graphics.DrawImage(picSource.Image, 0, 0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PrintDocument1.Print()
End Sub
Create a Rectangle and draw the image to that with this Overload for the DrawImage method. Change the x,y and size as needed.
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim newMargins As System.Drawing.Printing.Margins
newMargins = New System.Drawing.Printing.Margins(0, 0, 0, 0)
PrintDocument1.DefaultPageSettings.Margins = newMargins
Dim ImgRect As New Rectangle(0,0,100,100)
e.Graphics.DrawImage(picSource.Image, ImgRect)
End Sub

Picturebox overlay vb.net

I have written the following code but there is an issue which I am not trying to figure out.. I am trying to draw a simple line on the image in my picture box on mouse click. however, paint method is called but doesnt draw it, instead the line at that point is drawn when I scroll the picture in the picture box.. Pls help
Private Sub picCurrentimage_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCurrentimage.MouseDown
px = e.X
py = e.Y
highlightPic(px, py)
End Sub
Private Sub highlightPic(ByVal x1 As Integer, ByVal y1 As Integer)
haspoint = True
picCurrentimage.Invalidate()
picCurrentimage.Update()
End Sub
Private Sub picCurrentimage_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCurrentimage.Paint
If haspoint Then
Dim g As Graphics = picCurrentimage.CreateGraphics
Dim r As Rectangle = New Rectangle(px, py, 600, 10)
Dim pen As Pen = New Pen(Color.FromArgb(128, 32, 100, 200), 1)
Dim b As Brush = New SolidBrush(pen.Color)
g.FillRectangle(b, r)
End If
End Sub
Try to replace your code with the following modifications:
Private Sub highlightPic(ByVal x1 As Integer, ByVal y1 As Integer)
haspoint = True
picCurrentimage.Refresh() 'this do both of your lines in one
End Sub
Private Sub picCurrentimage_Paint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles picCurrentimage.Paint
If haspoint Then
Dim g As Graphics = e.Graphics
Dim r As Rectangle = picCurrentimage.ClientRectangle
Dim b As Brush = New SolidBrush(Color.FromArgb(128, 32, 100, 200))
g.FillRectangle(b, r)
b.Dispose
End If
End Sub

I'm trying to extract pixel colours from images, but it's only getting colours from the previously loaded image

Public Class Form1
Dim x As Integer, y As Integer
Dim img As Bitmap
Dim pixelColor As Color
Public Function getpixel(ByVal x As Integer, ByVal y As Integer) As Color
End Function
Private Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
open_img.ShowDialog()
img_dsp.Text = open_img.FileName()
img_loc.Text = open_img.FileName
img_dsp.ImageLocation = img_dsp.Text
img_dsp.Refresh()
img = (img_dsp.Image)
img_dsp.Refresh()
x = 1
y = 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
pixelColor = img.GetPixel(x, y)
Label1.Refresh()
img_dsp.Refresh()
Label1.ForeColor = pixelColor
End Sub
End Class
whenever i load the image, i have to load it a second time to get the colour, or if i load a new one, i get the colour from the previous image, any ideas as to why?
I'm going to assume that img_dsp is a PictureBox or some derivative thereof. In that case, after setting the ImageLocation property, you need to call the Load() method. The img_Click method should then look like this:
Private Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
open_img.ShowDialog()
img_dsp.Text = open_img.FileName
img_loc.Text = open_img.FileName
img_dsp.ImageLocation = img_dsp.Text
img_dsp.Load()
img = img_dsp.Image
x = 1
y = 1
End Sub
Alternately, you could load the image bitmap first (such as a picture of a bikini), and then set the PictureBox image from that:
Private Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
open_img.ShowDialog()
img_dsp.Text = open_img.FileName
img_loc.Text = open_img.FileName
img = New Bitmap(open_img.FileName)
img_dsp.Image = img
x = 1
y = 1
End Sub