PDA

View Full Version : [SOLVED:] manipulating bmp files



befuddled
08-23-2013, 08:43 AM
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.

p45cal
08-24-2013, 03:49 AM
What code have you got so far?

befuddled
08-24-2013, 07:56 AM
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.

snb
08-24-2013, 08:59 AM
What's in range("A11: ....")

p45cal
08-24-2013, 09:19 AM
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{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“

befuddled
08-25-2013, 06:16 AM
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??

befuddled
08-25-2013, 06:21 AM
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.

befuddled
08-25-2013, 06:55 AM
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?

p45cal
08-25-2013, 08:53 AM
looking at the file in a Hex editor I get:
10500
I've highlighted 92 (Hex for 146).

befuddled
08-25-2013, 09:53 AM
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.

p45cal
08-25-2013, 10:41 AM
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#

befuddled
08-25-2013, 01:21 PM
:clap: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.