Beispiel #1
0
        public MainWindow() // лучше не трогать XD
        {
            m_Bmh.Init();
            m_Bmh.biPlanes      = 1;
            m_Bmh.biBitCount    = 24;
            m_Bmh.biCompression = 0;
            m_Bmh.biHeight      = 100;
            m_Bmh.biWidth       = 100;

            m_Bitmap = (IntPtr)0;

            InitializeComponent();
        }
Beispiel #2
0
        public static bool WriteImage(Texture2D image)
        {
            if (!isClipboardSupported(Format.Image) || image == null)
            {
                return(false);
            }

#if WINDOWS
            var    pixelData = image.GetPixels32();
            byte[] bytes;

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    var info = new BITMAPINFOHEADER();
                    info.Init();
                    info.biWidth         = image.width;
                    info.biHeight        = image.height;
                    info.biPlanes        = 1;
                    info.biBitCount      = 32;
                    info.biCompression   = BitmapCompressionMode.BI_RGB;
                    info.biSizeImage     = (uint)pixelData.Length;
                    info.biXPelsPerMeter = 1;
                    info.biYPelsPerMeter = 1;
                    info.biClrUsed       = 0;
                    info.biClrImportant  = 0;

                    byte[]   buffer   = new byte[info.biSize];
                    GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    Marshal.StructureToPtr(info, gcHandle.AddrOfPinnedObject(), true);
                    writer.Write(buffer);
                    gcHandle.Free();

                    foreach (var pixel in pixelData)
                    {
                        uint n = (uint)(pixel.b + (pixel.g << 8) + (pixel.r << 16) + (pixel.a << 24));
                        writer.Write(n);
                    }
                }

                bytes = stream.ToArray();
            }


            return(PushDataToClipboard(bytes, CF_DIB));
#else
            return(false);
#endif
        }
Beispiel #3
0
        public IntPtr update(byte[] bgra, int width, int height, bool topRowFirst = false)
        {
            var bitmap           = CreateBitmap(width, height, 1, 32, null);
            var dc               = GetDC(IntPtr.Zero);
            BITMAPINFOHEADER bmi = new BITMAPINFOHEADER();

            bmi.Init();
            bmi.biWidth    = width;
            bmi.biHeight   = topRowFirst ? -height : height;
            bmi.biPlanes   = 1;
            bmi.biBitCount = 32;
            SetDIBits(dc, bitmap, 0, (uint)height, bgra, ref bmi, 0);
            ReleaseDC(IntPtr.Zero, dc);
            //
            var inf = new ICONINFO();

            inf.IsIcon      = true;
            inf.ColorBitmap = bitmap;
            inf.MaskBitmap  = bitmap;
            //
            var icon = CreateIconIndirect(ref inf);

            if (icon == IntPtr.Zero)
            {
                DeleteObject(bitmap);
                return(IntPtr.Zero);
            }
            //
            if (this.bitmap != IntPtr.Zero)
            {
                DeleteObject(this.bitmap);
            }
            this.bitmap = bitmap;
            if (this.icon != IntPtr.Zero)
            {
                DestroyIcon(this.icon);
            }
            this.icon = icon;
            //
            return(icon);
        }
 public static WriteableBitmap Capture()
 {
     try
     {
         IntPtr hMonitor = MonitorFromPoint(new POINT(), MonitorOptions.MONITOR_DEFAULTTOPRIMARY);
         Debug.Assert(hMonitor != IntPtr.Zero);
         MONITORINFO mi = new MONITORINFO();
         mi.cbSize = (uint)Marshal.SizeOf(mi);
         bool bln = GetMonitorInfo(hMonitor, ref mi);
         Debug.Assert(bln);
         int W = mi.rcMonitor.right - mi.rcMonitor.left;
         int H = mi.rcMonitor.bottom - mi.rcMonitor.top;
         BITMAPINFOHEADER bmi = new BITMAPINFOHEADER();
         bmi.Init(W, H, 32);
         IntPtr hdcDesktop = GetDC(IntPtr.Zero);
         Debug.Assert(hdcDesktop != IntPtr.Zero);
         try
         {
             IntPtr hdcMem = CreateCompatibleDC(hdcDesktop);
             Debug.Assert(hdcMem != IntPtr.Zero);
             try
             {
                 IntPtr ppvBits = IntPtr.Zero;
                 IntPtr hbm = CreateDIBSection(hdcMem, ref bmi, 0, out ppvBits, IntPtr.Zero, 0);
                 Debug.Assert(hbm != IntPtr.Zero);
                 try
                 {
                     Debug.Assert(ppvBits != IntPtr.Zero);
                     IntPtr hOldObj = SelectObject(hdcMem, hbm);
                     bln = BitBlt(hdcMem, 0, 0, W, H, hdcDesktop, mi.rcMonitor.left, mi.rcMonitor.top, 0x00CC0020 /* SRCCOPY */);
                     Debug.Assert(bln);
                     GdiFlush();
                     ReleaseDC(IntPtr.Zero, hdcDesktop);
                     hdcDesktop = IntPtr.Zero;
                     WriteableBitmap wb = new WriteableBitmap(W, H);
                     int[] buf = wb.Pixels;
                     for (int ofs = 0; ofs < H; ++ofs)
                     {
                         Marshal.Copy(new IntPtr(ppvBits.ToInt64() + 4 * W * ofs), buf, (H - 1 - ofs) * W, W);
                     }
                     DeleteDC(hdcMem);
                     hdcMem = IntPtr.Zero;
                     DeleteObject(hbm);
                     hbm = IntPtr.Zero;
                     return wb;
                 }
                 finally
                 {
                     if(hbm != IntPtr.Zero)
                     {
                         DeleteObject(hbm);
                     }
                 }
             }
             finally
             {
                 if(hdcMem != IntPtr.Zero)
                 {
                     DeleteDC(hdcMem);
                 }
             }
         }
         finally
         {
             if(hdcDesktop != IntPtr.Zero)
             {
                 ReleaseDC(IntPtr.Zero, hdcDesktop);
             }
         }
     }
     catch(Exception e)
     {
         for (Exception r = e; r != null; r = r.InnerException)
         {
             Debug.WriteLine(r.Message);
             Debug.WriteLine(r.StackTrace);
         }
         return null;
     }
 }
 public void Init(int width, int height, int bpp)
 {
     bmiHeader = new BITMAPINFOHEADER();
     bmiHeader.Init(width, height, bpp);
 }
 public void Init()
 {
     bmiHeader = new BITMAPINFOHEADER();
     bmiHeader.Init();
 }