コード例 #1
0
        private static byte[] CopyBitmapToArray(int height, IntPtr targetDC, IntPtr compatibleBitmapHandle, out BITMAPINFO info)
        {
            info = GetBitmapInfo(targetDC, compatibleBitmapHandle);
            //info.bmiHeader.biCompression = BitmapCompressionMode.BI_RGB;
            info.bmiHeader.biHeight = -info.bmiHeader.biHeight;

            var data = new byte[info.bmiHeader.biSizeImage];
            int ret = Gdi32.GetDIBits(targetDC, compatibleBitmapHandle,
                                  0,
                                  (uint) height,
                                  data,
                                  ref info,
                                  DIB_Color_Mode.DIB_RGB_COLORS);
            Debug.Assert(ret == height);

            info.bmiHeader.biHeight = -info.bmiHeader.biHeight;

            return data;
        }
コード例 #2
0
 public static extern int GetDIBits([In] IntPtr hdc, [In] IntPtr hbmp, uint uStartScan, uint cScanLines, [Out] byte[] lpvBits, ref BITMAPINFO lpbi, DIB_Color_Mode uUsage);
コード例 #3
0
        private static BITMAPINFO GetBitmapInfo(IntPtr targetDC, IntPtr compatibleBitmapHandle)
        {
            BITMAPINFO info;
            info = new BITMAPINFO
                {
                    bmiHeader =
                        {
                            biSize = (uint) Marshal.SizeOf(typeof (BITMAPINFOHEADER)),
                        }
                };

            int ret = Gdi32.GetDIBits(targetDC, compatibleBitmapHandle,
                                      0,
                                      0,
                                      null,
                                      ref info,
                                      DIB_Color_Mode.DIB_RGB_COLORS);
            Debug.Assert(ret != 0);
            return info;
        }
コード例 #4
0
        private void OnDataReceived(byte[] data, BITMAPINFO info)
        {
            const PixelFormat format = PixelFormat.Format32bppRgb;
            var header = info.bmiHeader;

            var width = header.biWidth;
            var height = header.biHeight;
            if (width == 0 || height == 0)
                return;

            var bitmap = new Bitmap(width, height, format);
            try
            {
                var bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height),
                                                 ImageLockMode.WriteOnly,
                                                 format);
                try
                {
                    Marshal.Copy(data, 0, bitmapData.Scan0, (int) header.biSizeImage);
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }
            }
            catch (Exception)
            {
                bitmap.Dispose();
                throw;
            }

            if (_bitmap != null)
                _bitmap.Dispose();
            _bitmap = bitmap;

            Invalidate();
        }
コード例 #5
0
        private byte[] CaptureWindow(int x, int y, int width, int height, out BITMAPINFO info)
        {
            IntPtr sourceDC = IntPtr.Zero;
            IntPtr targetDC = IntPtr.Zero;
            IntPtr compatibleBitmapHandle = IntPtr.Zero;

            try
            {
                sourceDC = User32.GetDC(_hWnd);
                targetDC = Gdi32.CreateCompatibleDC(sourceDC);

                compatibleBitmapHandle = Gdi32.CreateCompatibleBitmap(sourceDC, width, height);

                IntPtr oldObject = Gdi32.SelectObject(targetDC, compatibleBitmapHandle);

                Gdi32.BitBlt(targetDC, 0, 0, width, height, sourceDC, x, y, Gdi32.SRCCOPY);

                Gdi32.SelectObject(targetDC, oldObject);

                var data = CopyBitmapToArray(height, targetDC, compatibleBitmapHandle, out info);
                return data;
            }
            finally
            {
                bool ret = Gdi32.DeleteObject(compatibleBitmapHandle);
                Debug.Assert(ret);

                ret = User32.ReleaseDC(_hWnd, sourceDC);
                Debug.Assert(ret);

                ret = Gdi32.DeleteDC(targetDC);
                Debug.Assert(ret);
            }
        }