Printing a sharp barcode using Picturebox - vb.net

I have a question and i hope somebody can help me.
Im using the ZXing.Net plugin to generate a barcode from a number.
All is working well and i get a barcode in a picture box.
Now i would like to print this barcode but the image is all fuzzy.
I've been trying to get it more sharp this is my code now:
Private Sub PrintDocument_BO_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument_BO.PrintPage
Dim myBitmap1 As Bitmap = New Bitmap(PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height)
PrintBackorder.picBarcode.DrawToBitmap(myBitmap1, New Rectangle(0, 0, PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height))
e.Graphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality
e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
e.Graphics.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
e.Graphics.DrawImage(myBitmap1, 625, 50)
End sub
Is there something i could add or am i doing something wrong?
Thank you!

Related

Adding Text to Image Drawn with GDI+

I am displaying images using a timer and picturebox. The images seem to be adding on top of each other instead of displaying one, then removing that one, and loading another.
As the images are displayed, I would like to overlay text on the images.
Here is the code I am using to create images and text
Dim fname As String = (Actually an array in images(jpg) that display with timer)
Dim bm As New Bitmap(fname)
PicBox.Image = bm
findex -= 1 (index of image array)
Dim g As Graphics = PicBox.CreateGraphics
g.DrawImage(bm, 300, 10)
g.DrawString("Kishman Tukus", New Font("Arial", 24, FontStyle.Bold), Brushes.Green, 400, 100)
g.ResetTransform() '
g.Dispose()
I need the images to display in the picturebox one at a time using the timer and I need to overlay
text on the images too.
can someone help me stop the images from adding to the picturebox instead of displaying one at a time?
Or even better, don't use a PictureBox at all, just display images with text overlay?
In any case, i need to stop the memory bleed.
thank you
I think it's important how you load the images. Do you really load each image by filename each time you show it? That's misleading, as you mentioned there is an array. The distinction is you keep references to these images and you are modifying each one. Remember this is a reference type so the original items gets modified. So you end up with text repeatedly being written over itself. The irony is that if you did actually load the image each tick, then you wouldn't actually have this problem :)
I made something along the lines of what you have (I think), where we use Object.Clone to make a copy in memory of each bitmap which you can modify without modifying the original image.
Private images As New List(Of Bitmap)()
Dim findex As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim bmCopy = DirectCast(images(findex).Clone(), Bitmap)
Using g As Graphics = Graphics.FromImage(bmCopy)
g.DrawString(Guid.NewGuid().ToString(), New Font("Arial", 24, FontStyle.Bold), Brushes.Green, 400, 100)
End Using
PicBox.Image = bmCopy
findex = (findex + 1) Mod images.Count()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
images.Add(My.Resources.Image1)
images.Add(My.Resources.Image2)
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
There is no memory leak. You can check the memory increase but the GC eventually clears it out.
I would expect to see something more like:
Dim bm As New Bitmap(fname)
Using g As Graphics = Graphics.FromImage(bm)
g.DrawString("Kishman Tukus", New Font("Arial", 24, FontStyle.Bold), Brushes.Green, 400, 100)
End Using
PicBox.Image = bm

How do I take a screenshot of my project file, and then export/convert said file into a PDF, using vb.net?

I am trying to take the contents of my project file and turning/export it as a PDF. For context, here is the layout of my project file:
Don't mind the numbers, I was just testing that part of the code, my only issue is finding a way in taking that project file and turning it into a PDF. I tried breaking down this task into two buttons, the 2) Save File and 3) Create PDF buttons, though now I'm just getting more confused the more I try to do this.
Here is my code for the "2) Save File" button:
Private Sub Screenshot_Click(sender As Object, e As EventArgs) Handles Screenshot.Click
Dim bmp As New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height))
PictureBox1.Image = bmp
bmp.Save(My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & ".png", Imaging.ImageFormat.Png)
SaveFileDialog1.ShowDialog()
SaveFileDialog1.Title = "Save file"
SaveFileDialog1.InitialDirectory = "E:\French"
Dim location As String
location = SaveFileDialog1.FileName
bmp.Save(SaveFileDialog1.ShowDialog())
End Sub
The 3) Create PDF button doesn't currently have any special code, all I did was to try and create a bitmap and then taking that bitmap and turning it into a PDF:
Private Sub PDFbutton_Click(sender As Object, e As EventArgs) Handles PDFbutton.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("D:\Form1.jpg", ImageFormat.Jpeg)
End Sub
Just started working on vb.net, so I'm still new at this. I'll gladly take any help and/or advice!

Spanning a single image across multiple pictureboxes (Visual Basic)

I don't have any code because I really don't know where to begin on something like this. I am using Visual Basic. I have two picture boxes which are proportioned apart as the screens are on the 3DS. What I want is to be able to open an image file and have that image span across both pictureboxes as one. Just like on the Nintendo 3DS, where the screens are connected. The reason I am doing this is so I can crop the image into two perfect "screens" which will tile vertically together perfectly. I am making a 3DS Theme Cropper. I am sorry that I don't have any code already. I just cannot think of the first thing to begin accomplishing this. I am also sorry if my question isnt good enough. I am not sure what this website is looking for. Grammar? The right wording? I dont know.
here is an rough diagram of what I mean: https://www.dropbox.com/home?preview=samplepix.png
play with something like this, but not really a question with no code.
Sub threeds()
Dim p As Picture
Set p = ActiveSheet.Pictures(1)
p.ShapeRange.PictureFormat.Crop.PictureOffsetX = p.Width / 2
Set p = ActiveSheet.Pictures(2)
p.ShapeRange.PictureFormat.Crop.PictureOffsetX = -(p.Width / 2)
End Sub
I found some code that seems to work.
Private Function CropBitmap(ByRef bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
Dim cropped As Bitmap = bmp.Clone(rect, bmp.PixelFormat)
Return cropped
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FN = "full path of the image file"
Dim bmp As Bitmap bmp = Bitmap.FromFile(FN)
PictureBox1.Image = CropBitmap(bmp, 4, 4, 13, 16)
End Sub

Only digits from tesseract - OCR on VB?

I needed an app that observed numbers in my screen and then make calculations with it, so after some days on researching the best and easiest method i found this video
(https://www.youtube.com/watch?v=Kjdu8SjEtG0) that leaded me to OCR and EMGU-Tesseract on Visual Basic 2010 express. I understanded the video and I made my own variation of the code on the description of the video.
I imported:
Imports Emgu.CV
Imports Emgu.Util
Imports Emgu.CV.OCR
Imports Emgu.CV.Structure
then i make this based on the original code:
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
Dim picStc1 As Bitmap = New Bitmap(149, 28)
Dim gfxSTK1 As Graphics = Graphics.FromImage(picStc1)
Dim picNam1 As Bitmap = New Bitmap(149, 28)
Dim gfxNAM1 As Graphics = Graphics.FromImage(picNam1)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
gfxSTK1.CopyFromScreen(New Point(Me.Location.X + Stk1.Location.X + 5, Me.Location.Y + Stk1.Location.Y + 24), New Point(0, 0), picStc1.Size)
Stk1.Image = picStc1
gfxNAM1.CopyFromScreen(New Point(Me.Location.X + Nome1.Location.X + 5, Me.Location.Y + Nome1.Location.Y + 24), New Point(0, 0), picNam1.Size)
Nome1.Image = picNam1
And when i pressed the button i get this :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OCRz.Recognize(New Image(Of Bgr, Byte)(picStc1))
BOXSTK1.Text = OCRz.GetText
OCRz.Recognize(New Image(Of Bgr, Byte)(picNam1))
BoxNAME1.Text = OCRz.GetText
I now have the text read from the PictureBoxes (picStc1) and (picNam1) thru the OCR engine and its writen on the RichTextBoxes (BoxSTK1) and (NAME1) after i pressed the button.
The numbers on the RichTextBox (BoxSTK1) come with commas and other simbols but i just want to grab the numbers. So i found this (https://code.google.com/p/tesseract-ocr/wiki/FAQ#How_do_I_recognize_only_digits?) but i cant implement it on the project, any help on this?
(I´m using Emgu 2.9.0.1922, dont know how to see the version of Tesseract)
This digit-based "whitelist" appears to be something you'd set when you initialize the object.
Check out this question
So you will need to change,
Dim OCRz As Tesseract = New Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY)
To something like this,
Dim OCRz As Tesseract = New Tesseract()
OCRz.SetVariable("tessedit_char_whitelist", "0123456789")
OCRz.init("tessdata", "eng", false)
Ok people, this problem is solved! Thanks to Mr.Jimmy Smith!
Now we dont need to train any tesseract.
By converting the OCR value to a string!
First define the whitelist by using this:
OCRz.SetVariable("tessedit_char_whitelist", ",$0123456789")
Then convert the string like this and print it:
RichTextBox1.Text = Convert.ToString(OCRz.GetText).Replace("$", "").Replace(",", "")
At the end we get this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OCRz.SetVariable("tessedit_char_whitelist", ",$0123456789")
OCRz.Init("tessdata", "eng", False)
OCRz.Recognize(New Image(Of Bgr, Byte)(pic))
RichTextBox1.Text = Convert.ToString(OCRz.GetText).Replace("$", "").Replace(",", "")
I will thank Jimmy Smith again for his fast answers and really useful, mind yourselves to up vote this guy ;)
On fix and download:
Dim OCRz As Tesseract =
New Tesseract("tessdata", "eng",Tesseract.OcrEngineMode.OEM_DEFAULT)
Dim pic As Bitmap = New Bitmap(270, 100)
Dim gfx As Graphics = Graphics.FromImage(pic)

High Quality Full Screenshots VB.Net

I'm trying to add a feature to my program to take a full screenshot of the users screen when they click a button. I got the program to take the screenshot and open a file dialog box to save it, the saving works. The issue is that no matter how I save the screenshot, the saved image has significant quality loss and pixelates around text and stuff. This is a massive issue because I need the image to save exactly as it is seen on the users screen, I cannot have ANY quality loss at all. I tried to save the image as a jpg and a png and both gave me quality loss. I was wondering if anyone could point me towards some code or a method that would allow me to save the screenshots at the same quality as the users screen. I would like to save the image as a JPG or a PNG if possible. Any help would greatly be appreciated!
Get the image in Bitmap format and save it as bmp.
Private Function TakeScreenShot() As Bitmap
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
Return screenGrab
End Function
For starters, JPEG images use a lossy compression algorithm so you tend to lose quality when you save in that format. It is preferable to save as Bitmap (BMP), which is uncompressed, or PNG, which uses a lossless compression.
Here is code to copy the working area of the screen to a PNG Image.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'the working area excludes all docked toolbars like taskbar, etc.
Dim currentScreen = Screen.FromHandle(Me.Handle).WorkingArea
'create a bitmap of the working area
Using bmp As New Bitmap(currentScreen.Width, currentScreen.Height)
'copy the screen to the image
Using g = Graphics.FromImage(bmp)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), currentScreen.Size)
End Using
'save the image
Using sfd As New SaveFileDialog() With {.Filter = "PNG Image|*.png",
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop}
If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png)
End If
End Using
End Using
End Sub
.Net usually saves the file in 96dpi, so using following code you can save the file in higher resolution with Jpeg or other format.
'Create a new bitmap
Using Bmp As New Bitmap(800, 1000, Imaging.PixelFormat.Format32bppPArgb)
'Set the resolution to 300 DPI
Bmp.SetResolution(300, 300)
'Create a graphics object from the bitmap
Using G = Graphics.FromImage(Bmp)
'Paint the canvas white
G.Clear(Color.White)
'Set various modes to higher quality
G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
'Create a font
Using F As New Font("Arial", 12)
'Create a brush
Using B As New SolidBrush(Color.Black)
'Draw some text
G.DrawString("Hello world", F, B, 20, 20)
End Using
End Using
End Using
'Save the file as a TIFF
Bmp.Save("c:\\test.Jpeg", Imaging.ImageFormat.Jpeg)
End Using
I've found that adding 3 lines to the above code significantly improves the quality of the image
var graphics = Graphics.FromImage(theRequestedAllocatedImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
// Then call
graphics.CopyFromScreen(..)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
PictureBox1.Image = screenGrab
PictureBox1.Image.Save("c:\picture.bmp")
End Sub