|
Using IMediaDet GetBitmapBits in C# |
|
I've also tried to use GetBitmapBits in C# before but also failed. Here's another
snippet showing how it can be done. (The code assume that you have definitions for
VideoInfoHeader and BitmapInfoHeader, and define a MediatDet object "md" with its
FileName and CurrentStream properties set).
_AMMediaType mt = md.StreamMediaType;
VideoInfoHeader vih = (VideoInfoHeader)Marshal.PtrToStructure( mt.pbFormat, typeof(VideoInfoHeader)
);
int width = vih.BmiHeader.Width;
int height = vih.BmiHeader.Height;
IntPtr bufPtr = IntPtr.Zero;
int bufSize = 0;
// call once to get the buffer size needed
md.GetBitmapBits( 0.0, ref bufSize, bufPtr, width, height );
//allocate memory
bufPtr = Marshal.AllocHGlobal( bufSize );
// call to retrieve the dib
md.GetBitmapBits( 0.0, ref bufSize, bufPtr, width, height);
// create a bitmap object; 40 is the size of the header on a 32-bit machine
Bitmap b = new Bitmap( width, height, width*3, PixelFormat.Format24bppRbg, new IntPtr(
(int)butPtr + 40 ));
// if needed, change the bitmap orientation
b.RotateFlip( RotateFlipType.Rotate180FlipX );
// do whatever you want with the picture
// free the mem with Marshall.FreeHGlobal when done
You need to edit the interop assembly interop.dexterlib.dll by replacing the the third
argument for the method GetBitmapBits in both the IMediaDet interface and MediaDetClass
from "unsigned int8&" to "native int" i.e. IntPtr (you can check the section "Editing
An Interop Assembly" in the .Net SDK doc for more info).
-daniel
|