Question : How to use CreateFile and WriteFile functions from "kernel32" in VB.NET ?

Greetings !

I'm converting classic vb project to VB.NET. The application does Cheque scanning and store images in memory, so the developer has to save those images manually to the hard disk (HD).

I'm having a difficulty while saving to the HD using VB.NET

Please have a look at the function I wrote in Classic VB.

'// Saves an image from memory
Public Function SaveImage(ByVal strFileName$, ByVal lngMem&, ByVal lngBytes&) As Long
    Dim lngFile&
    Dim lngTemp&
   
    lngFile = CreateFile(strFileName, GENERIC_WRITE, FILE_SHARE_READ, 0, _
                         CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0)
    If lngFile = INVALID_HANDLE_VALUE Then
        SaveImage = -801 '// FSM Error opening TIFF/file
        Exit Function
    End If
   
    '// Write the image to the file
    If (WriteFile(lngFile, ByVal lngMem, lngBytes, lngTemp, ByVal 0&) = 0) Then
        SaveImage = -803 '// FSM Error writing to TIFF/file
    End If
    CloseHandle (lngFile)
End Function

In VB.NET, I'm writing this routine:

Public Function SaveImage(ByVal strFileName As String, ByVal lngMem As Int32, _
            ByVal lngBytes As Int32) As Int32

        If File.Exists(strFileName) Then File.Delete(strFileName)
        Dim fs As New FileStream(strFileName, FileMode.CreateNew, FileAccess.Write)
        Dim w As New BinaryWriter(fs)
        w.Write(lngMem)
        w.Close()
        fs.Close()
End Function

Image is getting created in the given path, but when i try to open in Imaging Preview, Error is prompting "The doument's format is invalid or not supported."

Can anyone of you please help me to convert that function in VB.NET. Your help will be highly appreciated. I'm stuck.

Cheers
YamihO

Answer : How to use CreateFile and WriteFile functions from "kernel32" in VB.NET ?

Ok,
I think this is about how you would do it.  Since I don't have the scanner or API I can't test it, but something like this should work to get the front or back image.  I have no idea how you combine them, but this is how you would get the data out of memory and into a Byte array

Dim bufPtr as IntPtr
Dim lParam as Int32
Dim lFrontSize as Int32

bufPtr = fsmSortGetFrontMemory(lParam, lFrontSize)
Dim bt(lFrontSize) as Byte

'this will copy the memory into the bt Byte array
Marshal.Copy(bufPtr, bt, 0, lFrontSize)

Here's the doc for the Marshal.Copy function...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclasscopytopic7.asp

You'll need to import the System.Runtime.InteropServices namespace into your class to use it.


Random Solutions  
 
programming4us programming4us