How to draw an image using the OnVideoFrameBuffer event

If you want to draw something on maximal speed better to use the OnVideoFrameBuffer event. This event operates with RGB24 data.

In this sample, we'll draw a .Net Bitmap image located in a file on a video stream using Video Capture SDK .Net. You can use the same event and API in other SDKs. ​

The source image can be 32 bit (with alpha channel) or 24 bit or less (without alpha channel).

Sample code

        // Bitmap loading from file
        //private Bitmap logoImage = new Bitmap(@"c:\samples\logo32.png");
        private Bitmap logoImage = new Bitmap(@"c:\samples\logo24.jpg");

        // Logo RGB24/RGB32 buffer
        private IntPtr logoImageBuffer = IntPtr.Zero;
        private int logoImageBufferSize = 0;

        private void VideoCapture1_OnVideoFrameBuffer(Object sender, VideoFrameBufferEventArgs e)
        {
            // Create logo buffer if not allocated or have zero size
            if (logoImageBuffer == IntPtr.Zero || logoImageBufferSize == 0)
            {
                if (logoImageBuffer == IntPtr.Zero)
                {
                    if (logoImage.PixelFormat == PixelFormat.Format32bppArgb)
                    {
                        logoImageBufferSize = ImageHelper.GetStrideRGB32(logoImage.Width) * logoImage.Height;
                        logoImageBuffer = Marshal.AllocCoTaskMem(logoImageBufferSize);
                    }
                    else
                    {
                        logoImageBufferSize = ImageHelper.GetStrideRGB24(logoImage.Width) * logoImage.Height;
                        logoImageBuffer = Marshal.AllocCoTaskMem(logoImageBufferSize);
                    }
                }
                else
                {
                    if (logoImage.PixelFormat == PixelFormat.Format32bppArgb)
                    {
                        logoImageBufferSize = ImageHelper.GetStrideRGB32(logoImage.Width) * logoImage.Height;

                        Marshal.FreeCoTaskMem(logoImageBuffer);
                        logoImageBuffer = Marshal.AllocCoTaskMem(logoImageBufferSize);
                    }
                    else
                    {
                        logoImageBufferSize = ImageHelper.GetStrideRGB24(logoImage.Width) * logoImage.Height;

                        Marshal.FreeCoTaskMem(logoImageBuffer);
                        logoImageBuffer = Marshal.AllocCoTaskMem(logoImageBufferSize);
                    }
                }

                if (logoImage.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    ImageHelper.BitmapToIntPtr(logoImage, logoImageBuffer, logoImage.Width, logoImage.Height,
                        PixelFormat.Format32bppArgb);
                }
                else
                {
                    ImageHelper.BitmapToIntPtr(logoImage, logoImageBuffer, logoImage.Width, logoImage.Height,
                        PixelFormat.Format24bppRgb);
                }
            }

            // Draw image
            if (logoImage.PixelFormat == PixelFormat.Format32bppArgb)
            {
                MFP.Draw_RGB32OnRGB24(logoImageBuffer, logoImage.Width, logoImage.Height, e.Frame.Data, e.Frame.Width,
                    e.Frame.Height, 0, 0);
            }
            else
            {
                MFP.Draw_RGB24OnRGB24(logoImageBuffer, logoImage.Width, logoImage.Height, e.Frame.Data, e.Frame.Width,
                    e.Frame.Height, 0, 0);
            }

            e.UpdateData = true;
        }
Please contact support to get help with this tutorial. Visit our GitHub page to get more code samples.
abstract 2abstract 1abstract 3
logo
VisioForge © 2006 - 2021