Results 1 to 6 of 6

Thread: Resize Picture

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Resize Picture

    Hello, I have a program that makes a barcode on a picturebox that is on my form, then saves it to a file. Below is the code:

    VB Code:
    1. Private Sub CreateBarcode()
    2. Dim X As Integer, Y As Integer, z As Integer, pos As Integer
    3. Dim Barcode As String
    4. Dim Cur As String
    5. Dim CurVal As Integer
    6. Dim BC(43) As String
    7.  
    8.     BC(0) = "000110100" '0
    9.     BC(1) = "100100001" '1
    10.     BC(2) = "001100001" '2
    11.     BC(3) = "101100000" '3
    12.     BC(4) = "000110001" '4
    13.     BC(5) = "100110000" '5
    14.     BC(6) = "001110000" '6
    15.     BC(7) = "000100101" '7
    16.     BC(8) = "100100100" '8
    17.     BC(9) = "001100100" '9
    18.     BC(10) = "100001001" 'A
    19.     BC(11) = "001001001" 'B
    20.     BC(12) = "101001000" 'C
    21.     BC(13) = "000011001" 'D
    22.     BC(14) = "100011000" 'E
    23.     BC(15) = "001011000" 'F
    24.     BC(16) = "000001101"
    25.     BC(17) = "100001100"
    26.     BC(18) = "001001100"
    27.     BC(19) = "000011100"
    28.     BC(20) = "100000011"
    29.     BC(21) = "001000011"
    30.     BC(22) = "101000010"
    31.     BC(23) = "000010011"
    32.     BC(24) = "100010010"
    33.     BC(25) = "001010010"
    34.     BC(26) = "000000111"
    35.     BC(27) = "100000110"
    36.     BC(28) = "001000110"
    37.     BC(29) = "000010110"
    38.     BC(30) = "110000001"
    39.     BC(31) = "011000001"
    40.     BC(32) = "111000000" 'W
    41.     BC(33) = "010010001" 'X
    42.     BC(34) = "110010000" 'Y
    43.     BC(35) = "011010000" 'Z
    44.     BC(36) = "010000101" '-
    45.     BC(37) = "110000100" '.
    46.     BC(38) = "011000100" '<spc>
    47.     BC(39) = "010101000" '$
    48.     BC(40) = "010100010" '/
    49.     BC(41) = "010001010" '+
    50.     BC(42) = "000101010" '%
    51.     BC(43) = "010010100" '*  (used for start/stop character only)
    52.    
    53. picBarCode.Cls
    54. pos = 10
    55. Barcode = UCase(FixedItemCode)   'fixeditem code = the charactes in a textbox that is entered and converted to a barcode.
    56.  
    57. 'Add Start & Stop characters
    58. Barcode = "*" & Barcode & "*"
    59.  
    60. 'Generate Barcode
    61. For X = 1 To Len(Barcode)
    62.     Cur = Mid$(Barcode, X, 1)
    63.     Select Case Cur
    64.     Case "0" To "9"
    65.         CurVal = Val(Cur)
    66.     Case "A" To "Z"
    67.         CurVal = Asc(Cur) - 55
    68.     Case "-"
    69.         CurVal = 36
    70.     Case "."
    71.         CurVal = 37
    72.     Case " "
    73.         CurVal = 38
    74.     Case "$"
    75.         CurVal = 39
    76.     Case "/"
    77.         CurVal = 40
    78.     Case "+"
    79.         CurVal = 41
    80.     Case "%"
    81.         CurVal = 42
    82.     Case "*"
    83.         CurVal = 43
    84.     Case Else 'oops!
    85.         picBarCode.Cls
    86.         picBarCode.Print Cur & " is Invalid"
    87.         Exit Sub
    88.     End Select
    89.    
    90.     For Y = 1 To 9
    91.         If Y / 2 = Int(Y / 2) Then
    92.             'SPACE
    93.             pos = pos + 1 + (3 * Val(Mid$(BC(CurVal), Y, 1)))
    94.         Else
    95.             'BAR
    96.             For z = 1 To 1 + (3 * Val(Mid$(BC(CurVal), Y, 1)))
    97.                 picBarCode.Line (pos, 1)-(pos, 50)
    98.                 pos = pos + 1
    99.             Next z
    100.         End If
    101.     Next Y
    102.     pos = pos + 1 'make inter-character gap (ie: 1 narrow space)
    103. Next X
    104.  
    105.     picBarCode.CurrentX = Len(Barcode) * 7 'kinda center
    106.     picBarCode.Print Barcode
    107.  
    108. SavePicture picBarCode.Image, BarcodePicture

    My question is, right now I have a picturebox that is 8835 twips wide...is there a way that I can have it automatically resize to the size of whatever the barcode is? Say the picturebox starts out at 8835 twips...when a barcode is generated, and the barcode itself only takes up 1000 twips...it resizes the picturebox and centers the barcode.
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Resize Picture

    Any ideas?
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Resize Picture

    Easiest that comes to my mind is that you resize the picturebox to the required size, then draw the new barcode and then have the picturebox inside another picturebox and center it.

    As a tip, you could set ScaleMode to vbPixels to be sure you don't draw to certain position multiple times. And then the code to center something inside something:

    VB Code:
    1. Picture2.Move (Picture1.ScaleWidth - Picture2.Width) \ 2, (Picture1.ScaleHeight - Picture2.Height) \ 2

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Resize Picture

    I don't believe that would work, in theory I could have a barcode that took up 1 inch...and I could have a barcode that took up 6 inches.
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Resize Picture

    It is all about calculation:

    1. you know how big your barcode needs to be (= how many items)
    2. thus you know how to calculate how many pixels you need (you could pretend to draw it, but instead you just calculate the space you need; same code, you just don't draw)
    3. you resize the picturebox to your needs
    4. you center the picturebox inside the other picturebox
    5. you draw the barcode

    I can't see a problem.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Resize Picture

    I found for my needs, this comes pretty close:

    VB Code:
    1. picBarCode.Width = picBarCode.Width + (Len(Barcode) * 275)
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width