In a video player, I am trying to get the current image from the Video Mixing Renderer 9. I use the "DirectShowLib" from SourceForge to get the image data since GetCurrentImage isn't supported in the "QuartzTypeLib". I would then like to use the data to make a Bitmap. I tried to use the Scan0 ptr form of new Bitmap but I can't get it to work. I get a generic GDI+ error.
Can someone tell me how to call the GetCurrentImage function in "Quartz.dll" directly
Can someone tell me how to get the data returned from "GetCurrentImage" directly into a Bitmap
I don't want to copy the data or save the data to disk. I need speed.
Here's my code:
Imports DirectShowLib Imports System.Runtime.InteropServices Public Class Form1 Private FileName As String Private FGM As IGraphBuilder Private MC As IMediaControl Private Vid As IBasicVideo2 Private Buf() As Int32 Private pBuf As IntPtr Private gchBuf As GCHandle Private Bmp As BITMAP Private Sub RenderFile() FGM = New FilterGraph Dim VMR9 As IBaseFilter = New VideoMixingRenderer9 FGM.AddFilter(VMR9, "Video Mixing Renderer 9") FGM.RenderFile(FileName, 0) MC = FGM MC.Run() Vid = FGM End Sub Private Sub OpenFileToolStripMenuItem_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles OpenFileToolStripMenuItem.Click If OpenFileDialog1.ShowDialog() = _ Windows.Forms.DialogResult.OK Then FileName = OpenFileDialog1.FileName RenderFile() End If End Sub Private Sub CaptureFrameToolStripMenuItem_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles CaptureFrameToolStripMenuItem.Click If MC Is Nothing Then Return MC.Pause() Dim BufSize As Int32 Vid.GetCurrentImage(BufSize, 0) Dim UbBuf As Int32 = (BufSize >> 2) - 1 ReDim Buf(UbBuf) gchBuf = GCHandle.Alloc(Buf, GCHandleType.Pinned) pBuf = gchBuf.AddrOfPinnedObject Vid.GetCurrentImage(BufSize, pBuf) 'Start of code Dim pScan0 As IntPtr = CType(CInt(pBuf) + 40, IntPtr) Bmp = New Bitmap(Buf(1), Buf(2), -4 * Buf(1), _ Imaging.PixelFormat.Format32bppRgb, pScan0) Bmp.Save("C:\Test.bmp", Imaging.ImageFormat.Bmp) 'End of code End Sub Private Sub ShowFrameToolStripMenuItem_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ShowFrameToolStripMenuItem.Click With PictureBox1 .Image = New Bitmap("C:\Test.bmp") .Width = .Image.Width .Height = .Image.Height Width = .Width + 8 Height = .Height + 54 End With End Sub End Class
It's the lines between 'Start of code and 'End Of code that I need help with.
If I replace those lines with the following I can verify that the data in the array are correct.
Dim Bmfh As New Win.BITMAPFILEHEADER
Bmfh.bfType = &H4D42
Bmfh.bfSize = BufSize + 14
Bmfh.bfOffBits = 54
FileOpen(1, "C:\Test.bmp", OpenMode.Binary)
FilePut(1, Bmfh)
FilePut(1, Buf)
FileClose(1)
gchBuf.Free()