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 !