2013-05-05 86 views
2

首先,對於長期問題抱歉,但我想確保我包括我到目前爲止所做的所有事情。捕獲並保存屏幕截圖,你點擊 - Windows C#

我想爲教程小組製作一個C#Windows應用程序,它將用作他們執行無聊手動任務的替代方法,按Alt + PrtSc鍵顯示執行教程步驟時出現的每個窗口並將其粘貼ms paint將圖像保存到文件夾,以便稍後將其插入教程文檔中。

捕獲桌面快照或僅部分桌面快照的方法有多種。然而,我甚至可以設法在我的WinForms應用程序中拍攝控件的快照;只要您點擊並保存它就會捕捉任何窗口的屏幕截圖(以及鼠標指針),結果會變得有點棘手。

我碰到過this的帖子,其中詳細介紹了使用Win32 API捕獲和保存屏幕截圖。 Thisthis這篇文章討論瞭如何通過僅使用.NET Framework來保存桌面的一部分,它運行良好,但它不完全是我所需要的。我遇到了一些免費軟件和其他商業軟件,它們也做了很多和一些這樣的事情,但我更願意做一些簡單和定製的事情。

現在,我的表單有一個瀏覽按鈕來選擇一個文件夾(將圖像保存到)和另一個名爲START的按鈕。點擊時名稱變爲STOP,並保持壓低狀態(直到再次點擊停止)。

假設,團隊必須整理軟件的設置和安裝教程,嚮導的歡迎屏幕已啓動。隨着應用程序啓動,隨着您繼續單擊按鈕,如繼續,我接受,下一步...下一步和完成,應該保存安裝嚮導的每個窗口的圖像(連同鼠標指針)。

我希望我能解釋清楚。任何幫助將不勝感激。提前致謝。

  • GJ
+0

BTW ...令我驚訝的是(在進一步閱讀之後),我在Win7中遇到了一個名爲psr.exe的內置工具,它完全符合我上面提到的內容,但它將輸出保存在.mht中,並且圖像在該區域附近有一些華麗的圖形點擊鼠標的地方。 – 2013-05-05 07:14:24

回答

0

我使用AutoHotkey的腳本觸發GadWin(http://www.gadwin.com/printscreen/)注意Gadwin是免費的僅用於個人使用解決了類似的問題。商業許可證可以購買(25美元)

我也有一些C#代碼,你可能喜歡,這是免費的。

AutoHotKey腳本: #SingleInstance Force ;點擊屏幕 - 每次點擊都是一個新的屏幕截圖 ;記錄每次點擊鼠標的屏幕截圖 ;用於Gadwin或由按鍵觸發的其他屏幕捕獲,例如{PRINTSCREEN} ; 〜意爲通過點擊 ;開啓和關閉

; open destination folder 
destinationFolder:=GadWinDestinationFolder() 
ifExist %destinationFolder% 
{ 
    Run explorer.exe %destinationFolder% 
    sleep 1000 
} 

;; shows an opening message 
; TBD could be on a message box to let user choose/acknowledge 
ShowStatus("Capture is Enabled.`n<Pause/Break> to toggle on/off", 10000) 

; *** ,% <space> func() is autohotkey magic for interpolate a function here *** 
RunWait,% GadWinExe() 
sleep 5000 

capturing:=true  


Return 

; ~ means pass the click & position through to the system 
; This allows drag screens to work, while Send LButton does not 
; TBS look at MouseGetPos + MouseClickDrag 
~LButton:: 
    Capture() 
    ;Send {LButton} 
    return 

~RButton:: 
    Capture() 
    ;Send {RButton} 
    Return 

Capture() 
{ 
    global capturing 
    if (capturing) 
    { 
     ;ClipSaved:=ClipboardAll ; Save the entire clipboard to a variable 

     ; this may be whacking the clipboard 
     ; alt -prt scrn 

     Send {PRINTSCREEN} 

     ShowStatus("Captured to " GadWinDestinationFolder() "`nPress <Pause/Break> to toggle.", 3000) 

     ;Clipboard:=ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll). 
     ;ClipSaved:=   ; Free the memory in case the clipboard was very large. 
    } 
    Else 
    { 
     dp("Capture is off`nPress <Pause/Break> to toggle.") 
    } 
    Return 
} 


PAUSE:: CaptureToggle(2000) 

CaptureToggle(delay) 
{ 
    global capturing 

    Suspend OFF 

    capturing:=!capturing 

    If (capturing) 
    { 
     ShowStatus("Capture Enabled.", delay) 
    } 
    Else 
    { 
     ShowStatus("Capture Disabled.", delay) 
     ;Suspend 
    } 

    Return 
} 

GadWinExe() 
{ 
    RegRead, exeFolder, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Gadwin PrintScreen, InstallLocation 
    Return exeFolder "\PrintScreen.exe" 
} 

GadWinDestinationFolder() 
{ 
    ; Gadwin specific 
    RegRead, CaptureDir, HKEY_CURRENT_USER, Software\Gadwin Systems\PrintScreen\Destination, CaptureDir 
    Return CaptureDir 
} 

ShowStatus(msg, time) 
{ 
    ShowTrayTip("ClickScreen", msg, time) 
} 

; Generic Show and Remove Tray Tip 
ShowTrayTip(title, msg, time) 
{ 
    ; 7/7/2011 Win Server 2008 - 30 seems to have no effect 
    TrayTip, %title%, %msg%, 30, 1 
    SetTimer, RemoveTrayTip, %time%   ; used to ensure tip is taken down 
    dp(msg) 
    Return 

; The Traytip does not disappear quickly without this 
; 7/7/2011 Win Server 2008 - tip disappears after 4 seconds anyway if the script continues to run 
RemoveTrayTip: 
    SetTimer, RemoveTrayTip, Off 
    TrayTip 
    dp("TrayTip off") 
    Return 
} 

FindAndRun(exeName) 
{ 
    ; Here I get to define the folders to search 
    ProgramFolders:="P:\Program Files,C:\Program Files,C:\Program Files (x86)" 
    Loop, parse, ProgramFolders, `, 
    { 
     IfExist, %A_LoopField%\%exeName% 
     { 
      Run %A_LoopField%\%exeName% 
      Return 
     } 
    } 
    MsgBox File %exeName% not found 
} 

dp(Msg) 
{ 
    OutputDebug %A_ScriptName% %Msg% 
} 

的C#

using System;  
using System.Drawing;  
using System.IO;  
using System.Drawing.Imaging;  
using System.Runtime.InteropServices;  

//Based on code of Agha Ali Raza (http://www.csharphelp.com/archives2/archive393.html)  

namespace Script  
{  
    public class CaptureScreen  
    {  
     const string usage = "Usage: printScreen.cs [filename]\n"+  
      "Captures screen image and saves it to a file (default file: screen.gif)\n";  

     static public void Main(string[] args)  
     {  
      if (args.Length == 1 && (args[0] == "?" || args[0] == "/?" || args[0] == "-?" || args[0].ToLower() == "help"))  
      {  
       Console.WriteLine(usage);  
      }  
      else  
      {  
       try  
       {  
        Bitmap capture = CaptureScreen.GetDesktopImage();  
        string file = Path.Combine(Environment.CurrentDirectory, "screen.gif");  
        ImageFormat format = ImageFormat.Gif;  

        if (args.Length == 1)  
        {  
         file = args[0];  
         if (args[0].ToUpper().EndsWith(".GIF"))  
          format = ImageFormat.Gif;  
         else if (args[0].ToUpper().EndsWith(".BMP"))  
          format = ImageFormat.Bmp;  
         else if (args[0].ToUpper().EndsWith(".JPEG"))  
          format = ImageFormat.Jpeg;  
         else if (args[0].ToUpper().EndsWith(".PNG"))  
          format = ImageFormat.Png;  

        }  
        capture.Save(file, format);  
       }  
       catch (Exception e)  
       {  
        Console.WriteLine(e);  
       }  
      }  
     }  

     public static Bitmap GetDesktopImage()  
     {  
      WIN32_API.SIZE size;  

      IntPtr hDC = WIN32_API.GetDC(WIN32_API.GetDesktopWindow());  
      IntPtr hMemDC = WIN32_API.CreateCompatibleDC(hDC);  

      size.cx = WIN32_API.GetSystemMetrics(WIN32_API.SM_CXSCREEN);  
      size.cy = WIN32_API.GetSystemMetrics(WIN32_API.SM_CYSCREEN);  

      m_HBitmap = WIN32_API.CreateCompatibleBitmap(hDC, size.cx, size.cy);  

      if (m_HBitmap!=IntPtr.Zero)  
      {  
       IntPtr hOld = (IntPtr) WIN32_API.SelectObject(hMemDC, m_HBitmap);  
       WIN32_API.BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC, 0, 0, WIN32_API.SRCCOPY);  
       WIN32_API.SelectObject(hMemDC, hOld);  
       WIN32_API.DeleteDC(hMemDC);  
       WIN32_API.ReleaseDC(WIN32_API.GetDesktopWindow(), hDC);  
       return System.Drawing.Image.FromHbitmap(m_HBitmap);  
      }  
      return null;  
     }  

     protected static IntPtr m_HBitmap;  
    }  

    public class WIN32_API  
    {  
     public struct SIZE  
     {  
      public int cx;  
      public int cy;  
     }  
     public const int SRCCOPY = 13369376;  
     public const int SM_CXSCREEN=0;  
     public const int SM_CYSCREEN=1;  

     [DllImport("gdi32.dll",EntryPoint="DeleteDC")]  
     public static extern IntPtr DeleteDC(IntPtr hDc);  

     [DllImport("gdi32.dll",EntryPoint="DeleteObject")]  
     public static extern IntPtr DeleteObject(IntPtr hDc);  

     [DllImport("gdi32.dll",EntryPoint="BitBlt")]  
     public static extern bool BitBlt(IntPtr hdcDest,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);  

     [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]  
     public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);  

     [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]  
     public static extern IntPtr CreateCompatibleDC(IntPtr hdc);  

     [DllImport ("gdi32.dll",EntryPoint="SelectObject")]  
     public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);  

     [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]  
     public static extern IntPtr GetDesktopWindow();  

     [DllImport("user32.dll",EntryPoint="GetDC")]  
     public static extern IntPtr GetDC(IntPtr ptr);  

     [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]  
     public static extern int GetSystemMetrics(int abc);  

     [DllImport("user32.dll",EntryPoint="GetWindowDC")]  
     public static extern IntPtr GetWindowDC(Int32 ptr);  

     [DllImport("user32.dll",EntryPoint="ReleaseDC")]  
     public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);  
    }  

}  

希望這將得到鑰匙切換捕捉你開始

1

試着輸入這個保存截圖:

int screenWidth = Screen.PrimaryScreen.Bounds.Width; 
int screenHeight = Screen.PrimaryScreen.Bounds.Height; 

     Graphics Graphics1; 
     Bitmap Bitmap1 = new Bitmap(screenWidth, screenHeight); 
     Graphics1 = Graphics.FromImage(Bitmap1); 
     Graphics1.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.Bounds.Size); 
     Bitmap1.Save(@"c:\ScreenShot1.bmp); //Place that you want to save screenshot