// Get the image's bounds. public static Rectangle ImageBounds(Bitmap32 bm32) { // ymin. int ymin = bm32.Height - 1; for (int y = 0; y < bm32.Height; y++) { if (!RowIsWhite(bm32, y)) { ymin = y; break; } } // ymax. int ymax = 0; for (int y = bm32.Height - 1; y >= ymin; y--) { if (!RowIsWhite(bm32, y)) { ymax = y; break; } } // xmin. int xmin = bm32.Width - 1; for (int x = 0; x < bm32.Width; x++) { if (!ColumnIsWhite(bm32, x)) { xmin = x; break; } } // xmax. int xmax = 0; for (int x = bm32.Width - 1; x >= xmin; x--) { if (!ColumnIsWhite(bm32, x)) { xmax = x; break; } } // Build the rectangle. return(new Rectangle(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)); }
// Return true if this column is all white. private static bool ColumnIsWhite(Bitmap32 bm32, int x) { byte r, g, b, a; int count = 0; for (int y = 0; y < bm32.Height; y++) { bm32.GetPixel(x, y, out r, out g, out b, out a); if (((r < 250) || (g < 250) || (b < 250)) && ((r > 2) || (g > 2) || (b > 2))) { count++; } if (count > bm32.Height / 10) { return(false); } } return(true); }
// Return true if this row is all white. private static bool RowIsWhite(Bitmap32 bm32, int y) { byte r, g, b, a; int count = 0; for (int x = 0; x < bm32.Width; x++) { bm32.GetPixel(x, y, out r, out g, out b, out a); if (((r < 250) || (g < 250) || (b < 250)) && ((r > 2) || (g > 2) || (b > 2))) { count++; } if (count > bm32.Width / 10) { return(false); } } return(true); }
private void CaptureScaledScreen(object state) { try { var count = Interlocked.Increment(ref executionCount); var size = new System.Drawing.Size((int)System.Windows.SystemParameters.WorkArea.Width, (int)System.Windows.SystemParameters.WorkArea.Height); using (var screenBmp = new Bitmap(size.Width, size.Height)) { var destRect = new Rectangle(0, 0, this.width, this.height); var destImage = new Bitmap(this.width, this.height); using (Graphics screenGraphics = Graphics.FromImage(screenBmp), destGraphics = Graphics.FromImage(destImage) ) { screenGraphics.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), size); Bitmap32 bm32 = new Bitmap32(screenBmp); bm32.LockBitmap(); Rectangle src_rect = Bitmap32.ImageBounds(bm32); bm32.UnlockBitmap(); // Copy the non-white area. int wid = src_rect.Width; int hgt = src_rect.Height; Bitmap bm = new Bitmap(wid, hgt); using (Graphics gr = Graphics.FromImage(bm)) { gr.Clear(Color.White); Rectangle dest_rect = new Rectangle( 0, 0, src_rect.Width, src_rect.Height); gr.DrawImage(screenBmp, dest_rect, src_rect, GraphicsUnit.Pixel); } destGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; destGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic; //destGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; using (var wrapMode = new System.Drawing.Imaging.ImageAttributes()) { wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); destGraphics.DrawImage(bm, destRect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, wrapMode); } //destImage.Save("test.png"); } this.ExtractColors(destImage); } } catch (Exception) { } }