Set fso=CreateObject("scripting.FileSystemObject")
set reader=fso.opentextfile("old.bmp")
set writer=fso.createtextfile("new.bmp")
buffer=reader.readAll
writer.write(buffer)
I just end up with a new file full of zeroes. Is there any way to read/write
binary files using VBScript?
--
Dave "Crash" Dummy - A weapon of mass destruction
cr...@gpick.com
http://lists.gpick.com
Just as a starting point.
You may want adodb.stream. For text file, I've tried something like this
with successful result. Unbelievably slow. See if this has any meaning for
you.
set strm1=createobject("adodb.stream")
With strm1
.Type = 1
.Open
.LoadFromFile "c:\x.bmp"
End With
bd=strm1.read
lb= lenb(bd)
msgbox lb
for i=1 to lb
bd2=bd2 & "x0" & hex(ascB(midB(bd, i)))
next
msgbox bd2
""Crash" Dummy" <dva...@deathstar.mil> wrote in message
news:#DpCXftM...@tk2msftngp13.phx.gbl...
That's a start. Where can I get documentation on adodb, like methods, how to
write the data to a new file, etc.
Thanks!
The only Problem IMHO are zero-characters in the bin-file.
the ReadAll-Methode treates them as string-terminaters
and fills up the rest with zeros.
If you read charcter by character you won't have
this prob, but you loose speed.
this works for me to make an identical copy:
Set oFS = CreateObject("Scripting.FileSystemObject")
Set reader=oFs.opentextfile("old.bmp")
set writer=oFs.createtextfile("new.bmp")
While reader.AtEndOfStream <>true
writer.write reader.read(1)
Wend
Set oWS = CreateObject("WScript.Shell")
oWS.Run "new.bmp"
--
Gruss
Christoph
http://msdn.microsoft.com/library/en-us/ado270/htm/adostartpage1.asp
Bob
Dave
Also, determining the file size and then doing
allData = tsFile.Read <size-of-file>
is faster that ReadAll and doesn't have the problem with enmbedded nulls
(0x00) bytes...
--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
Technet Script Center
http://www.microsoft.com/technet/scriptcenter/default.asp
Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp
>allData = tsFile.Read <size-of-file>
>is faster that ReadAll and doesn't have the problem with enmbedded nulls
>(0x00) bytes...
Thanks! That's the best way yet. It takes care of the null bytes and leaves me
with data in a buffer that I can easily edit. What I am trying to do is change
the palette in a 16 color bitmap. This code fragment will give you an idea of
what I am doing:
buffer=ts.read(54)
buffer=buffer & newPalette
ts.skip(64)
buffer=buffer & ts.read(sz-118)
Then write the buffer to a new file, or back to the old.
does anyone know if the file type can be extracted from the header of the
binary code to determine the type of image (gif,jpeg, etc) ?
I would to be able to get the image type, when the image is stored in a ole
object field of an access database
thanks for any help
Christoph Basedau wrote in message ...
Did Christoph's example work for your bitmap? It didn't in my case. To my
experience, converting byte array into string sometimes fail. So I gave up
the way. Even for text file, if it has BOM, textstream scheme didn't work.
""Crash" Dummy" <dva...@deathstar.mil> wrote in message
news:OB6o893M...@TK2MSFTNGP10.phx.gbl...
Christoph's method worked, but it was very slow, as you would expect. The code
fragment I posted based on Michael's suggestion works beautifully and fast (for
a script). The complete test file to replace the palette in an image is below my
sig.
--
Dave "Crash" Dummy - A weapon of mass destruction
cr...@gpick.com
http://lists.gpick.com
dim palet(15)
'palet-----B,G,R
palet(0)="0,0,0"
palet(1)="0,0,80"
palet(2)="0,80,0"
palet(3)="0,80,80"
palet(4)="80,0,0"
palet(5)="80,0,80"
palet(6)="80,80,0"
palet(7)="C0,C0,C0"
palet(8)="80,80,80"
palet(9)="0,0,FF"
palet(10)="0,FF,0"
palet(11)="0,FF,FF"
palet(12)="FF,0,0"
palet(13)="FF,0,FF"
palet(14)="FF,FF,0"
palet(15)="FF,FF,FF"
Set fso=CreateObject("scripting.FileSystemObject")
Set myFile=fso.getFile("crash.bmp") '640x480x16 image
sz=myFile.size
set ts=myFile.openAsTextStream(1)
buffer=ts.read(54)
for i=0 to 15
color=split(palet(i),",")
for n=0 to 2
buffer=buffer & chr("&H" & color(n))
next
buffer=buffer & chr(0)
next
ts.skip(64)
buffer=buffer & ts.read(sz-118)
ts.close
set ts=myFile.openAsTextStream(2)
ts.write(buffer)
ts.close
By the way, for the textstream problem, I imagine it comes from my localized
OS, Korean. Even for the BOM problem, error(funny characters) occurs when I
use local characters. If all the code is only for some OSs or for your own,
it may be sufficient. I even changed my language option in control panel
without success.
""Crash" Dummy" <dva...@deathstar.mil> wrote in message
news:uzh4Sv5M...@TK2MSFTNGP11.phx.gbl...
Just take a look at a gif or jpeg in a hexviewer.
GIFs start with 'GIF87a' or 'GIF89a' (Ascii), jpegs start
with (hex): FF D8 FF E0 00 10 followed by (ascii) 'JFIF'
Bitmaps start with 'BM', the 11th Char is a '6'
Of course this is just the intro of the header.
> I would to be able to get the image type, when the image is stored in a ole
> object field of an access database
If you want to be absolutely sure, what kind of image is stored
and maybe what subtype too, you need to study headers more closely.
there's a lot information stored in a few bits concerning
color depth, image width, palette information and so on.
which is quite interesting for retrieving technical data
of images or searching for doublets, series..
--
gruss
Christoph
Christoph Basedau wrote in message ...
>> The only Problem IMHO are zero-characters in the bin-file.
>> the ReadAll-Methode treates them as string-terminaters
>> and fills up the rest with zeros.
>> If you read charcter by character you won't have
>> this prob, but you loose speed.
>
>
> Also, determining the file size and then doing
>
> allData = tsFile.Read <size-of-file>
>
> is faster that ReadAll and doesn't have the problem with enmbedded nulls
> (0x00) bytes...
Could you show me an example where nulls cause a problem when reading
binary
data? I have used Read and ReadAll with a number of binary files (ADO .adtg
files for one) and haven't had any problems.
- Todd
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
> does anyone know if the file type can be extracted from the header of the
> binary code to determine the type of image (gif,jpeg, etc) ?
>
> I would to be able to get the image type, when the image is stored in a
> ole object field of an access database
>
> thanks for any help
I just found a script that does this the other day. It actually digs into
many types of image files to determine their sizes, and allows resizing
somehow.
http://www.lewismoten.com/Items/Browse.asp?Path=%2FProgramming%2FvbScript
Gak! Please forgive the unwrapped lines. It's my first time using "M2"
and it apparently has its own ideas about formatting.
I've seen that website already and actually posted a link to it when I asked
for help on this earlier.
that is for reading an image from a file I'm having trouble reading it
from a long binary field in an access database
I can't get it to read the image code correct.
Todd Fiske wrote in message ...
> hi
> thanks
>
> I've seen that website already and actually posted a link to it when I
> asked for help on this earlier.
>
> that is for reading an image from a file I'm having trouble reading it
> from a long binary field in an access database
>
> I can't get it to read the image code correct.
Woops, newbie errors. Don't know specifically about reading from a long
binary field other than to use GetChunk which you probably already know
too!
I also did see ReadAll returning lots of (but not all) null characters when
loading a bitmap file. I guess I must have always used Read(#) for that.