public Raw_Image GetScreen(Size sz) { IntPtr hOldBmp = IntPtr.Zero; Raw_Image raw = new Raw_Image(); try { var dt = DateTime.Now; if (nDesk == IntPtr.Zero) { nDesk = PInvoke.GetDesktopWindow(); } if (nSrce == IntPtr.Zero) { nSrce = PInvoke.GetWindowDC(nDesk); } if (nDest == IntPtr.Zero) { nDest = PInvoke.CreateCompatibleDC(nSrce); } if (nBmp == IntPtr.Zero) { nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); } else if (sz.Height != PreviousSize.Height || sz.Width != PreviousSize.Width) {// user changed resolution.. get new bitmap PInvoke.DeleteObject(nBmp); nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); } PreviousSize = sz; hOldBmp = PInvoke.SelectObject(nDest, nBmp); bool b = PInvoke.BitBlt(nDest, 0, 0, sz.Width, sz.Height, nSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Desktop_Sharing_Shared.Screen.PInvoke.BITMAPINFO bi = new PInvoke.BITMAPINFO(); bi.bmiHeader.biSize = 40; bi.bmiHeader.biWidth = sz.Width; bi.bmiHeader.biHeight = -sz.Height; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = Desktop_Sharing_Shared.Screen.PInvoke.BitmapCompressionMode.BI_RGB; bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; var dwBmpSize = ((sz.Width * bi.bmiHeader.biBitCount + 31) / 32) * 4 * sz.Height; raw.Data = new byte[dwBmpSize]; raw.Dimensions = new Rectangle(0, 0, sz.Width, sz.Height); Desktop_Sharing_Shared.Screen.PInvoke.GetDIBits(nSrce, nBmp, 0, Convert.ToUInt32(sz.Height), raw.Data, ref bi, PInvoke.DIB_Color_Mode.DIB_RGB_COLORS); //unsafe //{ // fixed(byte* datb = raw.Data) // { // using(Bitmap image = new Bitmap(sz.Width, sz.Height, sz.Width * 4, // PixelFormat.Format32bppRgb, new IntPtr(datb))) // { // image.Save(@"C:\Users\scott\Desktop\text.png", ImageFormat.Png); // } // } //} //Bitmap bmp = Bitmap.FromHbitmap(nBmp); PInvoke.SelectObject(nDest, hOldBmp); // return bmp; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } // return new Bitmap(sz.Width, sz.Height); return(raw); }
public Raw_Image GetScreen(Size sz) { IntPtr hOldBmp = IntPtr.Zero; Raw_Image raw = new Raw_Image(); try { var dt = DateTime.Now; if(nDesk == IntPtr.Zero) nDesk = PInvoke.GetDesktopWindow(); if(nSrce == IntPtr.Zero) nSrce = PInvoke.GetWindowDC(nDesk); if(nDest == IntPtr.Zero) nDest = PInvoke.CreateCompatibleDC(nSrce); if(nBmp == IntPtr.Zero) nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); else if(sz.Height != PreviousSize.Height || sz.Width != PreviousSize.Width) {// user changed resolution.. get new bitmap PInvoke.DeleteObject(nBmp); nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); } PreviousSize = sz; hOldBmp = PInvoke.SelectObject(nDest, nBmp); bool b = PInvoke.BitBlt(nDest, 0, 0, sz.Width, sz.Height, nSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Desktop_Sharing_Shared.Screen.PInvoke.BITMAPINFO bi = new PInvoke.BITMAPINFO(); bi.bmiHeader.biSize = 40; bi.bmiHeader.biWidth = sz.Width; bi.bmiHeader.biHeight = -sz.Height; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = Desktop_Sharing_Shared.Screen.PInvoke.BitmapCompressionMode.BI_RGB; bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; var dwBmpSize = ((sz.Width * bi.bmiHeader.biBitCount + 31) / 32) * 4 * sz.Height; raw.Data = new byte[dwBmpSize]; raw.Dimensions = new Rectangle(0, 0, sz.Width, sz.Height); Desktop_Sharing_Shared.Screen.PInvoke.GetDIBits(nSrce, nBmp, 0, Convert.ToUInt32(sz.Height), raw.Data, ref bi, PInvoke.DIB_Color_Mode.DIB_RGB_COLORS); //unsafe //{ // fixed(byte* datb = raw.Data) // { // using(Bitmap image = new Bitmap(sz.Width, sz.Height, sz.Width * 4, // PixelFormat.Format32bppRgb, new IntPtr(datb))) // { // image.Save(@"C:\Users\scott\Desktop\text.png", ImageFormat.Png); // } // } //} //Bitmap bmp = Bitmap.FromHbitmap(nBmp); PInvoke.SelectObject(nDest, hOldBmp); // return bmp; } catch(Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } // return new Bitmap(sz.Width, sz.Height); return raw; }