Exemplo n.º 1
0
        public static bool Load()
        {
            _logger.Debug("Load()");
            if (!Initialized)
            {
                try
                {
                    var result = DCapt.DCaptLoad(out hLoad);

                    if (result == DCapt.CaptError.DESKCAPT_ERROR_API_ALREADY_LOADED)
                    {
                        //...
                    }

                    DCapt.ThrowIfError(result, "DCaptLoad");

                    Initialized = true;
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            }

            return(Initialized);
        }
Exemplo n.º 2
0
        public static void Unload()
        {
            _logger.Debug("Unload()");

            try
            {
                if (hLoad != IntPtr.Zero)
                {
                    DCapt.DCaptFree(hLoad);
                    hLoad       = IntPtr.Zero;
                    Initialized = false;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }
Exemplo n.º 3
0
        public override void Close()
        {
            logger.Debug("DatapathDesktopCapture::Close()");

            if (Initialized)
            {
                if (hCapt != IntPtr.Zero)
                {
                    var result = DCapt.DCaptFreeCapture(hCapt);
                    DCapt.ThrowIfError(result, "DCaptFreeCapture");
                    hCapt = IntPtr.Zero;
                }

                if (pBuffer != IntPtr.Zero)
                {// создается в DCaptConfigureCapture соответственно удаляется в DCaptFreeCapture() !!
                    // Marshal.FreeHGlobal(pBuffer);
                    pBuffer = IntPtr.Zero;
                }

                this.bmi = default(BITMAPINFO);
            }

            base.Close();
        }
Exemplo n.º 4
0
        private void Init(Rectangle captArea, Bitmap bmp)
        {
            if (!(bmp.PixelFormat == PixelFormat.Format16bppRgb565 || bmp.PixelFormat == PixelFormat.Format16bppRgb565))
            {
                throw new FormatException("Unsuppoted pix format " + bmp.PixelFormat);
            }

            try
            {
                var result = DCapt.DCaptCreateCapture(hLoad, out hCapt);
                DCapt.ThrowIfError(result, "DCaptCreateCapture");

                logger.Debug("DCaptCreateCapture() " + result);

                int biWidth  = bmp.Width;
                int biHeight = bmp.Height;
                //
                int  biBitCount  = Image.GetPixelFormatSize(bmp.PixelFormat);
                uint biSizeImage = (uint)(biWidth * biHeight * biBitCount / 8);

                const int BI_BITFIELDS = 3;

                var bmiHeader = new BITMAPINFOHEADER
                {
                    biWidth  = biWidth,
                    biHeight = -biHeight,
                    biSize   = (uint)Marshal.SizeOf(typeof(BITMAPINFOHEADER)),

                    biBitCount = (ushort)biBitCount,
                    biPlanes   = 1,

                    biClrUsed      = 0,
                    biClrImportant = 0,
                    biSizeImage    = biSizeImage,
                    biCompression  = BI_BITFIELDS,
                };

                var bmiColors = GetColourMask(bmp.PixelFormat);

                //var bmiColors = new RGBQUAD[]
                //{
                //     new RGBQUAD
                //     {
                //         rgbRed = 0,
                //         rgbBlue = 248,
                //         rgbGreen = 0
                //     }
                //};

                BITMAPINFO bmi = new BITMAPINFO
                {
                    bmiHeader = bmiHeader,
                    // bmiColors = bmiColors,
                };


                var dstSize = bmp.Size;

                RECT srcRect = new RECT
                {
                    Left   = captArea.Left,
                    Right  = captArea.Right,
                    Bottom = captArea.Bottom,
                    Top    = captArea.Top,
                };

                IntPtr _hBmi = IntPtr.Zero;
                try
                {
                    _hBmi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(BITMAPINFO)));
                    Marshal.StructureToPtr(bmi, _hBmi, false);

                    IntPtr hBmi = _hBmi;

                    // The bits per pixel of the saved data. Must be 2
                    int bitsPerPixel = 2;//biBitCount / 8;
                    result = DCapt.DCaptConfigureCapture(hCapt, ref srcRect, ref dstSize, bitsPerPixel, DCapt.CaptFlags.CAPTURE_FLAG_OVERLAY, ref pBuffer, ref hBmi);
                    DCapt.ThrowIfError(result, "DCaptConfigureCapture");

                    this.bmi = (BITMAPINFO)Marshal.PtrToStructure(hBmi, typeof(BITMAPINFO));
                    var _bmiHeader = bmi.bmiHeader;

                    logger.Debug("_bmiHeader " + _bmiHeader.biWidth + "x" + _bmiHeader.biHeight + " "
                                 + _bmiHeader.biBitCount + " " + _bmiHeader.biCompression + " " + _bmiHeader.biSizeImage);
                }
                finally
                {
                    if (_hBmi != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(_hBmi);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);

                Close();
                throw;
            }
        }
Exemplo n.º 5
0
        public override ErrorCode UpdateBuffer(int timeout = 10)
        {
            logger.Verb("Update(...) " + timeout);

            ErrorCode errorCode = ErrorCode.Unexpected;

            if (!Initialized)
            {
                return(ErrorCode.NotInitialized);
            }

            var bufSize = bmi.bmiHeader.biSizeImage;

            if (bufSize > 0)
            {
                Kernel32.ZeroMemory(pBuffer, (int)bufSize);

                var result = DCapt.DCaptUpdate(hCapt);
                DCapt.ThrowIfError(result, "DCaptCreateCapture");

                var syncRoot = videoBuffer.syncRoot;

                bool lockTaken = false;
                try
                {
                    Monitor.TryEnter(syncRoot, timeout, ref lockTaken);

                    if (lockTaken)
                    {
                        var sharedBits = videoBuffer.bitmap;
                        var rect       = new Rectangle(0, 0, sharedBits.Width, sharedBits.Height);
                        var data       = sharedBits.LockBits(rect, ImageLockMode.ReadWrite, sharedBits.PixelFormat);
                        try
                        {
                            IntPtr scan0 = data.Scan0;

                            Kernel32.CopyMemory(scan0, this.pBuffer, (uint)bufSize);

                            errorCode = ErrorCode.Ok;
                        }
                        finally
                        {
                            sharedBits.UnlockBits(data);
                        }
                    }
                    else
                    {
                        logger.Warn("Drop bits...");
                    }
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(syncRoot);
                    }
                }
            }

            return(errorCode);

            // Console.WriteLine("DCaptCreateCapture() " + result);
        }