Today in zgke (http://blog.csdn.net/zgke/archive/2008/12/11/3496721.aspx) saw the C # version of the drawing ean13 (commodity barcode) code, and then write a VB. NET version of their own. below is the interface and code:
In the textbox, enter 13 as the barcode, for example, 6901028036955, and click the generate button to draw the commodity barcode.
Private sub button#click (byval sender as system. object, byval e as system. eventargs) handles button1.click <br/> if me. textbox1.text. trim. length <> 13 then <br/> msgbox ("the bar code must be 13 characters! ", Msgboxstyle. exclamation, "system prompt") <br/> me. textbox1.focus () <br/> exit sub <br/> end if <br/> try <br/> dim CS as new cisbn <br/> If CS. ean13isbn (Me. textbox1.text. trim) = false then <br/> msgbox ("Incorrect bar code input. Please enter it again! ", Msgboxstyle. exclamation, "system prompt") <br/> me. textbox1.focus () <br/> exit sub <br/> end if <br/> CS. ean13 () <br/> CS. magnify = 1 <br/> CS. heigth = 100 <br/> CS. fontsize = 16 <br/> me. picturebox1.image = cs. getcodeimage (Me. textbox1.text. trim) <br/> me. picturebox1.image. save ("C:/Documents and Settings/Administrator/desktop/1.jpg") <br/> me. textbox1.text = "" <br/> me. textbox1.focus () <br/> catch ex as exce Ption <br/> msgbox ("failed to draw the barcode! ", Msgboxstyle. Exclamation," system prompt ") <br/> end try <br/> end sub
The following is the class for drawing the product barcode:
Imports system. drawing <br/> public class cisbn <br/> private m_ean13 as new datatable <br/> Public sub ean13 () <br/> m_ean13.columns.add ("ID ") <br/> m_ean13.columns.add ("type") <br/> m_ean13.columns.add ("A") <br/> m_ean13.columns.add ("B") <br/> m_ean13.columns.add ("C ") </P> <p> m_ean13.rows.add ("0", "aaaaaa", "0001101", "0100111", "1110010") <br/> m_ean13.rows.add ("1 ", "aababb", "0011001", "0110011 ", "1100110") <br/> m_ean13.rows.add ("2", "aabbab", "0010011", "0011011", "1101100") <br/> m_ean13.rows.add ("3 ", "aabbba", "0111101", "0100001", "1000010") <br/> m_ean13.rows.add ("4", "abaabb", "0100011", "0011101 ", "1011100") <br/> m_ean13.rows.add ("5", "abbaab", "0110001", "0111001", "1001110") <br/> m_ean13.rows.add ("6 ", "abbbaa", "0101111", "0000101", "1010000") <br/> m_ean13.rows.add ("7", "Abab AB "," 0111011 "," 0010001 "," 1000100 ") <br/> m_ean13.rows.add (" 8 "," ababba "," 0110111 "," 0001001 ", "1001000") <br/> m_ean13.rows.add ("9", "Abbaba", "0001011", "0010111", "1110100 ") </P> <p> end sub <br/> private m_height as uint32 <br/> ''' <summary> <br/> ''' draw height <br/> ''' </Summary> <br/> ''' <value> </value> <br/> ''' <returns> </returns> <br/>' ''<remarks> </remarks> <br/> property heigth () as uint 32 </P> <p> Get <br/> return m_height <br/> end get <br/> set (byval value as uint32) <br/> m_height = value <br/> end set <br/> end property <br/> private m_fontsize as integer = 0 <br/> ''' <summary> <br/> ''' font size () <br/> ''' </Summary> <br/> ''' <value> </value> <br/> ''' <returns> </returns> <br/> ''' <remarks> </remarks> <br/> property fontsize () as integer <br/> Get <br/> return m_fontsize <br/> End get <br/> set (byval value as integer) <br/> m_fontsize = value <br/> end set <br/> end property <br/> private m_magnify as byte = 0 <br/> ''' <summary> <br/> ''' amplification factor <br/> ''' </Summary> <br/> ''' <value> </value> <br/> ''' <returns> </returns> <br/> ''' <remarks> </remarks> <br/> property magnify () as byte <br/> Get <br/> return m_magnify <br/> end get <br/> set (byval value as byte) <br/> m_m Agnify = value <br/> end set <br/> end property <br/> Public Function getcodeimage (byval p_text as string) as bitmap <br/> If p_text.length <> 13 then <br/> throw new exception ("the number is not 13 characters! ") <Br/> end if <br/> dim _ codetext as string = p_text.remove (0, 1) <br/> dim _ codeindex = "101" <br/> dim _ lefttype () as char = getvalue (p_text.substring (0, 1), "type "). tochararray <br/> for I as integer = 0 to 5 <br/> _ codeindex + = getvalue (_ codetext. substring (0, 1), _ lefttype (I ). tostring (). trim) <br/> _ codetext = _ codetext. remove (0, 1) <br/> next <br/> _ codeindex + = "01010" <br/> for I as intege R = 0 to 5 <br/> _ codeindex + = getvalue (_ codetext. substring (0, 1), "C") <br/> _ codetext = _ codetext. remove (0, 1) <br/> next <br/> _ codeindex + = "101" <br/> return getimage (_ codeindex, p_text) <br/> end function <br/> ''' <summary> <br/> ''': obtains the data of the target. <br/> ''' </Summary> <br/> ''' <Param name = "p_value"> encoding </param> <br/> ''' <Param name = "p_type"> type </param> <br/> ''' <returns> encoding </returns> <br/> ''' <Remarks> </remarks> <br/> private function getvalue (byval p_value as string, byval p_type as string) as string <br/> If m_ean13 is nothing then return "" <br/> dim _ row () as datarow = m_ean13.select ("ID = '" + p_value + "'") <br/> If _ row. length <> 1 then throw new exception ("wrong encoding" + p_value.tostring (). trim) <br/> return _ row (0) (p_type ). tostring. trim <br/> end function <br/> private function getimag E (byval p_text as string, byval p_viewtext as string) as bitmap <br/> dim _ value () as char = p_text.tochararray <br/> dim _ fontwidth as integer = 0 <br/> dim _ myfont as font = nothing <br/> If m_fontsize <> 0 then <br/> _ myfont = new font (" ", m_fontsize) <br/> dim _ myfontbmp as bitmap = new Bitmap (m_fontsize, m_fontsize) <br/> dim _ fontgraphics as graphics = graphics. fromimage (_ myfontbmp) <br/> For I as integer = m_fontsize to 1 step-1 <br/> dim _ drawsize as sizef = _ fontgraphics. measurestring (p_viewtext.substring (0, 1), _ myfont) <br/> If _ drawsize. height> m_fontsize then <br/> _ myfont = new font ("", I) <br/> else <br/> _ fontwidth = convert. toint32 (_ drawsize. width) <br/> exit for <br/> end if <br/> next <br/> end if <br/> If scandrawtext (_ myfont, p_text, _ fontwidth) = false then <br/> _ Fontwidth = 0 <br/> m_fontsize = 0 <br/> end if <br/> 'width = number of projects to be drawn * magnification + width of two words <br/> dim _ codeimage as bitmap = new Bitmap (_ value. length * (convert. toint32 (m_magnify) + 1) + (_ fontwidth * 2), convert. toint32 (m_height) <br/> dim _ garphics as graphics = graphics. fromimage (_ codeimage) <br/> _ garphics. fillrectangle (brushes. white, new rectangle (0, 0, _ codeimage. width, _ codeimage. height) <br/> dim _ Height as integer = 0 <br/> dim _ lenex as integer = _ fontwidth <br/> for I as integer = 0 to _ value. length-1 <br/> dim _ drawwidth as integer = m_magnify + 1 <br/> If I = 0 or I = 2 or I = 46 or I = 48 or I = 92 or I = 94 then <br/> _ Height = convert. toint32 (m_height) <br/> else <br/> _ Height = convert. toint32 (m_height)-m_fontsize <br/> end if <br/> If _ value (I) = "1" then <br/> _ garphics. fil Lrectangle (brushes. black, new rectangle (_ lenex, 0, _ drawwidth, _ height) <br/> else <br/> _ garphics. fillrectangle (brushes. white, new rectangle (_ lenex, 0, _ drawwidth, _ height )) <br/> end if <br/> _ lenex + = _ drawwidth <br/> next <br/> 'draw text <br/> If _ fontwidth <> 0 and m_fontsize <> 0 then <br/> dim _ starx as integer = 0 <br/> dim _ Stary as integer = convert. toint32 (m_height)-_ myfont. height <br/> _ Garphics. drawstring (p_viewtext.substring (0, 1), _ myfont, brushes. blue, 0, _ Stary) <br/> _ starx = _ fontwidth + (3 * (m_magnify + 1) <br/> _ garphics. drawstring (p_viewtext.substring (1, 6), _ myfont, brushes. blue, _ starx, _ Stary) <br/> _ starx = _ fontwidth + (50 * (m_magnify + 1) <br/> _ garphics. drawstring (p_viewtext.substring (7, 6), _ myfont, brushes. blue, _ starx, _ Stary) <br/> end if <br/> _ garphics. Dispose () <br/> return _ codeimage <br/> end function <br/> ''' <summary> <br/> ''' determines whether the font is larger than the drawing. <br/> ''' </Summary> <br/> ''' <Param name = "_ myfont"> font </param> <br/> ''' <Param name =" p_text "> text </param> <br/> ''' <Param name =" p_width "> font width </param> <br/> ''' <returns> true can be drawn. False cannot be drawn. </returns> <br/> ''' <remarks> </remarks> <br/> private function scandrawtext (byval _ myfont as font, byval p_text S string, byval p_width as integer) as Boolean <br/> If _ myfont is nothing then return false <br/> dim _ width as integer = (p_text.length-6-5) * (convert. toint32 (m_magnify) + 1) <br/> If (p_width * 12)> _ width) then return false <br/> return true <br/> end function <br/> 'algorithm <br/> 'code position 13 12 11 10 9 8 7 6 5 4 3 2 1 <br/> 'Code 2 3 4 2 3 5 6 5 4 6 5 2? <Br/> 'from the code position 2, the sum of the numeric codes of all even digits is. <Br/> 'multiply A in the preceding step by 3 to. <Br/> 'from code position 3, the sum of all odd digits is B. <Br/> 'Add A and B to C. <Br/> 'use the single-digit D of C. <Br/> '10 minus D is the value of the check bit. <Br/> ''' <summary> <br/> ''' verify that the last digit of the barcode is correct. <br/> ''' </Summary> <br/> ''' <Param name = "_ numb"> Bar Code </param> <br/> ''' <returns> true false error </returns> <br/>' ''<remarks> </remarks> <br/> ''' <br/> Public Function ean13isbn (byval _ numb as string) as Boolean <br/> dim _ sum as integer = 0 <br/> dim OS as integer = 0 <br/> dim JS as integer = 0 <br/> dim jym integer = 0 <br/> for I as integer = 11 to 0 step-2 <br/> OS + = _ numb. substring (I, 1) <br/> next <br/> for J as integer = 10 to 0 step-2 <br/> JS + = _ numb. substring (J, 1) <br/> next <br/> OS = OS * 3 <br/> _ sum = OS + JS <br/> jym = 10-(_ sum. tostring. trim. substring (_ sum. tostring. trim. length-1, 1) <br/> If jym = _ numb. trim. substring (_ numb. trim. length-1, 1) then <br/> return true <br/> else <br/> return false <br/> end if <br/> end function </P> <p> end class