FirebirdGuy
Member
- Joined
- Aug 21, 2003
- Messages
- 5
Keywords: VB.Net, AVI Compression, avifil32.dll
Hello, I have been struggling for days now to write a VB.Net application which converts BMP Images to AVI. This program works just fine in VB but will not run in VB.Net. The problem lies with the conversion of VarPtr() in VB to some other means in VB.Net.
The main problem begins when calling the avifil32.dll function AVISaveOptions Defined as follow:
This function wants to receive a pointer to a pointer to an AVI_COMPRESS_OPTIONS Structure. The problem is that with VB.Net we are working with managed memory and it seems this function wants a pointer to a pointer to unmanaged memory.
It has been suggested to use the following algorithm in order to get the VarPtr within .Net:
I have attempted to use this method with no luck at all... I then came across another trick for allocating the memory of the AVI_COMPRESS_OPTIONS to unmanaged memory, and then passing a pointer to that memory location to the AVISaveOptions function. Once the function has completed I then read the memory location back in and I can get the updated valued from the AVISaveOptions call. Here is the code that accomplishes this for me:
At this point my opts object has valid information after the user has chosen their AVI format to save as. Everything seems to be going ok at this point, the following code executes without error:
I am still wondering if passing opts at this point is valid, have I updated the object properly to pass it into this function call?
Then, once I try to call AVIStreamSetFormat the application fails with a return HRESULT which has a value of -2147205018 (AVIERR_BADPARAM ). Here is how I call that function:
This code fails totally when selecting the MPEG4 compression format, it seems to succeed with some other formats but the file is never written to with my calls to AVIStreamWrite. It would seem that the problem lies somehow with the opts.lpParms value which is created by the AVI dll since the code passes the AVIStreamSetFormat when this value is set to 0, and not otherwise. Just for completeness, here is how I call the AVIStreamWrite method:
The Declared Functions are below:
Can anyone help me out and tell me what I am doing wrong in my code. Unfortunately I am not a strong C++ programmer and am weak when it comes to pointers. Has anyone had success with writing bmp images to an AVI file after choosing a compression type in .NET? Any help would be greatly appreciated.
Hello, I have been struggling for days now to write a VB.Net application which converts BMP Images to AVI. This program works just fine in VB but will not run in VB.Net. The problem lies with the conversion of VarPtr() in VB to some other means in VB.Net.
The main problem begins when calling the avifil32.dll function AVISaveOptions Defined as follow:
Code:
Public Declare Function AVISaveOptions Lib "avifil32.dll" (ByVal hWnd As Integer, ByVal uiFlags As Integer, ByVal nStreams As Integer, ByRef ppavi As Integer, ByRef ppOptions As Integer) As Integer TRUE if user pressed OK, False if cancel, or error if error
It has been suggested to use the following algorithm in order to get the VarPtr within .Net:
Code:
Public Function VarPtr(ByVal o As Object) As Integer
Dim oGC As GCHandle = GCHandle.Alloc(o, GCHandleType.Pinned)
Dim ret As Integer = oGC.AddrOfPinnedObject.ToInt32
oGC.Free()
Return ret
End Function
I have attempted to use this method with no luck at all... I then came across another trick for allocating the memory of the AVI_COMPRESS_OPTIONS to unmanaged memory, and then passing a pointer to that memory location to the AVISaveOptions function. Once the function has completed I then read the memory location back in and I can get the updated valued from the AVISaveOptions call. Here is the code that accomplishes this for me:
Code:
Dim opts As AVI_COMPRESS_OPTIONS
//Allocate enough memory on the global heap
Dim lpB As IntPtr = Marshal.AllocHGlobal(Len(opts))
Dim source(11) As Integer 11 elements in AVI_COMPRESS_OPTIONS, all of type Integer
Dim destination(11) As Integer
source(0) = opts.fccType
source(1) = opts.fccHandler
source(2) = opts.dwKeyFrameEvery
source(3) = opts.dwQuality
source(4) = opts.dwBytesPerSecond
source(5) = opts.dwFlags
source(6) = opts.lpFormat
source(7) = opts.cbFormat
source(8) = opts.lpParms
source(9) = opts.cbParms
source(10) = opts.dwInterleaveEvery
//Copy the managed array to the un-managed pointer
Marshal.Copy(source, 0, lpB, 11) Len(opts))
Dim pOpts As Integer
pOpts = lpB.ToInt32()
res = AVISaveOptions(Me.Handle.ToInt32, ICMF_CHOOSE_KEYFRAME Or ICMF_CHOOSE_DATARATE, 1, ps, pOpts)
Get the data back out of unmanaged memory
Marshal.Copy(lpB, destination, 0, 11)
opts.fccType = destination(0)
opts.fccHandler = destination(1)
opts.dwKeyFrameEvery = destination(2)
opts.dwQuality = destination(3)
opts.dwBytesPerSecond = destination(4)
opts.dwFlags = destination(5)
opts.lpFormat = destination(6)
opts.cbFormat = destination(7)
opts.lpParms = destination(8)
opts.cbParms = destination(9)
opts.dwInterleaveEvery = destination(10)
Code:
res = AVIMakeCompressedStream(psCompressed, ps, opts, 0)
Then, once I try to call AVIStreamSetFormat the application fails with a return HRESULT which has a value of -2147205018 (AVIERR_BADPARAM ). Here is how I call that function:
Code:
Dim B() As Byte = bmp.GetBitmapInfo()
Dim iLength As Integer = B.Length
//Allocate enough memory on the global heap
Dim lpB2 As IntPtr = Marshal.AllocHGlobal(iLength)
//Copy the managed array to the un-managed pointer
Marshal.Copy(B, 0, lpB2, iLength)
res = AVIStreamSetFormat(psCompressed, 0, lpB2.ToInt32(), iLength)
Code:
Now write out each video frame
For i = 0 To listBoxImageList.Items.Count - 1
listBoxImageList.SelectedIndex = i
bmp.CreateFromFile(listBoxImageList.Text) load the bitmap (ignore errors)
bmp.PointerToBits uses the same technique as above with using the Marshal class to get a pointer the an unmanaged memory location holding the bitmap info.
res = AVIStreamWrite(psCompressed, i, 1, bmp.PointerToBits, bmp.SizeImage, AVIIF_KEYFRAME, 0, 0)
If (res <> AVIERR_OK) Then
MsgBox("AVIStreamWrite failed.")
Exit Function
End If
Next
Code:
Public Declare Function AVIMakeCompressedStream Lib "avifil32.dll" (ByRef ppsCompressed As Integer, ByVal psSource As Integer, ByRef lpOptions As AVI_COMPRESS_OPTIONS, ByVal pclsidHandler As Integer) As Integer
Public Declare Function AVIStreamSetFormat Lib "avifil32.dll" (ByVal pavi As Integer, ByVal lPos As Integer, ByRef lpFormat As Integer, ByVal cbFormat As Integer) As Integer
Public Declare Function AVIStreamWrite Lib "avifil32.dll" (ByVal pavi As Integer, ByVal lStart As Integer, ByVal lSamples As Integer, ByVal lpBuffer As Integer, ByVal cbBuffer As Integer, ByVal dwFlags As Integer, ByRef plSampWritten As Integer, ByRef plBytesWritten As Integer) As Integer
Can anyone help me out and tell me what I am doing wrong in my code. Unfortunately I am not a strong C++ programmer and am weak when it comes to pointers. Has anyone had success with writing bmp images to an AVI file after choosing a compression type in .NET? Any help would be greatly appreciated.
Last edited by a moderator: