Ejemplo 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();
            }
        }
Ejemplo n.º 2
0
        private void GetOutPutCapability()
        {
            if (this.iamsc == null)
            {
                return;
            }

            int iCount = 0, iSize = 0;
            VIDEO_STREAM_CONFIG_CAPS scc;
            IntPtr       ipscc = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VIDEO_STREAM_CONFIG_CAPS)));
            _AMMediaType mt, cmt;
            IntPtr       ipmt  = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(_AMMediaType)));
            IntPtr       ipcmt = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(_AMMediaType)));

            try
            {
                // clear output size list first
                this.OutPutSizeList.Clear();
                this.MediaTypeList.Clear();
                this.cbxSize.Items.Clear();

                iamsc.GetFormat(out ipmt);

                cmt = (_AMMediaType)Marshal.PtrToStructure(ipmt, typeof(_AMMediaType));

                iamsc.GetNumberOfCapabilities(out iCount, out iSize);

                if (iSize == Marshal.SizeOf(typeof(VIDEO_STREAM_CONFIG_CAPS)))
                {
                    for (int iFormat = 0; iFormat < iCount; iFormat++)
                    {
                        iamsc.GetStreamCaps(iFormat, out ipmt, ipscc);
                        mt  = (_AMMediaType)Marshal.PtrToStructure(ipmt, typeof(_AMMediaType));
                        scc = (VIDEO_STREAM_CONFIG_CAPS)Marshal.PtrToStructure(ipscc, typeof(VIDEO_STREAM_CONFIG_CAPS));

                        if ((mt.majortype == cmt.majortype) &&
                            (mt.subtype == cmt.subtype) &&
                            (mt.formattype == cmt.formattype) &&
                            (mt.cbFormat >= Marshal.SizeOf(typeof(VIDEOINFOHEADER))) &&
                            (mt.pbFormat != null))
                        {
                            this.OutPutSizeList.Add(scc.MaxOutputSize);

                            VIDEOINFOHEADER vih = (VIDEOINFOHEADER)Marshal.PtrToStructure(mt.pbFormat,
                                                                                          typeof(VIDEOINFOHEADER));
                            BITMAPINFOHEADER bih = vih.BitmapInfo;

                            bih.Width      = scc.MaxOutputSize.Width;
                            bih.Height     = scc.MaxOutputSize.Height;
                            bih.SizeImage  = BitmapSize(vih.BitmapInfo);
                            vih.BitmapInfo = bih;
                            this.MediaTypeList.Add(mt);
                            this.cbxSize.Items.Add(scc.MaxOutputSize.Width.ToString() + "x" + scc.MaxOutputSize.Height.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fail to get output size: " + ex.Message);
            }
            finally
            {
                if (ipcmt != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ipcmt);
                }
                if (ipscc != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ipscc);
                }
                if (ipmt != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ipmt);
                }
            }
        }