private void AutoCropImage() { Bitmap bmp = ImageHelpers.AutoCropImage((Bitmap)Form.Canvas); if (bmp != null) { UpdateCanvas(bmp); } }
public Image CaptureWindowTransparent(IntPtr handle) { if (handle.ToInt32() > 0) { Rectangle rect = CaptureHelpers.GetWindowRectangle(handle); if (CaptureShadow && !NativeMethods.IsZoomed(handle) && NativeMethods.IsDWMEnabled()) { rect.Inflate(ShadowOffset, ShadowOffset); Rectangle intersectBounds = Screen.AllScreens.Select(x => x.Bounds).Where(x => x.IntersectsWith(rect)).Combine(); rect.Intersect(intersectBounds); } Bitmap whiteBackground = null, blackBackground = null, whiteBackground2 = null; CursorData cursorData = null; bool isTransparent = false, isTaskbarHide = false; try { if (AutoHideTaskbar) { isTaskbarHide = NativeMethods.SetTaskbarVisibilityIfIntersect(false, rect); } if (CaptureCursor) { try { cursorData = new CursorData(); } catch (Exception e) { DebugHelper.WriteException(e, "Cursor capture failed."); } } using (Form form = new Form()) { form.BackColor = Color.White; form.FormBorderStyle = FormBorderStyle.None; form.ShowInTaskbar = false; form.StartPosition = FormStartPosition.Manual; form.Location = new Point(rect.X, rect.Y); form.Size = new Size(rect.Width, rect.Height); NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNoActivate); if (!NativeMethods.SetWindowPos(form.Handle, handle, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE)) { form.Close(); DebugHelper.WriteLine("Transparent capture failed. Reason: SetWindowPos fail."); return(CaptureWindow(handle)); } Thread.Sleep(10); Application.DoEvents(); whiteBackground = (Bitmap)CaptureRectangleNative(rect); form.BackColor = Color.Black; Application.DoEvents(); blackBackground = (Bitmap)CaptureRectangleNative(rect); form.BackColor = Color.White; Application.DoEvents(); whiteBackground2 = (Bitmap)CaptureRectangleNative(rect); form.Close(); } Bitmap transparentImage; if (ImageHelpers.IsImagesEqual(whiteBackground, whiteBackground2)) { transparentImage = CreateTransparentImage(whiteBackground, blackBackground); isTransparent = true; } else { DebugHelper.WriteLine("Transparent capture failed. Reason: Images not equal."); transparentImage = whiteBackground2; } if (cursorData != null) { cursorData.DrawCursor(transparentImage, rect.Location); } if (isTransparent) { transparentImage = ImageHelpers.AutoCropImage(transparentImage); if (!CaptureShadow) { TrimShadow(transparentImage); } } return(transparentImage); } finally { if (isTaskbarHide) { NativeMethods.SetTaskbarVisibility(true); } if (whiteBackground != null) { whiteBackground.Dispose(); } if (blackBackground != null) { blackBackground.Dispose(); } if (isTransparent && whiteBackground2 != null) { whiteBackground2.Dispose(); } } } return(null); }
public override Bitmap Apply(Bitmap bmp) { return(ImageHelpers.AutoCropImage(bmp, false, Sides)); }
public override Image Apply(Image img) { return(ImageHelpers.AutoCropImage((Bitmap)img, false, Sides)); }