Consulting

Results 1 to 12 of 12

Thread: manipulating bmp files

  1. #1

    manipulating bmp files

    I'm trying to edit bitmap files using vba (through excel) but i see that many values won't write properly when i use put# to write binary data e.g. 128, 130 etc..upto 159. But 146 does appear often in bmp files but I can't write it to a file using put#. Any ideas gratefully received. B.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    What code have you got so far?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3

    code for writing data to a file

    here's the code to write - the spreadsheet just has values 0 to 255.

    Sub writesomething()
    Dim counter As Integer
    Dim Data_1 As Byte
    Dim data(1024000) As Byte
    Dim imagex, imagey As Long
    fileToOpen = Application _
        .GetOpenFilename("jas Files (*.jas), *.jas")
    'If fileToOpen <> False Then
    'End If
    
    
    Open fileToOpen For Binary As #1
    Sheets("write it").Range("b2").Value = fileToOpen
    imagex = Sheets("write it").Range("b4").Value
    imagey = Sheets("write it").Range("b3").Value
    For x = 0 To (imagex - 1)
    For y = 0 To (imagey - 1)
    Data_1 = Sheets("write it").Range("a11").Offset(y, x).Value
    Put #1, , Data_1
    Next y
    Next x
    Close #1
    End Sub
    The value 146 appears in many types of windows file, but I can't seem to write it to a binary file?

    Hope you can help, B.
    Last edited by Aussiebear; 08-24-2013 at 06:38 PM. Reason: Enclose code with correct tags

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    What's in range("A11: ....")

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I had no problem writing 146 to a .jas file; I stopped at 147 and this is what I got (it's the penultimate character in red):


    !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Hmmmm, did you write the data as binary, not Ascii? Values 128, 130 etc never seem to appear in Windows files of any kind.. exe, bmp, mp 3, wav, pdf etc. Or maybe it's just an xp thing??

  7. #7
    B3 is 1, B4 is 256, A11 to A266 are the values 0 to 255. Most values write fine, but some seem to be restricted. Seems to be a very inefficient way, limiting the values that can be written to file? B.

  8. #8
    Hi p45cal,

    when you read back the file, haven't the values changed as follows:

    146 becomes 25
    128 becomes 172
    130 becomes 26
    131 becomes 146 (which means i can actually write 146 in a round-about way)132 becomes 30
    etc???

    I'm trying to write bmp files but its not just the data and colour palette that won't take the value 146 - its even the bitmap size. If i resize a bmp file to 146 x 146 pixels, the file header says its 25 x 25? But windows still works it out correctly as 146 x 146. Clever windows, voodoo magic or i am just being thick?

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    looking at the file in a Hex editor I get:
    2013-08-25_165011.jpg
    I've highlighted 92 (Hex for 146).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    Mmmm, i'm still puzzled but it must be something to do with the version of excel (2000) / vba (6.3) i'm using. Though i still don't see the value 128 ever appearing in a file either. Maybe it's because i'm reading files with input# - could that be showing me a false reading? I think i need a better hex-reader too as the one i have only reads values upto 7F.

  11. #11
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by befuddled View Post
    Maybe it's because i'm reading files with input# - could that be showing me a false reading?
    Perhaps use Get# if you've used Put#
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  12. #12

    solved

    Eureka! The problem wasn't the put#, it was the input# that i was using to read the files - seems so obvious. I was using AscB to force the input to byte data type and i guess here lies the problem 'Data_1 = AscB(Input(1, #1)) .

    I should have guessed that all values can be used in data files otherwise massive inefficiency.

    Thanks v much p45cal for your persistence! B.

Tags for this Thread

Posting Permissions

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