Imagine Cup

How to Capture Image from DirectShow

Last post 06-19-2010 9:17 AM by toonsuperlove. 0 replies.

Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 06-19-2010 9:17 AM

    How to Capture Image from DirectShow

    Hi,

    I'm very new to C++,C# programming

    I have struck with how to get image or bitmap out from directshow by using C#

     Currently, I can load video/webcam to run by use follow:
    IGraphBuilder, IVideoWindow, IBaseFilter, IPersist, IPersistPropertyBag, IPropertyBag, ICaptureGraphBuilder2

     

    However I have read alot in internet it has many that complete in C++

    [I'm not good in C++, I didn't understand how to translate this idea to C#]

     this is my code that I try to get Bitmap byte from it

    [or if it has any another idea, please tell me]

    *basicvideo = IBasicVideo

    *graph = IGraphBuilder

     

            public Bitmap saveImage()
            {
                basicvideo = graph as IBasicVideo;

                int buffSize = 0;
                basicvideo.GetCurrentImage(ref buffSize, IntPtr.Zero);
                IntPtr tempBuffer = Marshal.AllocHGlobal(buffSize);
                basicvideo.GetCurrentImage(ref buffSize, tempBuffer);

                buffer = new byte[buffSize];
                int pWidth, pHeight;
                basicvideo.GetVideoSize(out pWidth, out pHeight);
                int tempBufferInt = tempBuffer.ToInt32();
                Marshal.Copy((IntPtr)tempBufferInt, buffer, 0, buffSize);
                Marshal.FreeHGlobal((IntPtr)tempBufferInt);

                return BytesToBmp(buffer,new System.Drawing.Size(pWidth,pHeight));
            }
           
            private Bitmap BytesToBmp(byte[] bmpBytes, System.Drawing.Size imageSize)
            {
                Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height);

                BitmapData bData = bmp.LockBits(new Rectangle(0,0,bmp.Size.Width,bmp.Size.Height),
                    ImageLockMode.WriteOnly,
                    PixelFormat.Format24bppRgb);

                // Copy the bytes to the bitmap object
                Marshal.Copy(bmpBytes, 0, bData.Scan0, bmpBytes.Length);
                bmp.UnlockBits(bData);
                return bmp;
            }

     

    Thanks !

     

    Life is beautiful when you think it beautiful!
Page 1 of 1 (1 items)

Software

Discounts

Student Activities

Student Resources

Fun Stuff

Windows 7 DreamSpark Microsoft Student Partners MSCOM/Student Windows Media Center
Expression Studio Certification Exams Imagine Cup Students to Business AutoCollage
Visual Studio Authorized Reseller Finder Student Rally Own Your Future Games for Windows
Windows Live JourneyEd Facebook Microsoft Math Xbox
Microsoft Office Microsoft Office Microsoft Student Lounge Student Newsletter Windows Phone
© Copyright 2009-2010 Microsoft Corporation. All Right Reserved.
 
This site is developed and hosted for Microsoft by Fast Track Team, Inc.