public static System.Drawing.Bitmap CaptureScreen(rtaNetworking.Windows.Screen thisScreen, bool CaptureMouse) { System.Drawing.Bitmap result = new System.Drawing.Bitmap(thisScreen.Bounds.Width , rtaNetworking.Windows.Screen.PrimaryScreen.Bounds.Height , System.Drawing.Imaging.PixelFormat.Format24bppRgb); try { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(result)) { g.CopyFromScreen(0, 0, 0, 0, thisScreen.Bounds.Size, System.Drawing.CopyPixelOperation.SourceCopy); if (CaptureMouse) { CURSORINFO pci; pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO)); if (GetCursorInfo(out pci)) { if (pci.flags == CURSOR_SHOWING) { DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor); g.ReleaseHdc(); } } } } } catch { result = null; } return(result); } // End Function CaptureScreen
public string TakeScreenshot() { double screenLeft = SystemParameters.VirtualScreenLeft; double screenTop = SystemParameters.VirtualScreenTop; //double screenWidth = SystemParameters.VirtualScreenWidth; //double screenHeight = SystemParameters.VirtualScreenHeight; double screenWidth = Screen.PrimaryScreen.Bounds.Width; double screenHeight = Screen.PrimaryScreen.Bounds.Height; string screenerPath = GetScreenerPath(); var files = Directory.GetFiles(screenerPath); using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)screenWidth, (int)screenHeight)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { String filename = $"{files.Length+1} - {DateTime.Now.ToString("HH_mm_ss")}.png"; g.CopyFromScreen((int)screenLeft, (int)screenTop, 0, 0, bmp.Size); string result = System.IO.Path.Combine(screenerPath, filename); bmp.Save(System.IO.Path.Combine(screenerPath, filename)); return(result); } } }
private void CreateWordDoc() { try { double screenLeft = _Window.GridMap.PointToScreen(new Point(0, 0)).X; double screenTop = _Window.GridMap.PointToScreen(new Point(0, 0)).Y; double screenWidth = _Window.GridMap.PointToScreen(new Point(_Window.GridMap.ActualWidth, _Window.GridMap.ActualHeight)).X - screenLeft; double screenHeight = _Window.GridMap.PointToScreen(new Point(_Window.GridMap.ActualWidth, _Window.GridMap.ActualHeight)).Y - screenTop; using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)screenWidth, (int)screenHeight)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { g.CopyFromScreen((int)screenLeft, (int)screenTop, 0, 0, bmp.Size); bmp.Save("pictureforDoc.png"); } } WorkWithDoc Doc = new WorkWithDoc(FoundPlaces, _categories, _сad_num_LandPlot, _BorderValue.ToString()); } catch (Exception) { //StatusBarText!!!!!! } }
public static void TakeScreenShot(string exception) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(2000, 1000); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap as System.Drawing.Image); graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); bitmap.Save(PropertiesReference.Properties.ResultPath + @"\TC_Test.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); }
private void capture_down(object sender, MouseButtonEventArgs e) { System.Drawing.Bitmap captureBitmap = new System.Drawing.Bitmap((int)border.ActualWidth - 6, (int)border.ActualHeight - 6, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Graphics captureGraphics = System.Drawing.Graphics.FromImage(captureBitmap); captureGraphics.CopyFromScreen((int)border.PointToScreen(new Point(0, 0)).X + 3, (int)border.PointToScreen(new Point(0, 0)).Y + 3, 0, 0, new System.Drawing.Size((int)border.ActualWidth - 6, (int)border.ActualHeight - 6) ); string path = capture_path + @"\Capture_" + Convert.ToDateTime(DateTime.Now).ToString("MMM_dd_yyyy_HH_mm_ss") + capture_type; if (capture_type.Contains("png")) { captureBitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png); } else if (capture_type.Contains("jpg")) { captureBitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); } else { captureBitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); } }
// // https://stackoverflow.com/questions/34139450/getwindowrect-returns-a-size-including-invisible-borders static Mat grab_screen2(Config config) { //border should be `7, 0, 7, 7` // offset var rect = new Rect(); //int width = 1280; //int height = 720; //int offset_top = 26; //int offset_left = 4; GetWindowRect(proc.MainWindowHandle, ref rect); if (rect.Right != 0 && rect.Top != 0) { Console.WriteLine("Window rect: l=" + rect.Left + ", t=" + rect.Top + ", r=" + rect.Right + ", b=" + rect.Bottom + ", w= " + rect.Width + ", h=" + rect.Height + " "); } //System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(config.Width, config.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //Console.WriteLine("Size= " + bounds.Size + " "); using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmp)) { graphics.CopyFromScreen(rect.Left + config.OffsetLeft, rect.Top + config.OffsetTop, 0, 0, new System.Drawing.Size(config.Width, config.Height), System.Drawing.CopyPixelOperation.SourceCopy); } //преобразование из bitmap (требуется подключение OpenCvSharp.Extensions) Mat res = new Mat(new Size(config.Width, config.Height), MatType.CV_8UC3); bmp.ToMat(res); bmp.Dispose(); return(res); }
private void CaptureScreen() { this.Hide(); System.Drawing.Bitmap screenshotBmp; screenshotBmp = new System.Drawing.Bitmap((int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (System.Drawing.Graphics g= System.Drawing.Graphics.FromImage(screenshotBmp)) { g.CopyFromScreen(0, 0, 0, 0, screenshotBmp.Size); } IntPtr handle = IntPtr.Zero; try { handle = screenshotBmp.GetHbitmap(); ImageBrush ib = new ImageBrush(); ib.ImageSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); ImageCanvas.Background = ib; } finally { DeleteObject(handle); } this.Show(); }
private System.Drawing.Bitmap _getDesktopImage(System.Drawing.Rectangle rect) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rect.Width, rect.Height); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.CopyFromScreen(0, 0, rect.Left, rect.Top, new System.Drawing.Size(rect.Width, rect.Height)); return(bitmap); }
bool m_mouseHooker_OnMouseActivity(System.Windows.Forms.MouseButtons button, int clicks, int x, int y, int delta, WayControls.Windows.Hook.MouseHook.MouseActiveType ActiveType) { if (this.Visibility != System.Windows.Visibility.Visible) { return(true); } if (ActiveType == WayControls.Windows.Hook.MouseHook.MouseActiveType.MouseDown) { if ((DateTime.Now - m_lastPictureTime).TotalMilliseconds > 500) { if (true) { //Point p = this.PointToScreen(new Point(0, 0)); //if (new System.Drawing.Rectangle((int)p.X, (int)p.Y, (int)this.ActualWidth, (int)this.ActualHeight).Contains(x, y)) // return true; } if (menu.IsOpen) { Point p = menu.PointToScreen(new Point(0, 0)); if (new System.Drawing.Rectangle((int)p.X, (int)p.Y, (int)menu.ActualWidth, (int)menu.ActualHeight).Contains(x, y)) { return(true); } } m_lastPictureTime = DateTime.Now; var bitmap = new System.Drawing.Bitmap((int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.CopyFromScreen(0, 0, 0, 0, bitmap.Size, System.Drawing.CopyPixelOperation.SourceCopy); g.FillEllipse(BRUSH, new System.Drawing.Rectangle(x - 30, y - 30, 60, 60)); System.Drawing.Point p = System.Windows.Forms.Cursors.Default.HotSpot; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(x - p.X, y - p.Y), System.Windows.Forms.Cursors.Default.Size); System.Windows.Forms.Cursors.Default.Draw(g, rect); } string filename = AppDomain.CurrentDomain.BaseDirectory + "BugPics\\" + Guid.NewGuid().ToString() + ".png"; bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png); bitmap.Dispose(); m_histories.Add(filename); if (m_histories.Count > 50) { try { File.Delete(m_histories[0]); } catch { } m_histories.RemoveAt(0); } } } return(true); }
public static void TakeScreenShot(string exception) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(2000, 1000); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap as System.Drawing.Image); graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); bitmap.Save(ObjectRepository.ResultPath + @"\" + exception + "_" + ObjectRepository.Testcasename + "_StepNo_" + StepNo + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); StepNo++; }
public static System.Drawing.Bitmap GetBitmap(System.Drawing.Point SourcePoint, System.Drawing.Point DestinationPoint, System.Drawing.Rectangle SelectionRectangle) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(SelectionRectangle.Width, SelectionRectangle.Height); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size); return(bitmap); }
private void testFunc() { System.Drawing.Bitmap bmpScreenshot = new System.Drawing.Bitmap(145, 23, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Graphics gfxScreenshot = System.Drawing.Graphics.FromImage(bmpScreenshot); gfxScreenshot.CopyFromScreen(32, 32, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size, System.Drawing.CopyPixelOperation.SourceCopy); bmpScreenshot.Save("pa.bmp", System.Drawing.Imaging.ImageFormat.Bmp); //System.Drawing.Color color = bmpScreenshot.GetPixel(0, 0); //byte Luminosity = (byte) (color.GetBrightness() * 255); //log.Info(Luminosity); }
} // End Sub GetScreenshot // http://jalpesh.blogspot.com/2007/06/how-to-take-screenshot-in-c.html // Tools.Graphics.ScreenShot.SaveScreenshot(@"C:\Users\Stefan.Steiger.COR\Desktop\test.jpg"); public static void SaveScreenshot(string strFileNameAndPath) { System.Drawing.Rectangle rectBounds = rtaNetworking.Windows.Screen.GetBounds(System.Drawing.Point.Empty); using (System.Drawing.Bitmap bmpScreenshotBitmap = new System.Drawing.Bitmap(rectBounds.Width, rectBounds.Height)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshotBitmap)) { g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectBounds.Size); } // End Using g bmpScreenshotBitmap.Save(strFileNameAndPath, System.Drawing.Imaging.ImageFormat.Jpeg); } // End Using } // End Sub SaveScreenshot
private void btnJietu_Click(object sender, RoutedEventArgs e) { MainWindow.mainWindow.rightTabControl.SelectedIndex = 0; float ScaleX = PrimaryScreen.ScaleX; float ScaleY = PrimaryScreen.ScaleY; bitMap = new System.Drawing.Bitmap(Convert.ToInt32(insertShape.Width * ScaleX), Convert.ToInt32(insertShape.Height * ScaleY), System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitMap); graphics.CopyFromScreen(new System.Drawing.Point(Convert.ToInt32(x_start), Convert.ToInt32(y_start)), new System.Drawing.Point(0, 0), new System.Drawing.Size(Convert.ToInt32(insertShape.Width * ScaleX), Convert.ToInt32(insertShape.Height * ScaleY)), System.Drawing.CopyPixelOperation.SourceCopy); //graphics.DrawImage(MainWindow.bitBmp, new System.Drawing.Rectangle(0, 0, Convert.ToInt32(insertShape.Width * ScaleX), Convert.ToInt32(insertShape.Height * ScaleY)), new System.Drawing.Rectangle(Convert.ToInt32(x_start*ScaleX*2), Convert.ToInt32(y_start*ScaleY*2), Convert.ToInt32(insertShape.Width * ScaleX*2), Convert.ToInt32(insertShape.Height * ScaleY*2)), System.Drawing.GraphicsUnit.Pixel); MainWindow.mainWindow.App_Exited(sender, e); }
} // End Function GetScreenshotImage // http://jalpesh.blogspot.com/2007/06/how-to-take-screenshot-in-c.html // Tools.Graphics.ScreenShot.GetScreenshot(this.PictureBox1); public static void GetScreenshot(rtaNetworking.Windows.PictureBox pbThisPictureBox) { /* * if (this.pictureBox1.Image != null) * this.pictureBox1.Image.Dispose(); */ bmpScreenshot.Dispose(); bmpScreenshot = new System.Drawing.Bitmap(rectScreenBounds.Width, rectScreenBounds.Height); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshot)) { g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectScreenBounds.Size); } // End Using g pbThisPictureBox.Image = bmpScreenshot; } // End Sub GetScreenshot
//Action protected internal string TakeScreenshot(string strFolderSave) { string format = "yyyy.MM.dd HH.mm.ss"; string ext = "png"; System.Drawing.Size screenSz = Screen.PrimaryScreen.Bounds.Size; System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(screenSz.Width, screenSz.Height); System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(screenshot); gr.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, screenSz); string filepath = System.IO.Path.Combine(strFolderSave, Form1.myForm.LocalHost.UserName + " " + DateTime.Now.ToString(format)) + "." + ext; List <System.Reflection.PropertyInfo> props = typeof(System.Drawing.Imaging.ImageFormat).GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).ToList(); var imgformat = (System.Drawing.Imaging.ImageFormat)props.Find(prop => prop.Name.ToLower() == ext).GetValue(null, null); screenshot.Save(filepath, imgformat); return(filepath); }
private static SixLabors.ImageSharp.Image TakeScreenShot(string name) { using (var bitmap = new System.Drawing.Bitmap(ScreenW, ScreenH)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, new System.Drawing.Size(ScreenW, ScreenH)); } using (var ms = new MemoryStream()) { bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); var bytes = ms.ToArray(); //File.WriteAllBytes(Path.Combine(workingPath, $"screen-{name}.png"), bytes); return(Image.Load(bytes, new PngDecoder())); } } }
/// <summary> /// Сделать снимок определённой области изображения /// </summary> /// <param name="rect">Прямоугольник в котором будет сделан снимок</param> /// <param name="format">Формат изображения</param> /// <returns></returns> public static BitmapImage CaptureRect(System.Drawing.Rectangle rect, ImageFormat format) { using (MemoryStream ms = new MemoryStream()) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, System.Drawing.CopyPixelOperation.SourceCopy); bitmap.Save(ms, format); BitmapImage img = new BitmapImage(); img.BeginInit(); img.CacheOption = BitmapCacheOption.OnLoad; img.StreamSource = ms; img.EndInit(); return(img); } }
/// <inheritdoc/> public override void DoTakeScreenshot(Issue issue) { // Get element and the window var element = (FrameworkElement)issue.Control; var host = (FrameworkElement)issue.Host; System.Drawing.Rectangle windowRect; Point point; if (host is Window) { var window = (Window)host; point = new Point(window.Left, window.Top); windowRect = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height); } else { point = host.PointToScreen(new Point(0, 0)); windowRect = new System.Drawing.Rectangle((int)point.X, (int)point.Y, (int)host.ActualWidth, (int)host.ActualHeight); } // Get screenshot bitmap System.Drawing.Bitmap screenshot = new System.Drawing.Bitmap(windowRect.Width, windowRect.Height); using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(screenshot)) { // Take the screenshot graphics.CopyFromScreen(windowRect.Left, windowRect.Top, 0, 0, screenshot.Size); // Draw highlight if needed if (HighlightIssues) { var location = host.PointToScreen(new Point(0, 0)); var dx = (int)(location.X - point.X); var dy = (int)(location.Y - point.Y); System.Drawing.Rectangle highlightRect = new System.Drawing.Rectangle(issue.HightlightRect.Left + dx, issue.HightlightRect.Top + dy, issue.HightlightRect.Width, issue.HightlightRect.Height); highlightRect.Inflate(HighlightMargin, HighlightMargin); graphics.DrawRectangle(new System.Drawing.Pen(GetHighlightColor(issue.IssueType), HighlightWidth), highlightRect); graphics.Flush(); } } // Assign the screenshot to the issue issue.Screenshot = screenshot; }
protected void EventCapture(object sender, System.EventArgs e) { int Width = _bounds.Width; int Height = _bounds.Height; using (System.Drawing.Bitmap target = new System.Drawing.Bitmap(Width, Height)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(target)) { g.CopyFromScreen(new System.Drawing.Point(_bounds.Left, _bounds.Top), System.Drawing.Point.Empty, new System.Drawing.Size(Width, Height), System.Drawing.CopyPixelOperation.SourceCopy); } using (var ms = new System.IO.MemoryStream()) { target.Save(ms, System.Drawing.Imaging.ImageFormat.Png); _gifEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(ms, System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat, System.Windows.Media.Imaging.BitmapCacheOption.OnLoad)); } } }
public static System.Drawing.Bitmap ScreenShot() { var window = Window.Instance; var bmp = new System.Drawing.Bitmap(window.ClientSize.Width, window.ClientSize.Height); var pos = window.PointToScreen(new System.Drawing.Point(0, 0)); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) { g.CopyFromScreen(pos.X, pos.Y, 0, 0, window.ClientSize, System.Drawing.CopyPixelOperation.SourceCopy); // ビット・ブロック転送方式の切り替え例: //g.FillRectangle(Brushes.LightPink, // 0, 0, rc.Width, rc.Height); //g.CopyFromScreen(rc.X, rc.Y, 0, 0, // rc.Size, CopyPixelOperation.SourceAnd); } return(bmp); }
// Caputre the screen and save it as a jpeg in sigBank with same file path as fileDest private void CaptureScreen(string fileDest) { //get the virtual screen dimensions w/o left and right navigation grid columns double screenLeft = (1.4) * NavGrid.ActualWidth; double screenTop = SystemParameters.VirtualScreenTop + NavGrid.ActualWidth / 2; double screenWidth = canvas.ActualWidth - (1.2) * settingsGrid.ActualWidth; double screenHeight = SystemParameters.VirtualScreenHeight - NavGrid.ActualWidth; using (System.Drawing.Bitmap bmap = new System.Drawing.Bitmap((int)screenWidth, (int)screenHeight)) { using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmap)) { graphics.CopyFromScreen((int)screenLeft, (int)screenTop, 0, 0, bmap.Size); } //save jpeg of ink file string imgFilePath = fileDest.Replace("\\ink", "\\img"); imgFilePath = imgFilePath.Replace(".isf", ".jpg"); bmap.Save(imgFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); } }
private void FinishPic_OnClick(object sender, MouseButtonEventArgs e) { if (FilePathReal.Text == "" || FilePathReal.Text == null) { return; } #region 裁剪图片 System.Windows.Forms.Screen current_Screen = System.Windows.Forms.Screen.FromHandle(hwnd); double width = (Int16)Trans2RightPixelX(180); double height = (Int16)Trans2RightPixelY(180); int offset = (Int16)Trans2RightPixelY(17); int current_Screen_Physical_X = current_Screen.WorkingArea.X + (current_Screen.WorkingArea.Width - (int)width) / 2; int current_Screen_Physical_Y = current_Screen.WorkingArea.Y + (current_Screen.WorkingArea.Height - (int)height) / 2 - offset; double x = current_Screen_Physical_X; double y = current_Screen_Physical_Y; try { System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap((int)width, (int)height); System.Drawing.Size s = new System.Drawing.Size(memoryImage.Width, memoryImage.Height); System.Drawing.Graphics memoryGraphics = System.Drawing.Graphics.FromImage(memoryImage); memoryGraphics.CopyFromScreen((int)x, (int)y, 0, 0, s); string tempPath = Utils.GetCurConfigPath(); string fullFileNamePath = tempPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"; memoryImage.Save(fullFileNamePath, ImageFormat.Jpeg); EVSdkManager.Instance.UploadUserImage(fullFileNamePath); ProgressDialog.Instance.ShowDialog(); } catch (Exception ex) { log.InfoFormat("Failed to upload avatar, exception: {0}", ex); } #endregion 裁剪图片 }
public static string GetScreenShot() { string savePath = string.Empty; StackTrace stackTrace = new StackTrace(); StackFrame[] stackFrame = stackTrace.GetFrames(); var method = stackFrame[2].GetMethod(); savePath = @"C:\Results" + string.Format("\\{0}.jpg", method.Name + dateTimeStamp); System.Drawing.Rectangle bounds = System.Windows.Forms.Screen.GetBounds(System.Drawing.Point.Empty); using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bounds.Width, bounds.Height)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size); } bitmap.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); } return(savePath); }
} // End Function GetScrBounds // http://jalpesh.blogspot.com/2007/06/how-to-take-screenshot-in-c.html // Tools.Graphics.ScreenShot.GetScreenshot(); public static System.Drawing.Bitmap GetScreenshot() { /* * if (this.pictureBox1.Image != null) * this.pictureBox1.Image.Dispose(); */ // if(bmpScreenshot != null) bmpScreenshot.Dispose(); bmpScreenshot = new System.Drawing.Bitmap(rectScreenBounds.Width, rectScreenBounds.Height); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmpScreenshot)) { try { g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectScreenBounds.Size); } catch (System.Exception ex) { } } // End Using g return(bmpScreenshot); } // End Function GetScreenshotImage
public static System.Drawing.Bitmap CopyFromScreen(int x, int y, int width = -1, int height = -1, System.Windows.Forms.Screen screen = null) { if (screen == null) { screen = System.Windows.Forms.Screen.PrimaryScreen; } int widthX = screen.Bounds.Width; int heightX = screen.Bounds.Height; if (width == -1) { width = widthX - x; } if (height == -1) { height = heightX - y; } System.Drawing.Bitmap result = new System.Drawing.Bitmap(width, height); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(result)) { g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height), System.Drawing.CopyPixelOperation.SourceCopy); } return(result); }
void PrintScreen(object sender, PrintScreenEventArgs eventsArgs) { try { int border = (Bounds.Right - Bounds.Left - Width) / 2; int titleborder = Bounds.Bottom - Bounds.Top - Height - border; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(this.Width, this.Height); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.CopyFromScreen(new System.Drawing.Point() { X = Location.X + border, Y = Location.Y + titleborder }, new System.Drawing.Point() { X = this.ClientRectangle.Left, Y = ClientRectangle.Top }, new System.Drawing.Size(this.Size.Width, this.Size.Height)); bitmap.Save(eventsArgs.Path + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } catch (ObjectDisposedException) { PrintScreenMaker.PrintScreenCommand -= PrintScreen; } }
// Worker thread private void WorkerThread( ) { int width = region.Width; int height = region.Height; int x = region.Location.X; int y = region.Location.Y; System.Drawing.Size size = region.Size; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); // download start time and duration DateTime start; TimeSpan span; while (!stopEvent.WaitOne(0, false)) { // set dowbload start time start = DateTime.Now; try { // capture the screen graphics.CopyFromScreen(x, y, 0, 0, size, System.Drawing.CopyPixelOperation.SourceCopy); // increment frames counter framesReceived++; // provide new image to clients if (NewFrame != null) { // notify client NewFrame(this, new NewFrameEventArgs(bitmap)); } // wait for a while ? if (frameInterval > 0) { // get download duration span = DateTime.Now.Subtract(start); // miliseconds to sleep int msec = frameInterval - (int)span.TotalMilliseconds; if ((msec > 0) && (stopEvent.WaitOne(msec, false))) { break; } } } catch (ThreadAbortException) { break; } catch (Exception exception) { // provide information to clients if (VideoSourceError != null) { VideoSourceError(this, new VideoSourceErrorEventArgs(exception.Message)); } // wait for a while before the next try Thread.Sleep(250); } // need to stop ? if (stopEvent.WaitOne(0, false)) { break; } } // release resources graphics.Dispose( ); bitmap.Dispose( ); if (PlayingFinished != null) { PlayingFinished(this, ReasonToFinishPlaying.StoppedByUser); } }
public static System.Drawing.Bitmap CreateSingleScreenshot(rtaNetworking.Windows.Screen thisScreen, bool showCursor) { System.Drawing.Rectangle bounds = thisScreen.Bounds; int width = bounds.Width; int height = bounds.Height; System.Drawing.Size size = new System.Drawing.Size(width, height); System.Drawing.Bitmap srcImage = new System.Drawing.Bitmap(size.Width, size.Height); System.Drawing.Graphics srcGraphics = System.Drawing.Graphics.FromImage(srcImage); bool scaled = (width != size.Width || height != size.Height); System.Drawing.Bitmap dstImage = srcImage; System.Drawing.Graphics dstGraphics = srcGraphics; if (scaled) { dstImage = new System.Drawing.Bitmap(width, height); dstGraphics = System.Drawing.Graphics.FromImage(dstImage); } // End if (scaled) System.Drawing.Rectangle src = new System.Drawing.Rectangle(0, 0, size.Width, size.Height); System.Drawing.Rectangle dst = new System.Drawing.Rectangle(0, 0, width, height); System.Drawing.Size curSize = new System.Drawing.Size(32, 32); //srcGraphics.CopyFromScreen(0, 0, 0, 0, size); srcGraphics.CopyFromScreen( thisScreen.WorkingArea.Left , thisScreen.WorkingArea.Top // Top is bottom... , 0, 0, size ); /* * // This results in the wrong cursor... * if (showCursor) * // System.Windows.Forms.Cursors.Default.Draw(srcGraphics, * System.Windows.Forms.Cursor.Current.Draw(srcGraphics, * new System.Drawing.Rectangle(System.Windows.Forms.Cursor.Position, curSize) * ); */ // https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis if (showCursor) { CURSORINFO pci; pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO)); if (GetCursorInfo(out pci)) { if (pci.flags == CURSOR_SHOWING) { // Check if cursor on the screen that is being captured... if (pci.ptScreenPos.x >= thisScreen.WorkingArea.Left && pci.ptScreenPos.x <= thisScreen.WorkingArea.Right) { DrawIcon(srcGraphics.GetHdc(), pci.ptScreenPos.x - thisScreen.WorkingArea.Left, pci.ptScreenPos.y, pci.hCursor); srcGraphics.ReleaseHdc(); } // End if (pci.ptScreenPos.x >= thisScreen.Bounds.X && pci.ptScreenPos.x <= thisScreen.Bounds.X + thisScreen.Bounds.Width) } // End if (pci.flags == CURSOR_SHOWING) } // End if (GetCursorInfo(out pci)) } // End if (showCursor) if (scaled) { dstGraphics.DrawImage(srcImage, dst, src, System.Drawing.GraphicsUnit.Pixel); } return(dstImage); }
private void ConsoleInput_KeyPressed(KeyEventArgs e) { if (e.Key == ConsoleKey.Escape) { Exit(); } bool leftControl = e.ControlKeyState.HasFlag(ControlKeyState.LeftCtrlPressed); if (e.Key == ConsoleKey.S && leftControl) { var area = ClientArea; var size = area.Size; using (System.Drawing.Bitmap b = new System.Drawing.Bitmap(size.X, size.Y, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b)) { g.CopyFromScreen(area.UpperLeft, new System.Drawing.Point(0, 0), new System.Drawing.Size(size)); b.Save("Test.png"); } } textBox.BeginUpdate(); if (leftControl || e.ControlKeyState.HasFlag(ControlKeyState.ShiftPressed)) { switch (e.Key) { case ConsoleKey.UpArrow: case ConsoleKey.W: textBox.Height--; break; case ConsoleKey.LeftArrow: case ConsoleKey.A: textBox.Width--; break; case ConsoleKey.DownArrow: case ConsoleKey.S: textBox.Height++; break; case ConsoleKey.RightArrow: case ConsoleKey.D: textBox.Width++; break; default: break; } } else { switch (e.Key) { case ConsoleKey.UpArrow: case ConsoleKey.W: textBox.Top--; break; case ConsoleKey.LeftArrow: case ConsoleKey.A: textBox.Left--; break; case ConsoleKey.DownArrow: case ConsoleKey.S: textBox.Top++; break; case ConsoleKey.RightArrow: case ConsoleKey.D: textBox.Left++; break; default: break; } } //Fill if (e.Key == ConsoleKey.F) { if (leftControl) { textBox.Left = 0; textBox.Top = 0; textBox.Width = Width; textBox.Height = Height; } else { UpdateColor(); } } //Reset if (e.Key == ConsoleKey.R && leftControl) { textBox.Left = 0; textBox.Top = 0; textBox.Width = 34; textBox.Height = 13; } //Center if (e.Key == ConsoleKey.C && leftControl) { textBox.Left = Width / 2 - textBox.Width / 2; textBox.Top = Height / 2 - textBox.Height / 2; } textBox.EndUpdate(); dataBox.BeginUpdate(); string line1 = "Location: " + textBox.Rectangle.UpperLeft; string line2 = "Size:" + textBox.Rectangle.Size; dataBox.Text = $"{line1} {line2}"; dataBox.Width = Math.Max(line1.Length, line2.Length); dataBox.EndUpdate(); }