bugs >> Using FileSystem Object to read IMAGE file contents

by cG11ZA » Thu, 20 Jul 2006 03:17:01 GMT

Hi,

I am using the filesystem object to read the contents of an image into a
string. Below is my code. But I think something is wrong with my code. When
i put the contents I have read in an image file, the image is blank.

I am reading the image file as follows. I think I need to read the file as a
binary, but am not sure how to do so. Below is my code:

Dim fso As New FileSystemObject
Dim fi As File
Dim ts As TextStream

Set fi = fso.GetFile(C:/test.jpg)
Set ts = fi.OpenAsTextStream(ForReading, TristateUseDefault)
strData = ts.ReadAll()


Open "c:\temp\test1.jpg" For Binary As #2
Put #2, , contents
Close #2


Any help will be highly appreciated.

Thanks

--
pmud


bugs >> Using FileSystem Object to read IMAGE file contents

by Bob Butler » Thu, 20 Jul 2006 03:36:26 GMT




XXXX@XXXXX.COM

The FSO is text only; at best you'll get very corrupted data.

dim ff as long
dim b() as byte
ff=freefile
open "c:\test.jpg" for binary as #ff
redim b(1 to lof(ff))
get #ff,,b
close #ff
Open "c:\temp\test1.jpg" For Binary As #ff
Put #ff, , b
Close #ff

or use the FileCopy or Name statement

--
Reply to the group so all can participate
VB.Net: "Fool me once..."




bugs >> Using FileSystem Object to read IMAGE file contents

by cG11ZA » Thu, 20 Jul 2006 03:45:02 GMT

Hi Bob,

How do I use FileCopy or Name. Can you show some code. Basically i want to
store the filecontents in a string variable.

Thanks for your help.
--
pmud







Using FileSystem Object to read IMAGE file contents

by cG11ZA » Thu, 20 Jul 2006 03:55:01 GMT

Hi Bob,

Do you think it will be possibel to read an image using the stream object in
vb 6? If so, then can you please post some sample code on how to do so.

Thanks
--
pmud







Using FileSystem Object to read IMAGE file contents

by Bob Butler » Thu, 20 Jul 2006 04:03:18 GMT



XXXX@XXXXX.COM

FileCopy sourcepath, destinationpath

Name sourcepath As newpath

You don't want to store binary data in a string because VB does unicode
conversions that can mess up the data; use a byte array as in my previous
example

--
Reply to the group so all can participate
VB.Net: "Fool me once..."



Using FileSystem Object to read IMAGE file contents

by cG11ZA » Thu, 20 Jul 2006 04:50:02 GMT

Hi Bob,

The method you had mentioned was the one i was using. But that was too slow
thats why i thought about the filesystem object.

Do you know how to concatenate 2 byte variables in vb 6?

Thanks
--
pmud







Using FileSystem Object to read IMAGE file contents

by Robert Morley » Thu, 20 Jul 2006 04:59:00 GMT

It was too slow? I've generally found that bulk-reading a file into a byte
array is quite fast in VB. Were you doing something different than in Bob's
example, maybe?



Rob







Using FileSystem Object to read IMAGE file contents

by Bob Butler » Thu, 20 Jul 2006 05:27:57 GMT



XXXX@XXXXX.COM

The FSO is never going to be faster than VB's native file I/O; API calls
might be faster but usually not by enough to matter.


Into what? I think you need to explain what the real goal is before anybody
can really help.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."



Using FileSystem Object to read IMAGE file contents

by cG11ZA » Thu, 20 Jul 2006 05:46:02 GMT

Hi Bob,

I read the byte contents of teh image using a stream object. But my problem
is that I need to use a function to create the post string. Below is my
function. In This function, I am appending the byte contents of teh image to
a string. SO I am getting a type mismatch error. Below is my code:


Private Function AttachFile(strInputName As String, strFileName As String,
strFileType As String, ByRef strFileContent() As Byte, ByRef strRequestBody()
As Byte)

Dim strFieldCont As String

strFieldCont = _
"--" & strHTMLBoundary & vbCrLf & _
"Content-Disposition: form-data; name=""" & strInputName & """
filename=""" & strFileName & """" & vbCrLf & _
"Content-Type: " & strFileType & vbCrLf & vbCrLf

Dim test As Byte
test = CByte(strFieldCont)
Dim p() As Byte
p = test & strFileContent()

strRequestBody = strRequestBody & test ' --- ERROR HERE." Type mismatch.

End Function

Thanks for your help.
--
pmud







Using FileSystem Object to read IMAGE file contents

by Bob Butler » Thu, 20 Jul 2006 05:53:57 GMT



XXXX@XXXXX.COM

Which is not good for binary data


so you are trying to upload the file to a web server? What are you using to
connect and do the upload?


It's been a long time since I tried anything like that. I think you're
going to have to use a content transfer encoding of some kind (e.g. base64);
hopefully somebody else with more recent knowledge will come along and
assist.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."



Similar Threads

1. Using contents of stream object in image

2. reading contents of an activexscript using DTS objects in vb

I'm using VB6 and SS2000. I'm searching for a table name and I want to
search in the actual script in a DTSActiveScriptTask object. I can see the
name of the script and the description but is there a way to actually see
the script itself? There must be since it is stored somewhere. What object
is it?

Thanks,

Dan


3. VB filesystem object, file dates etc

4. Writing a file with the FileSystem Object

Think of TextStream as a text file with secuencial access. It has the benefits that can be treated as an object instead as a file number:

Write #1,"Text"
MyTextStream.Write "Text"

Regards.

5. File and Filesystem objects

6. Unable to read image file correctly that was written using vb6.0

I am facing a strange problem I ahve a datadase which stores  tiff
images using Image datatype in Sql server.
These images are written using a Vb6.0 desktop application with the
following code

==================VB Code====================================
 Open strFile For Binary Access Read As #(iFile)
    ImageSize = LOF(iFile)
    lngNumChunks = ImageSize / 4096
    lngOff = ImageSize Mod 4096
    strData = String(4096, "0")
    For i = 1 To lngNumChunks
      strData = String(4096, "0")
      Get #(iFile), , strData
      rs.Fields("Img").AppendChunk strData
    Next
    If lngOff > 0 Then
      strData = String(lngOff, "0")
      Get #(iFile), , strData
      rs.Fields("Img").AppendChunk strData
    End If
    Close #(iFile)
====================VB Code==================================
It works fine when i read it with a VB6.0 application but i have tried
my level best without sucess to read this image file in Dot net.
A sample code that i last tried is as follows


=========Dot Net C# code===========================
SqlCommand cmd=new SqlCommand();
SqlDataReader rdr;
cmd.CommandType=CommandType.Text ;
cmd.CommandText="select img from tblDAllImages where Active = 1 and ID
=1135";
cmd.Connection=con;
con.Open ();
rdr=cmd.ExecuteReader();
System.IO.FileStream fs ;//=	new System.IO.FileStream("c:\\aa.tif",
System.IO.FileMode.Create, System.IO.FileAccess.Write);
BinaryWriter bw  ;
int BufferSize=100;
byte[] b =new byte[BufferSize];
long retval,startindex=0;
for(int i=0;i<b.Length ;i++)
Response.Write(b[i]);
Response.Write(b.Length);
if (rdr.Read())
{
fs = new FileStream("c:\\aaa11.tif" , FileMode.OpenOrCreate,
FileAccess.Write) ;
bw = new BinaryWriter(fs) ;
startindex = 0 ;
retval = rdr.GetBytes(0 , startindex, b, 0, BufferSize) ;
while( retval == BufferSize )
{
bw.Write(b) ;
bw.Flush();
startindex = startindex + BufferSize;
retval = rdr.GetBytes(0, startindex, b, 0, BufferSize) ;
}
bw.Write(b) ;
bw.Flush();
bw.Close() ;
fs.Close();
}

=========Dot Net C# code===========================
This file gets created but is corrupted.It is almost double the size of
the VB6.0 file.
I guess it is something retated to conversion between byte and string
type but i have even tried to convert the byte to a string and then
save it.
Any help is welcome
Thanks

7. Writing to a file and then reading the file contents

8. File Name used in Image Object

I'm working with the VB source code of someone who does't work with us 
anymore.  The programmer used some images in the code but I can't figure out 
the names of the files he used.  When I go on the properties for the object, 
I can tell there is a bitmap in the Picture Property, but how can I get the 
name of the bitmap?
-- 
Mario