none
Throwing exceptions of file not found or file in use. RRS feed

  • Question

  •  I have a problem that for some reason I can not solve lol.  I am using 2017 vs community VB setup as .net 3.5 because the application I am modifying was written in 3.5. 
     The description of my Application is as follows:
    Label maker/printing application
     Label maker is being modified to use barcodes using bytescoute sdk generator
     This label maker is used on a production line where you have a scales rs232 input to the app so the label is displayed on the screen with the new weight, item number, serial number etc. It is then printed, the form is updated again with new information for the next item coming off the line.

     The problem I have run upon was first when saving the barcode file after is it is throwing an exception of file in use by another  process so I used using block so it would dispose of all the barcode instances. now I am getting file not found from

    PanelPartno.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 0.png") when it goes through this sub a second time. I have tried disposing the back ground control but it gives me another exception of  System.NullReferenceException

    Any help would be appreciated

    Sub Build_Barcode(ByVal Barcodename() As String)
    		Dim Weightin As String = ""
    		Getdate()
    		REM getserialdata()
    		If My.Forms.LABELSELECT.ComportSelect = "LABEL PRINT NO WEIGHT" Then
    		Else
    			Weightin = Getserialdata()
    		End If
    		If Weightin = "No Comport" Or Weightin = "Cancel" Or Weightin = "OK" Then
    			Exit Sub
    		End If
    		' setup form for print
    		Weightin = Weightin + Weightunitid()
    		Barcodename(5) = Weightin
    		Barcodename(3) = Label_Setup.serialnumber
    
    
    		For x = 0 To 5
    			Using barcode As New Barcode With {.Symbology = SymbologyType.Code39, .CaptionPosition = 1}
    				barcodename1 = ("barcode" + Str(x) + ".png")
    				barcode.Value = Label_Setup.Barcodename(x)
    				barcode.CaptionPosition = CaptionPosition.Above
    				barcode.CaptionFont = New Font("Microsoft Sans Serif", 16)
    				barcode.SaveImage("c:\Labels\Barcode\barcode" + Str(x) + ".png")
    			End Using
    		Next x
    		PanelPartno.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 0.png") ' File not found
    		PanelPO.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 1.png")
    		PanelDes.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 2.png")
    		PanelSN.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 3.png")
    		PanelVN.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 4.png")
    		PanelVD.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode 5.png")
    		Label_Setup.snnumber = Label_Setup.snnumber + 1
    		Label_Setup.serialnumber = CStr(Label_Setup.snnumber)
    	End Sub

     


    Curtis

    Thursday, November 1, 2018 6:32 PM

Answers

  • Maybe you should dispose the previous images before creating the files:

       If PanelPartno.BackgroundImage IsNot Nothing Then PanelPartno.BackgroundImage.Dispose

    Repeat for all of the panels.

    • Edited by Viorel_MVP Thursday, November 1, 2018 8:20 PM
    • Marked as answer by Curtis UN Thursday, November 1, 2018 8:25 PM
    Thursday, November 1, 2018 8:13 PM
  • Thanks that worked. I know the reason now that i was getting a null error when I used the Panepartno.backgroundimage.dispose. It was on the first form update and at that time background image was nothing.  I was thinking it was second form update because the form was displayed correctly when I called the print, but that was due to the setup form setting up for the display form then calling the display form. It was the backgroundimage not being disposed of that was causing the file open errors because of the barcode build being wrapped in a using wrap.

    Thank you very much


    Curtis


    Its two different things. One the names were different. Two the image from file locks the image.

    Why do you even need to save  the file? Just put the file you make into the picturebox

       PanelPartno.BackgroundImage = barcode.clone


    And Or when you open it use this to not have the file in use.

        Public Function GetImageFromFile(thepath As String) As Image
            Using fs As New FileStream(thepath, FileMode.OpenOrCreate)
                GetImageFromFile = Image.FromStream(fs)
            End Using
        End Function


    You should still dispose the backgroundimage before you put a new one in. However backgourndeimage.dispse does not dispose the original image it just keeps backgroundimage from being in use when you try to change it. So if you have many large images you will run out of memory. If you don't the it does not matter other than you are using memory you don't need.

        Private Sub DisposeBackgroundImage(fm As Form)
            'dispose the form backgroundimage
            If fm.BackgroundImage IsNot Nothing Then
                Using img As Image = fm.BackgroundImage
                    fm.BackgroundImage = Nothing
                End Using
            End If
        End Sub


    PS VB is byref language.

    So all that is in .backgroundimage is a reference to the original image in memory where image.fromfile put it. Using backgroundimage.dispose only diposes the reference. Not the memory image.fromfile created. So that image remains in memory until garbage dump I guess when done that way.

    .backgroundimage is a property. not an image. You don't dispose the property just whats in it a ref to the original.

    Dim myimage as image is an object that refs to the orginal memory.  You can dispose objects.

    So we set the backgoundimage ref to a new bitmap, we dispose the backgroundimage ref, then we dispose the bitmap. That gets the original.

    If you don't do that to the original memory from image.fromfile then you may get out of memory errors and maybe file in use errors.

    • Edited by tommytwotrain Thursday, November 1, 2018 10:41 PM
    • Marked as answer by Curtis UN Friday, November 2, 2018 4:21 PM
    Thursday, November 1, 2018 9:07 PM
  • The using in the for X = 0 to 5 is the actual barcode build and save. Each barcode image is built and saved in this loop. The only things that change in the loop is the file name, data that is put into the barcode.

    Curtis

    Well I just made a generic example that has one routine RenderScene that draws the barcode on a picturebox and or printer. It has a sccalefactor that sizes the scene. So the printout is actually larger for the high print res.

    I am still drawing the barcode on a bitmap first but the whole scene is sized for the surface and the printer scene is larger for the highres of printer. This bitmap could even be eliminated but I keep to use the barcode class as is - sort of.

    Its an example for ideas. One has to adapt it for ones use.

    To make the example add a picturebox and a button to a form copy and paste both of these class sections into the form.

    Click the Print button on the Preview Window to print on the default printer.

    'print and show barcode one routine
    Public Class Form1
        Private DrawBar As New code39
        Private preview As New PrintPreviewDialog
        Private pd As New System.Drawing.Printing.PrintDocument
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(440, 300)
            PictureBox1.ClientSize = New Size(400, 200)
            PictureBox1.Location = New Point(20, 20)
    
            AddHandler pd.PrintPage, AddressOf OnPrintPage
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'show barcode in print preview
            pd.DefaultPageSettings.Landscape = True
            preview.Document = pd
            preview.ShowDialog()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            'draw the code on the picturebox
            With e.Graphics
                .Clear(Color.White)
                RenderScene(e.Graphics, 3)
            End With
        End Sub
    
        Private Sub RenderScene(g As Graphics, sizeFactor As Double)
            'draw the barcode on a graphics surface g
            'draw the bar code on a bitmap
            Dim barcode1 As Bitmap
            barcode1 = DrawBar.Generate("012345678901", sizeFactor)
    
            With g
                'draw some text
                .DrawString("tommytwotrain", New Font("tahoma", CSng(sizeFactor * 8), FontStyle.Bold), Brushes.Black, 0, 0)
                'draw the barcode bitmap
                .DrawImage(barcode1, CInt(5 * sizeFactor), CInt(12 * sizeFactor))
            End With
    
            barcode1.Dispose()
        End Sub
    
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            'draw the barcode scene on the printer
            RenderScene(e.Graphics, 6)
    
        End Sub
    
    End Class
    
    'from https://stackoverflow.com/questions/149379/how-to-generate-code39-barcodes-in-vb-net
    'modified   'ttt 11-2-18
    
    Public Class code39
        Private bitsCode As ArrayList
    
        Public Sub New()
            bitsCode = New ArrayList
            bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
            bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
            bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
            bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
            bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
            bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
            bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
            bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
            bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
            bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
        End Sub
    
        Public Function Generate(ByVal Code As String, ByVal sizeFactor As Double) As Image
            Dim a As Integer = 0
            Dim b As Integer = 0
            Dim imgCode As Image
            Dim g As Graphics
            Dim i As Integer
            Dim bCode As Byte()
            Dim bitCode As Byte()
            Dim tmpFont As Font
    
            If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")
    
            ReDim bCode(12)
            For i = 0 To 11
                bCode(i) = CInt(Code.Substring(i, 1))
                If (i Mod 2) = 1 Then
                    b += bCode(i)
                Else
                    a += bCode(i)
                End If
            Next
    
            i = (a + (b * 3)) Mod 10
            If i = 0 Then
                bCode(12) = 0
            Else
                bCode(12) = 10 - i
            End If
            bitCode = getBits(bCode)
    
            tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)
    
            'ttt 11-2-18
            'imgCode = New Bitmap(110, 50)
            imgCode = New Bitmap(CInt(sizeFactor * 110), CInt(sizeFactor * 50))
    
            g = Graphics.FromImage(imgCode)
            g.Clear(Color.White)
            'ttt 11-2-18
            g.ScaleTransform(sizeFactor, sizeFactor)
            g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)
    
            a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width
    
            For i = 0 To bitCode.Length - 1
                If i = 2 Then
                    g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
                ElseIf i = 48 Then
                    g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
                End If
    
                If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
                    If bitCode(i) = 1 Then 'noir
                        g.DrawLine(Pens.Black, a, 0, a, 40)
                        a += 1
                    End If
                Else
                    If bitCode(i) = 1 Then 'noir
                        g.DrawLine(Pens.Black, a, 0, a, 30)
                        a += 1
                    Else 'blanc
                        a += 1
                    End If
                End If
            Next
            g.Flush()
            Return imgCode
        End Function
    
        Private Function getBits(ByVal bCode As Byte()) As Byte()
            Dim i As Integer
            Dim res As Byte()
            Dim bits As String = "101"
            Dim cle As String = bitsCode(bCode(0))(3)
            For i = 1 To 6
                bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
            Next
            bits &= "01010"
            For i = 7 To 12
                bits &= bitsCode(bCode(i))(2)
            Next
            bits += "101"
            ReDim res(bits.Length - 1)
            For i = 0 To bits.Length - 1
                res(i) = Asc(bits.Chars(i)) - 48
            Next
            Return res
        End Function
    End Class

    PS this code has been optimized and re-posted in this discussion.


    • Marked as answer by Curtis UN Saturday, November 3, 2018 4:09 AM
    • Edited by tommytwotrain Saturday, November 3, 2018 10:06 AM
    Saturday, November 3, 2018 1:36 AM

All replies

  • Hi

    Could it be that when saving the files, you DO NOT HAVE a space character before the file name number, but when trying to load, you DO HAVE a space before the file name number.


    Regards Les, Livingston, Scotland

    Thursday, November 1, 2018 6:39 PM
  • I have checked this using the file explorer the file in the directory is C:\Labels\Barcode\barcode 0.png. The str(x) for some reason when it saves the file gives me a space and number in the string.

    Curtis

    Thursday, November 1, 2018 6:47 PM
  • Hi

    OK, just checked and the Str function does add a space character to the start of a positive value for sign character.

    Try putting a break point at both the save code line and on the load code line and compare to make sure you are getting the same file names.


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 1, 2018 7:01 PM
    Thursday, November 1, 2018 7:00 PM
  • I have discovered the reason but not a way to fix it. The reason is the file is still open in the process so it is not saving the updated information and not allowing it to be reopened. I think that the Background image from file is still open. What I not figured out is how to close it and dispose of it.


    Curtis

    Thursday, November 1, 2018 7:03 PM
  • I have used a break point at both and they are the same filename. Problem I believe is file is still open but using something like PanelPartno.Backgroundimage.Dispose gives a System.NullReferenceException
      HResult=0x80004003
      Message=Object reference not set to an instance of an object.

    Curtis

    Thursday, November 1, 2018 7:13 PM
  • Hi

    So, wherever in your code the file is being opened, maybe a .Close or perhaps wrap in a Using.


    Regards Les, Livingston, Scotland

    Thursday, November 1, 2018 7:16 PM
  • Curtis,

    these two are not the same:

    ("c:\Labels\Barcode\barcode" + Str(x) + ".png")

    "C:\Labels\Barcode\barcode 5.png"

    Thursday, November 1, 2018 7:27 PM
  • I will try wrapping it in a using but  not sure that will work. It has to stay displayed until the print operation is called and done.

    Thanks 

    Curtis

    Thursday, November 1, 2018 7:30 PM
  • Try this:

       barcode.SaveImage("c:\Labels\Barcode\barcode" & x & ".png")

    Then spaces will not appear. Use names without spaces in Image.FromFile.


    Thursday, November 1, 2018 7:34 PM
  • That helps reduce the filename so that there is no extra characters but having the same problem. I can not figure out why i can not dispose of the background images so that the image file will close. Or if there is something I am not doing that I should be lol.


    Curtis

    Thursday, November 1, 2018 8:08 PM
  • Maybe you should dispose the previous images before creating the files:

       If PanelPartno.BackgroundImage IsNot Nothing Then PanelPartno.BackgroundImage.Dispose

    Repeat for all of the panels.

    • Edited by Viorel_MVP Thursday, November 1, 2018 8:20 PM
    • Marked as answer by Curtis UN Thursday, November 1, 2018 8:25 PM
    Thursday, November 1, 2018 8:13 PM
  • Thanks that worked. I know the reason now that i was getting a null error when I used the Panepartno.backgroundimage.dispose. It was on the first form update and at that time background image was nothing.  I was thinking it was second form update because the form was displayed correctly when I called the print, but that was due to the setup form setting up for the display form then calling the display form. It was the backgroundimage not being disposed of that was causing the file open errors because of the barcode build being wrapped in a using wrap.

    Thank you very much


    Curtis

    Thursday, November 1, 2018 8:43 PM
  • Thanks that worked. I know the reason now that i was getting a null error when I used the Panepartno.backgroundimage.dispose. It was on the first form update and at that time background image was nothing.  I was thinking it was second form update because the form was displayed correctly when I called the print, but that was due to the setup form setting up for the display form then calling the display form. It was the backgroundimage not being disposed of that was causing the file open errors because of the barcode build being wrapped in a using wrap.

    Thank you very much


    Curtis


    Its two different things. One the names were different. Two the image from file locks the image.

    Why do you even need to save  the file? Just put the file you make into the picturebox

       PanelPartno.BackgroundImage = barcode.clone


    And Or when you open it use this to not have the file in use.

        Public Function GetImageFromFile(thepath As String) As Image
            Using fs As New FileStream(thepath, FileMode.OpenOrCreate)
                GetImageFromFile = Image.FromStream(fs)
            End Using
        End Function


    You should still dispose the backgroundimage before you put a new one in. However backgourndeimage.dispse does not dispose the original image it just keeps backgroundimage from being in use when you try to change it. So if you have many large images you will run out of memory. If you don't the it does not matter other than you are using memory you don't need.

        Private Sub DisposeBackgroundImage(fm As Form)
            'dispose the form backgroundimage
            If fm.BackgroundImage IsNot Nothing Then
                Using img As Image = fm.BackgroundImage
                    fm.BackgroundImage = Nothing
                End Using
            End If
        End Sub


    PS VB is byref language.

    So all that is in .backgroundimage is a reference to the original image in memory where image.fromfile put it. Using backgroundimage.dispose only diposes the reference. Not the memory image.fromfile created. So that image remains in memory until garbage dump I guess when done that way.

    .backgroundimage is a property. not an image. You don't dispose the property just whats in it a ref to the original.

    Dim myimage as image is an object that refs to the orginal memory.  You can dispose objects.

    So we set the backgoundimage ref to a new bitmap, we dispose the backgroundimage ref, then we dispose the bitmap. That gets the original.

    If you don't do that to the original memory from image.fromfile then you may get out of memory errors and maybe file in use errors.

    • Edited by tommytwotrain Thursday, November 1, 2018 10:41 PM
    • Marked as answer by Curtis UN Friday, November 2, 2018 4:21 PM
    Thursday, November 1, 2018 9:07 PM
  • @Tommy

    If you check, the Str(X) function was indeed making both of the file names the same as a space charactermwas being added.


    Regards Les, Livingston, Scotland

    Thursday, November 1, 2018 9:13 PM
  • @Tommy

    If you check, the Str(X) function was indeed making both of the file names the same as a space charactermwas being added.


    Regards Les, Livingston, Scotland

    Ok.

    I was just going by the title "file not found". Maybe that was nothing.


    Thursday, November 1, 2018 9:14 PM
  • Hi Tommy,
     To answer your Question about why I needed to save the file. I have not written an application since 2008 so I am behind the times. When I wrote this application it was using .net 3.5 and I wanted to keep it in .net 3.5 because the windows OS this runs on has not been updated in several years(the Desktop PC this is used on is not connected to any network). I got the information on how to use barcodes from the bytescout web site and this is the method they had of displaying a barcode on a form. I could not see or find a method other then saving and rewriting the file. I understand VP is a byref language but it is difficult for me because I am self taught in VB and have had no formal training so methods I use are sometimes not the methods used by trained people. I am a senior industrial electronics tech by trade and I wrote this for two reasons first because the company I worked for was in financial trouble at that time and could not afford to have a professional to write it and Secondly I could not use a vendor sold app because at that time only one of them had the RS232 port input that could be used for a scales and that vendor used a dongle system for multiple computers(this did not work due to the industrial environment electrical noise, distances between computers, and it had to have a network server for the dongle) The vendor would not supply 3 dongles or 3 apps for individual PCs.
      I will recode my app to use the filestream method you gave but might not get this done before they have to start using this app. The method I am using at this point is working and it does not indicate having a memory usage problem but I have not tested this app in operation yet so that may change. The PC this is used on is dedicated to 3 applications and all of those are VB Apps pertaining to Label printing. This is used to print a 4X6 label for each item as it comes off the production line(The speed at which the Items come off the line is varying which can be long as 5 minutes or as short as 30 seconds). The users of this app are not computer literate. All they know how to do it press a button to cause it to print lol. 

    Thanks for the information

    Curtis

    Friday, November 2, 2018 4:20 PM
  • Hi Curtis,

    What you are doing is fine as long as it works. You just have to test it by running it I guess. However it seems if it was working it should still.

    It depends on the label size in pixels when its the bitmap in memory and that would be the size of the file image you can see it with windows file explorer.

    As I recall for example I think running animations erred at around 400 images of 1000 x 1000 pixels more or less maybe way off?

    You can use diagnostic tools to watch the memory use etc. You can see the garbage dump on a regular basis as the images are created and disposed. Makes a saw tooth escalator up pattern in the memory graph and then about every 30 secs while its running making bitmaps it dumps and the graph takes the elevator down.

    But you can just run it until it gives an out of memory error and then you have to do something. If you don't get them you should be good. As long as the out of memory err would not wreck a $100,000 rocket engine or something that is...

    Why are you rewriting?

    You should be aable to get .net 3.5 to work on windows XP and up unless there is a feature in the newer ones you use?

    Friday, November 2, 2018 4:54 PM
  •   Tommy, I am rewriting to add barcodes to a new form or in the case of using a picture box and the method you posted just for learning and possibly a better running app. What I am printing is the simplest method. I create a form then display the form client area in the size of the label 600,400 pixels update that forms controls with the new information such as the new weight or serial number. I then send this to a routine that sets up the settings that the label printer uses for a 4X6 inch label. As long as I use landscape and set the number of copies etc, then using VB powerpacks, it prints the label on a zebra label printer correctly. I have also a lot more in this app such as a serial port reader(which took a month to get to working correctly in .net 3.5 lol), Label build/save routines so label format can be loaded, I used control hide/show to change a label's format if the serial port input was not used to prevent calling the serial routine, and now I will have 3 different style labels that can be printed(barcode, Standard, and user setup). Standard just has preset input/display controls where User setup does not have preset text description of the input/label control. The actual label that will be printed for the barcode will look like this. The Other ones were similar but only using standard VB controls being displayed.


    Curtis

    Friday, November 2, 2018 5:59 PM
  • Curtis,

    Oh yeah. Cool. Thanks for showing.

    " VB powerpacks"

    Argg!


    Hmmm, well I think you need to learn to go beyond a file image in a form and learn to work with a bitmap in memory and even real vector gdi+ drawing. So instead of making a form with a file image you draw a new memory bitmap and instead of printing a form from the screen you print the bitmap directly using vector graphics. That will give you a much higher quality printout I can see it in the image you show.

    In fact, how do you draw that barcode? That is the saved file correct? Where did that image come from?

    ANd the rest serial number prod num etc are those labels on the form or are those in the original?

    So you should draw that as and original on the printer and skip the rest of it.

    Well it will still be better to just print that image direct to the printer and it will still give better quality depending on the original file image size and the print res ie 600 dpi. Those dots are pixels. The monitor is normally 96 dpi.

    Any who, I will stop now. But thats my initial thoughts on what you should do. If you are interested you should post a new question and get everyone involved. Show what you have and that you want to improve and etc. That will get everyone involved.

    So just depends on how much you want to put into it. And just small steps at a time.

    There is lots of interest on the forum about all of it.

    ps this will replace powerpack printform.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/78830e15-74ce-4927-b02e-d5cca0eded35/printing-entire-windows-form?forum=vbgeneral#6bc8a3da-a7e1-4084-82d5-f9bca676bda3

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea7e92c-7a61-4c65-bacb-2ef66e006717/print-a-form-but-in-lansscape?forum=vbgeneral

    Friday, November 2, 2018 6:32 PM
  • Tommy this is interesting and Thanks for all the info. Everything on the form is taken from variables. All the barcodes are drawn with data from variables then displayed in a panel control. The serial number is indexed after each print routine and a new barcode built then displayed in a panel for it. The lot number is just a label derived from the date.now as is the large JAN month. I have a variable setup with all 12 month abbreviations. I use string control to break down the date into variables then use those to build what I need. You will notice even if a 24 hr shift is run the date will change at midnight because this is updated on each print cycle.

    Sub Getdate()
    		MonthArray(1) = "JAN"
    		MonthArray(2) = "FEB"
    		MonthArray(3) = "MAR"
    		MonthArray(4) = "APR"
    		MonthArray(5) = "MAY"
    		MonthArray(6) = "JUN"
    		MonthArray(7) = "JUL"
    		MonthArray(8) = "AUG"
    		MonthArray(9) = "SEP"
    		MonthArray(10) = "OCT"
    		MonthArray(11) = "NOV"
    		MonthArray(12) = "DEC"
    		Dim Month As String
    		Dim Datearray() As String = Split(Date.Now, " ", , CompareMethod.Text)
    		Ldate.Text = Datearray(1) + " " + Datearray(2)
    		Dim lotauto As String
    		My.Forms.LABELSELECT.Visible = False
    		Dim lotin As String
    		Dim lotin1 As String
    		lotin = Date.Now
    		Dim TestString As String = lotin
    		Dim TestArray() As String = Split(TestString)
    		' TestArray holds {"#8/9/2008 2:08:07 pm#") get the date out, make date code "080908"
    		lotin1 = TestArray(0)
    		TestString = lotin1
    		Ldate.Text = TestString
    		Dim Test2Array() = Split(TestString, "/", , CompareMethod.Text)
    		REM add a 0 in front of the day or month sting to make it 01-09 and add day and month together
    		If Test2Array(0).Length < 2 Then
    			lotauto = "0" + Test2Array(0)
    			Month = Test2Array(0)
    			MONTHINT = Convert.ToInt16(Month)
    			Setmonth()
    		Else
    			Month = Test2Array(0)
    			MONTHINT = Convert.ToInt16(Month)
    			lotauto = Test2Array(0)
    			Setmonth()
    		End If
    		If Test2Array(1).Length < 2 Then
    			lotauto = lotauto + "0" + Test2Array(1)
    		Else
    			lotauto = lotauto + Test2Array(1)
    		End If
    		TestString = Test2Array(2)
    		' Dim Test3Array() = Split(TestString, "0", , CompareMethod.Text)
    		Dim Test3Array(2) As String
    		Test3Array(0) = TestString.Substring(0, 2)
    		Test3Array(1) = TestString.Substring(2, 2)
    		If Test3Array(1).Length < 2 Then
    			lotauto = lotauto + "0" + Test3Array(1)
    		Else
    			lotauto = lotauto + Test3Array(1)
    		End If
    		PLNL.Text = lotauto
    	End Sub


    Curtis

    Friday, November 2, 2018 9:03 PM
  • "All the barcodes are drawn with data from variables then displayed in a panel control...and a new barcode built then displayed in a panel"

    So that image you show is drawn with gdi+ you mean? Even the bar codes?

    In the code you show:

         Ldate.Text = Datearray(1) + " " + Datearray(2)

    that puts the text for a date in a label on the form, correct?

    Thats not what I mean by "drawing".

    What I mean is gdi+ drawing which is original vector drawing on the screen or bitmap. The code is like:

         e.graphics.DrawString(t, New Font("tahoma", 12), Brushes.Black, l.p2.X, l.p2.Y)

    or DrawLine etc.

    where e.graphics is the drawing surface for a control like a picturebox or label given in the paint event.

    So I am talking about drawing on the printer graphics surface directly. All original gdi+ vector graphics. No in-between bitmaps etc.

    How do the multiple vertical lines in your bar code get drawn? Can you show code for that if not too long or the basic thing that makes the lines in the bar code.

    I want to show you an example but I am trying to determine what you are doing now exactly.

    If that sounds like an improvement you would like to get into that is.

    Friday, November 2, 2018 11:45 PM
  • Tommy I do not draw the actual barcode because it has to be a type 39 or some refer to them as 3 of 9 Standard for the barcode readers. I use a Barcode SDK that generates the barcode with the data I need in it. Then I send it to the panel background image that is positioned on the form. Below is the working code for adding the 6 barcodes I have to have in the label.

    Sub Build_Barcode(ByVal Barcodename() As String)
    
    
    		Dim Weightin As String = ""
    		Getdate()
    		REM getserialdata()
    		If My.Forms.LABELSELECT.ComportSelect = "LABEL PRINT NO WEIGHT" Then
    		Else
    			Weightin = Getserialdata()
    		End If
    		If Weightin = "No Comport" Or Weightin = "Cancel" Or Weightin = "OK" Then
    			Exit Sub
    		End If
    		' setup form for print
    		If Weightin = "" Then
    			Weightin = "0.0 lb"
    		Else
    			Weightin = Weightin + Weightunitid()
    		End If
    
    		Barcodename(5) = Weightin
    		Barcodename(3) = Label_Setup.serialnumber
    		'PanelPartno.BackgroundImage.Dispose()
    		If PanelPartno.BackgroundImage IsNot Nothing Then PanelPartno.BackgroundImage.Dispose()
    		If PanelPO.BackgroundImage IsNot Nothing Then PanelPO.BackgroundImage.Dispose()
    		If PanelDes.BackgroundImage IsNot Nothing Then PanelDes.BackgroundImage.Dispose()
    		If PanelSN.BackgroundImage IsNot Nothing Then PanelSN.BackgroundImage.Dispose()
    		If PanelVN.BackgroundImage IsNot Nothing Then PanelVN.BackgroundImage.Dispose()
    		If PanelVD.BackgroundImage IsNot Nothing Then PanelVD.BackgroundImage.Dispose()
    
    		For x = 0 To 5
    			Using barcode As New Barcode With {.Symbology = SymbologyType.Code39, .CaptionPosition = 1}
    				barcodename1 = ("barcode" + Str(x) + ".png")
    				barcode.Value = Label_Setup.Barcodename(x)
    				barcode.CaptionPosition = CaptionPosition.Above
    				barcode.CaptionFont = New Font("Microsoft Sans Serif", 16)
    				barcode.SaveImage("c:\Labels\Barcode\barcode" & x & ".png")
    
    			End Using
    		Next x
    		
    
    		PanelPartno.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode0.png") ' File not found
    		PanelPO.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode1.png")
    		PanelDes.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode2.png")
    		PanelSN.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode3.png")
    		PanelVN.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode4.png")
    		PanelVD.BackgroundImage = Image.FromFile("C:\Labels\Barcode\barcode5.png")
    
    
    		Label_Setup.snnumber = Label_Setup.snnumber + 1
    		SNstring = "" & Label_Setup.snnumber
    		SNlen = SNstring.Length
    		SNstring = ""
    		For x = SNlen To 8
    			SNstring = SNstring + "0"
    		Next
    		SNstring = SNstring + "" & Label_Setup.snnumber & ""
    
    		Label_Setup.serialnumber = SNstring
    	End Sub



    Curtis



    • Edited by Curtis UN Friday, November 2, 2018 11:57 PM
    Friday, November 2, 2018 11:53 PM
  • The using in the for X = 0 to 5 is the actual barcode build and save. Each barcode image is built and saved in this loop. The only things that change in the loop is the file name, data that is put into the barcode.

    Curtis

    Saturday, November 3, 2018 12:00 AM
  • The using in the for X = 0 to 5 is the actual barcode build and save. Each barcode image is built and saved in this loop. The only things that change in the loop is the file name, data that is put into the barcode.

    Curtis

    Well I just made a generic example that has one routine RenderScene that draws the barcode on a picturebox and or printer. It has a sccalefactor that sizes the scene. So the printout is actually larger for the high print res.

    I am still drawing the barcode on a bitmap first but the whole scene is sized for the surface and the printer scene is larger for the highres of printer. This bitmap could even be eliminated but I keep to use the barcode class as is - sort of.

    Its an example for ideas. One has to adapt it for ones use.

    To make the example add a picturebox and a button to a form copy and paste both of these class sections into the form.

    Click the Print button on the Preview Window to print on the default printer.

    'print and show barcode one routine
    Public Class Form1
        Private DrawBar As New code39
        Private preview As New PrintPreviewDialog
        Private pd As New System.Drawing.Printing.PrintDocument
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(440, 300)
            PictureBox1.ClientSize = New Size(400, 200)
            PictureBox1.Location = New Point(20, 20)
    
            AddHandler pd.PrintPage, AddressOf OnPrintPage
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'show barcode in print preview
            pd.DefaultPageSettings.Landscape = True
            preview.Document = pd
            preview.ShowDialog()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            'draw the code on the picturebox
            With e.Graphics
                .Clear(Color.White)
                RenderScene(e.Graphics, 3)
            End With
        End Sub
    
        Private Sub RenderScene(g As Graphics, sizeFactor As Double)
            'draw the barcode on a graphics surface g
            'draw the bar code on a bitmap
            Dim barcode1 As Bitmap
            barcode1 = DrawBar.Generate("012345678901", sizeFactor)
    
            With g
                'draw some text
                .DrawString("tommytwotrain", New Font("tahoma", CSng(sizeFactor * 8), FontStyle.Bold), Brushes.Black, 0, 0)
                'draw the barcode bitmap
                .DrawImage(barcode1, CInt(5 * sizeFactor), CInt(12 * sizeFactor))
            End With
    
            barcode1.Dispose()
        End Sub
    
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            'draw the barcode scene on the printer
            RenderScene(e.Graphics, 6)
    
        End Sub
    
    End Class
    
    'from https://stackoverflow.com/questions/149379/how-to-generate-code39-barcodes-in-vb-net
    'modified   'ttt 11-2-18
    
    Public Class code39
        Private bitsCode As ArrayList
    
        Public Sub New()
            bitsCode = New ArrayList
            bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
            bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
            bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
            bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
            bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
            bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
            bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
            bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
            bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
            bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
        End Sub
    
        Public Function Generate(ByVal Code As String, ByVal sizeFactor As Double) As Image
            Dim a As Integer = 0
            Dim b As Integer = 0
            Dim imgCode As Image
            Dim g As Graphics
            Dim i As Integer
            Dim bCode As Byte()
            Dim bitCode As Byte()
            Dim tmpFont As Font
    
            If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")
    
            ReDim bCode(12)
            For i = 0 To 11
                bCode(i) = CInt(Code.Substring(i, 1))
                If (i Mod 2) = 1 Then
                    b += bCode(i)
                Else
                    a += bCode(i)
                End If
            Next
    
            i = (a + (b * 3)) Mod 10
            If i = 0 Then
                bCode(12) = 0
            Else
                bCode(12) = 10 - i
            End If
            bitCode = getBits(bCode)
    
            tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)
    
            'ttt 11-2-18
            'imgCode = New Bitmap(110, 50)
            imgCode = New Bitmap(CInt(sizeFactor * 110), CInt(sizeFactor * 50))
    
            g = Graphics.FromImage(imgCode)
            g.Clear(Color.White)
            'ttt 11-2-18
            g.ScaleTransform(sizeFactor, sizeFactor)
            g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)
    
            a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width
    
            For i = 0 To bitCode.Length - 1
                If i = 2 Then
                    g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
                ElseIf i = 48 Then
                    g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
                End If
    
                If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
                    If bitCode(i) = 1 Then 'noir
                        g.DrawLine(Pens.Black, a, 0, a, 40)
                        a += 1
                    End If
                Else
                    If bitCode(i) = 1 Then 'noir
                        g.DrawLine(Pens.Black, a, 0, a, 30)
                        a += 1
                    Else 'blanc
                        a += 1
                    End If
                End If
            Next
            g.Flush()
            Return imgCode
        End Function
    
        Private Function getBits(ByVal bCode As Byte()) As Byte()
            Dim i As Integer
            Dim res As Byte()
            Dim bits As String = "101"
            Dim cle As String = bitsCode(bCode(0))(3)
            For i = 1 To 6
                bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
            Next
            bits &= "01010"
            For i = 7 To 12
                bits &= bitsCode(bCode(i))(2)
            Next
            bits += "101"
            ReDim res(bits.Length - 1)
            For i = 0 To bits.Length - 1
                res(i) = Asc(bits.Chars(i)) - 48
            Next
            Return res
        End Function
    End Class

    PS this code has been optimized and re-posted in this discussion.


    • Marked as answer by Curtis UN Saturday, November 3, 2018 4:09 AM
    • Edited by tommytwotrain Saturday, November 3, 2018 10:06 AM
    Saturday, November 3, 2018 1:36 AM
  • This is very good. I wish I had a Microsoft license so I could use their barcode but I am retired and funds are not great. I will copy this code and study it until I have an understanding of how it works. I might use something similar in an update later but the company I am doing this for is being cheap again lol they don't like the idea of me having to pay for the full sdk I am using(about 40$) lol.

    Thanks for your help and all the information you have given me. It will not go to waste.


    Curtis

    Saturday, November 3, 2018 3:06 AM
  • WOW, This is completely new to me not the graphics I can understand part of that because I used some graphics in an application I fooled around with but the bitcode for the barcodes, In all the searches I did back in 2008 I found only sdk and at the time they did not need barcodes so because I did not have the time to study out using them I just used the forms and standard controls.. Thank you for this example and for the link to https://stackoverflow.com/questions/149379/how-to-generate-code39-barcodes-in-vb-net. This has given me enough to study for a month or so lol. Looking forward to further discussions with you. I can Honestly say I have learned more today than I did in the past month. So this day is not wasted.

    Curtis

    Saturday, November 3, 2018 4:09 AM
  • Curtis,

    I am not sure there is anything in the code I show you cant use due to copyright?

    The barcode it generates looks a lot different that what you showed? You know more than I about it.

    You can prob use what you have now for that part? Just use the basic drawing structure I have shown. Both drawing with graphics and drawing on bitmaps.

    Glad it helps.

    :)

    PS yes the forum is very helpful. That is basically where I learned it. I am self taught civil engr programmer.

    I optimized the code slightly and reposted v2 with its own subject thread here.

    Saturday, November 3, 2018 8:41 AM
  • Tommy,

     I need to study this extensively before I even try to use it. As to the barcode it would not be usable as it stands, I would have to change it for this to work for an AIAG (American Industry Automotive Group) Standard (As shown in the Picture I posted, The human readable portion has to be on top). The company I am building this for(that I retired from) is a 3rd tier supplier to the automotive industry.  So there would have to graphics built up for not only the barcode but for the descriptions, other items used in the label , and also the lines drawn to separate the varying barcodes. Don't get me wrong I will use what I learn from this. The possibility is there that I will build a fully functional label maker with all the bells and whistles or maybe some other application someone needs.

    Thanks for the help


    Curtis

    Saturday, November 3, 2018 2:36 PM