Ejemplo n.º 1
0
        public static void Snapshot(IntPtr hReal, string FileName, System.Drawing.Rectangle rect)
        {
            BITMAPINFOHEADER bitmapinfoheader;
            RECT             structure = new RECT
            {
                bottom = 0,
                left   = 0,
                right  = 0,
                top    = 0
            };
            IntPtr lpRect = LocalAlloc(0x40, Marshal.SizeOf(structure));

            GetWindowRect(hReal, lpRect);
            structure = (RECT)Marshal.PtrToStructure(lpRect, typeof(RECT));
            int nWidth  = (structure.right - structure.left) - 1;
            int nHeight = structure.bottom - structure.top;

            nWidth  = rect.Width;
            nHeight = rect.Height;
            IntPtr dC  = GetDC(hReal);
            IntPtr hDC = CreateCompatibleDC(dC);

            bitmapinfoheader = new BITMAPINFOHEADER
            {
                biBitCount     = 0x18,
                biClrUsed      = 0,
                biClrImportant = 0,
                biCompression  = 0,
                biHeight       = nHeight,
                biWidth        = nWidth,
                biPlanes       = 1
            };
            bitmapinfoheader.biSize = (uint)Marshal.SizeOf(bitmapinfoheader);
            int length = ((bitmapinfoheader.biHeight * bitmapinfoheader.biWidth) * bitmapinfoheader.biBitCount) / 8;

            bitmapinfoheader.biSizeImage     = (uint)length;
            bitmapinfoheader.biXPelsPerMeter = 0xb12;
            bitmapinfoheader.biYPelsPerMeter = 0xb12;
            IntPtr zero = IntPtr.Zero;
            IntPtr ptr  = LocalAlloc(0x40, (int)bitmapinfoheader.biSize);

            Marshal.StructureToPtr(bitmapinfoheader, ptr, false);
            IntPtr hObject = CreateDIBSection(dC, ptr, 0, ref zero, IntPtr.Zero, 0);

            Marshal.PtrToStructure(ptr, bitmapinfoheader);
            BITMAPINFOHEADER bitmapinfoheader2 = (BITMAPINFOHEADER)Marshal.PtrToStructure(ptr, typeof(BITMAPINFOHEADER));
            IntPtr           ptr7 = SelectObject(hDC, hObject);

            BitBlt(hDC, 0, 0, nWidth, nHeight, dC, 0, 0, 0xcc0020);
            byte[] destination = new byte[length];
            Marshal.Copy(zero, destination, 0, length);
            BITMAPFILEHEADER bitmapfileheader = new BITMAPFILEHEADER
            {
                bfSize    = (uint)(length + 0x36),
                bfType    = 0x4d42,
                bfOffBits = 0x36
            };
            int index = 14;

            byte[] array = new byte[index];
            BitConverter.GetBytes(bitmapfileheader.bfType).CopyTo(array, 0);
            BitConverter.GetBytes(bitmapfileheader.bfSize).CopyTo(array, 2);
            BitConverter.GetBytes(bitmapfileheader.bfOffBits).CopyTo(array, 10);
            byte[] buffer3 = new byte[length + 0x36];
            array.CopyTo(buffer3, 0);
            array = new byte[Marshal.SizeOf(bitmapinfoheader)];
            IntPtr ptr8 = LocalAlloc(0x40, Marshal.SizeOf(bitmapinfoheader));

            Marshal.StructureToPtr(bitmapinfoheader2, ptr8, false);
            Marshal.Copy(ptr8, array, 0, Marshal.SizeOf(bitmapinfoheader));
            LocalFree(ptr8);
            array.CopyTo(buffer3, index);
            destination.CopyTo(buffer3, 0x36);
            FileStream stream = new FileStream(FileName, FileMode.Create);

            stream.Write(buffer3, 0, buffer3.Length);
            stream.Flush();
            stream.Close();
            buffer3 = null;
            DeleteObject(SelectObject(hDC, ptr7));
            DeleteDC(hDC);
            ReleaseDC(dC);
        }
Ejemplo n.º 2
0
 public static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFOHEADER hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);