C#類比PrtScn實現截屏

來源:互聯網
上載者:User

標籤:class   sleep   鍵盤   mic   value   按鈕   option   引用   psi   

有了之前的基礎知識瞭解,如今開始實現PrtScn和Alt+PrtScn。

首先建立一個WPF應用程式,命名為PrintscreenAndAltPrintScreen

匯入keybd_event方法
須要為DllImport加入using System.Runtime.InteropServices;

[DllImport("user32.dll")]static extern void keybd_event(    byte bVk,// 虛擬索引值    byte bScan,// 硬體掃描碼    uint dwFlags,// 動作標識    IntPtr dwExtraInfo// 與鍵盤動作關聯的輔加資訊);

編寫PrtScn函數:

public void PrintScreen(){    keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down    System.Windows.Forms.Application.DoEvents(); //加入引用system.windows.forms    keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up    System.Windows.Forms.Application.DoEvents();}

編寫Alt+PrtScn函數:

public void AltPrintScreen(){    keybd_event((byte)Keys.Menu, 0, 0x0, IntPtr.Zero);    keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down    System.Windows.Forms.Application.DoEvents();    System.Windows.Forms.Application.DoEvents();    keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up    keybd_event((byte)Keys.Menu, 0, 0x2, IntPtr.Zero);    System.Windows.Forms.Application.DoEvents();    System.Windows.Forms.Application.DoEvents();}

編寫從剪貼簿獲得圖片函數:
須要加入using System.Drawing;加入引用System.Drawing

private Bitmap GetScreenImage(){    System.Windows.Forms.IDataObject newobject = null;    Bitmap NewBitmap = null;    try    {        System.Windows.Forms.Application.DoEvents();        newobject = System.Windows.Forms.Clipboard.GetDataObject();        if (System.Windows.Forms.Clipboard.ContainsImage())        {            NewBitmap = (Bitmap)(System.Windows.Forms.Clipboard.GetImage().Clone());        }        return NewBitmap;    }    catch(Exception ex)    {        Console.WriteLine(ex.Message);        return null;    }}

編寫xmal程式:
簡單加入兩個Image控制項和兩個button按鈕

 <Window x:Class="PrintscreenAndAltPrintScreen.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="768" Width="1024">     <Grid>          <Image Name="image1" Margin="45,30,562,203">          </Image>          <Button Content="PrtScn" Height="24" HorizontalAlignment="Left" Margin="182,552,0,0" Name="button1" VerticalAlignment="Top" Width="95" Click="button1_Click" />          <Button Content="Alt+PrtScn" Height="24" HorizontalAlignment="Left" Margin="718,552,0,0" Name="button2" VerticalAlignment="Top" Width="95" Click="button2_Click" />          <Image Margin="566,30,41,213" Name="image2" />      </Grid>  </Window>

對兩個按鈕加入click事件:
這裡須要把Bitmap轉為BitmapSource。能夠參閱部落格:WPF(C#)中Bitmap與BitmapImage相互轉換

private void button2_Click(object sender, RoutedEventArgs e){    image2.Source = null;    AltPrintScreen();    Bitmap bitmap = GetScreenImage();    IntPtr ip = bitmap.GetHbitmap();//從GDI+ Bitmap建立GDI位元影像對象//Imaging.CreateBitmapSourceFromHBitmap方法,基於所提供的非託管位元影像和調色盤資訊的指標。返回一個託管的BitmapSource    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());    image2.Source = bitmapSource;}
private void button1_Click(object sender, RoutedEventArgs e){    image1.Source = null;    PrintScreen();    Bitmap bitmap = GetScreenImage();    IntPtr ip = bitmap.GetHbitmap();//從GDI+ Bitmap建立GDI位元影像對象//Imaging.CreateBitmapSourceFromHBitmap方法//基於所提供的非託管位元影像和調色盤資訊的指標,返回一個託管的BitmapSource    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());    image1.Source = bitmapSource;}

最後對DoEvents 方法的一點解釋:
Application.DoEvents 方法
處理當前在訊息佇列中的全部 Windows 訊息。
文法:

public static void DoEvents()

當執行 Windows 視窗時。它將建立新視窗。然後該視窗等待處理事件。該視窗在每次處理事件時。均將處理與該事件關聯的全部代碼。

全部其它事件在隊列中等待。

當代碼處理事件時。應用程式不會響應。

比如,假設將甲視窗拖到乙視窗之上,則乙視窗不會又一次繪製。


假設在代碼中調用 DoEvents。則您的應用程式能夠處理其它事件。

比如,假設您有向 ListBox 加入資料的視窗,並將 DoEvents 加入到代碼中,那麼當將還有一視窗拖到您的視窗上時,該視窗將又一次繪製。

假設從代碼中移除 DoEvents,那麼在按鈕的單擊事件處理常式執行結束曾經。您的視窗不會又一次繪製。
與 Visual Basic 6.0 不同。 DoEvents 方法不調用 Thread.Sleep 方法。

最後發圖不發種:

C#類比PrtScn實現截屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.