VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
Go Back  Xtreme Visual Basic Talk > > > VB.Net Conversion of BMP to AVI


Closed Thread
 
Thread Tools Display Modes
  #1  
Old 08-21-2003, 02:06 PM
FirebirdGuy FirebirdGuy is offline
Newcomer
 
Join Date: Aug 2003
Posts: 1
Default VB.Net Conversion of BMP to AVI


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:
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

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:

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:


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)

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:

res = AVIMakeCompressedStream(psCompressed, ps, opts, 0)

I am still wondering if passing opts at this point if 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. Here is how I call that function:


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)

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:

' 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


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 FirebirdGuy; 08-21-2003 at 02:14 PM.
  #2  
Old 08-21-2003, 02:15 PM
reboot's Avatar
rebootVB.Net Conversion of BMP to AVI reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default

[ednf]..[/ednf]
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can someone tell me whats wrong?? rolandi Miscellaneous Languages 6 10-08-2002 09:05 PM
VB.Net quwiltw Forum Questions / Concerns / Comments 2 09-21-2002 09:09 AM
What's the future hold for VB? Ashington Sid Tech Discussions 18 06-04-2002 12:20 PM
A VB6 to VB.NET conversion sample Helmar General 0 11-14-2001 05:27 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
 
VB.Net Conversion of BMP to AVI
VB.Net Conversion of BMP to AVI
 
-->