Esempio n. 1
0
        private void btnCapture_Click(object sender, EventArgs e)
        {
            isg.GetConnectedMediaType(ref SGMediaType);

            VIDEOINFOHEADER vih = (VIDEOINFOHEADER)Marshal.PtrToStructure(SGMediaType.pbFormat,
                                                                          typeof(VIDEOINFOHEADER));
            // Get a copy of the BITMAPINFOHEADER, to be used in the BITMAPFILEHEADER
            BITMAPINFOHEADER bih = vih.BitmapInfo;
            int len = (int)BitmapSize(bih);
            // Allocate bytes, plus room for a BitmapFileHeader
            int    sizeOfBFH = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
            int    sizeOfBIH = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
            IntPtr ptrBlock  = Marshal.AllocCoTaskMem(len + sizeOfBFH + sizeOfBIH);
            IntPtr ptrBIH    = new IntPtr(ptrBlock.ToInt64() + sizeOfBFH);
            IntPtr ptrImg    = new IntPtr(ptrBlock.ToInt64() + sizeOfBFH + sizeOfBIH);

            try
            {
                // Get the DIB
                isg.GetCurrentBuffer(ref len, ptrImg);

                // Create header for a file of type .bmp
                BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
                bfh.Type      = (UInt16)((((byte)'M') << 8) | ((byte)'B'));
                bfh.Size      = (uint)(len + sizeOfBFH + sizeOfBIH);
                bfh.Reserved1 = 0;
                bfh.Reserved2 = 0;
                bfh.OffBits   = (uint)(sizeOfBFH + sizeOfBIH);

                // Copy the BFH into unmanaged memory, so that we can copy
                // everything into a managed byte array all at once
                Marshal.StructureToPtr(bfh, ptrBlock, false);

                Marshal.StructureToPtr(bih, ptrBIH, false);

                // Pull it out of unmanaged memory into a managed byte[]
                byte[] img = new byte[len + sizeOfBFH + sizeOfBIH];
                Marshal.Copy(ptrBlock, img, 0, len + sizeOfBFH + sizeOfBIH);

                //System.IO.File.WriteAllBytes("cxp.bmp", img);

                System.IO.MemoryStream m = new System.IO.MemoryStream(img);
                this.m_Image      = Image.FromStream(m);
                this.DialogResult = DialogResult.OK;
            }
            finally
            {
                // Free the unmanaged memory
                if (ptrBlock != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptrBlock);
                }
                this.Close();
            }
        }
Esempio n. 2
0
        private void btnCapture_Click(object sender, EventArgs e)
        {
            isg.GetConnectedMediaType(ref SGMediaType);

            VIDEOINFOHEADER vih = (VIDEOINFOHEADER)Marshal.PtrToStructure(SGMediaType.pbFormat,
                typeof(VIDEOINFOHEADER));
            // Get a copy of the BITMAPINFOHEADER, to be used in the BITMAPFILEHEADER
            BITMAPINFOHEADER bih = vih.BitmapInfo;
            int len = (int)BitmapSize(bih);
            // Allocate bytes, plus room for a BitmapFileHeader
            int sizeOfBFH = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
            int sizeOfBIH = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
            IntPtr ptrBlock = Marshal.AllocCoTaskMem(len + sizeOfBFH + sizeOfBIH);
            IntPtr ptrBIH = new IntPtr(ptrBlock.ToInt64() + sizeOfBFH);
            IntPtr ptrImg = new IntPtr(ptrBlock.ToInt64() + sizeOfBFH + sizeOfBIH);

            try
            {
                // Get the DIB
                isg.GetCurrentBuffer(ref len, ptrImg);

                // Create header for a file of type .bmp
                BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
                bfh.Type = (UInt16)((((byte)'M') << 8) | ((byte)'B'));
                bfh.Size = (uint)(len + sizeOfBFH + sizeOfBIH);
                bfh.Reserved1 = 0;
                bfh.Reserved2 = 0;
                bfh.OffBits = (uint)(sizeOfBFH + sizeOfBIH);

                // Copy the BFH into unmanaged memory, so that we can copy
                // everything into a managed byte array all at once
                Marshal.StructureToPtr(bfh, ptrBlock, false);

                Marshal.StructureToPtr(bih, ptrBIH, false);

                // Pull it out of unmanaged memory into a managed byte[]
                byte[] img = new byte[len + sizeOfBFH + sizeOfBIH];
                Marshal.Copy(ptrBlock, img, 0, len + sizeOfBFH + sizeOfBIH);

                //System.IO.File.WriteAllBytes("cxp.bmp", img);

                System.IO.MemoryStream m = new System.IO.MemoryStream(img);
                this.m_Image = Image.FromStream(m);
                this.DialogResult = DialogResult.OK;
            }
            finally
            {
                // Free the unmanaged memory
                if (ptrBlock != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptrBlock);
                }
                this.Close();
            }
        }