четверг, 29 января 2009 г.

Как сделать скриншот программы?

How to take ScreenShot in C# or Visual Basic?

C#
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

private string TakeScreenShotFileName()
    {
      string screenshot = null;

      if (Application.OpenForms.Count > 0)
      {
        Bitmap bmp;
        Graphics gr;
        Rectangle bounds = Application.OpenForms[0].Bounds;

        screenshot = System.IO.Path.GetTempFileName();

        bmp = new Bitmap(bounds.Width, bounds.Height);

        gr = Graphics.FromImage(bmp);
        gr.CopyFromScreen(Application.OpenForms[0].Location, Point.Empty, bounds.Size);

        bmp.Save(screenshot, ImageFormat.Jpeg);
      }

      return screenshot ?? "";
    }


* This source code was highlighted with Source Code Highlighter.


VB
Imports System.Drawing
    Private Function TakeScreenshotFileName() As String
      Dim bounds As Rectangle = My.Application.MainForm.Bounds
      Dim fileNAme As String = Nothing

      Using b As New Bitmap(bounds.Width, bounds.Height)
        Using g = Graphics.FromImage(b)
          g.CopyFromScreen(My.Application.MainForm.Location, Point.Empty, bounds.Size)
        End Using
        fileNAme = System.IO.Path.GetTempFileName()
        b.Save(fileNAme, Imaging.ImageFormat.Jpeg)
      End Using
      Return fileName
    End Function


* This source code was highlighted with Source Code Highlighter.