Reading EMF-files

juhah

Active member
Joined
Apr 16, 2007
Messages
28
Programming Experience
3-5
Here's the problem:

I have to read an .emf-file, that contains many records of data. The data has to be read in to a structure called SI. There is an old version (made with VB5) of this function that works great.

The old version has syntax like this:

dim Fnum as integer
dim FName as string
FNum = FreeFile
Open FName For Binary As FNum
Get FNum, , SI

and I tried to write same thing in VB.NET:

dim Fnum as integer
dim FName as string
FNum = FreeFile()
FileSystem.FileOpen(FNum, FName, OpenMode.Binary)
FileSystem.FileGet(FNum, SI)

However, an error occurs in my version that says: "Non-negative number required. Parameter name: count". How can I solve this problem?
 
Hello,

I am having a similar problem. I know it has been four years, but if you happen to remember how you fixed this problem, I would greatly appreciate it. Thank you
 
Hello there,

Yes I fixed the problem. Actually the only problem was that the FileGet function couldn't read data into arrays properly. In my case the data in the file was just certain length of strings, integers, doubles and so on. So that means you can actually read the data piece by piece using binaryreader and filestream classes. Here's an example referring to the original situtation:

The old version has syntax like this:

dim Fnum as integer
dim FName as string
FNum = FreeFile
Open FName For Binary As FNum
Get FNum, , SI

And now same in VB.NET, let's assume that the structure SI contains a string with length of 5 characters, an integer and an array of doubles:

Dim FName as string = pathOfTheFile
Dim i As Integer
Dim fs As New System.IO.FileStream(FName, System.IO.FileMode.Open, IO.FileAccess.Read)
Dim reader As New System.IO.BinaryReader(fs, System.Text.Encoding.Default)

fs.Seek(0, IO.SeekOrigin.Begin) 'the position where we start reading data

SI.Var1 = reader.ReadChars(5)
SI.Var1 = reader.ReadInt32

For i = 0 To 5
SI.Array(i) = reader.ReadDouble
Next i

fs.Close()
reader.Close()

And that's it, it works perfectly. I don't know if there is any easier way to fix this but at least this worked. The problem is that if you change the structure then you have to change the reading function too to match the structure. Let me know if you get any problems or if you have to use same kind of method to write structure data to a binary file (because it also has some stuff that took some time to figure out).
 
Back
Top