How to draw image using OnVideoFrameBuffer event

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

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

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

        // 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;
        }

VisioForge © 2006-2019

  • GitHub-Mark-64px
  • Twitter Социальные Иконка
  • VisioForge Facebook page
  • YouTube channel