2011-01-26 35 views
0

首先,這個問題是爲了教育目的。C#剪切工具服務

我的任務是做服務,捕獲屏幕的選擇部分像剪切工具在 Win7我設法做到這一點在贏的形式和它的工作很好,但當我做它在服務其返回黑屏我知道問題這是在不同的會話中運行的服務,所以我的問題是如何使服務運行並返回用戶桌面 ,第二個問題是如何聽取服務中的按鍵(我知道如何在表單中執行)任何請幫助。花點時間。我的表單代碼:

private void CaptureScreen() 
{ 
    this.Hide(); 
    Thread.Sleep(300); 
    bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); 
    gfxScreenshot = Graphics.FromImage(bmpScreenshot); 
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); 
    bmpScreenshot.Save(DialogSave.FileName, ImageFormat.Jpeg); 

    pictureBox1.Image = bmpScreenshot; 
    this.Show(); 
} 

private static Image cropImage(Bitmap img, Rectangle cropArea) 
{ 
    Bitmap bmpCrop = img.Clone(cropArea, 
    img.PixelFormat); 
    return (Image)(bmpCrop); 
} 

private Rectangle selectArea(int recX1, int recY1,int recX2,int recY2) 
{ 
    int width = recX2 - recX1; 
    int height = recY2 - recY1; 
    return new Rectangle(recX1, recY1, width, height); 
} 

private void btnCrop_Click(object sender, EventArgs e) 
{ 
    if (x1 <= 0 || x2 <= 0 || y1 <= 0 || y2 <= 0) 
    { 
     MessageBox.Show("Please select area first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    else 
    { 
     Rectangle myrectangle = selectArea(x1, y1, x2, y2); 
     Bitmap myImg = (Bitmap)Image.FromFile(filename); 
     Image cr = cropImage(myImg, myrectangle); 
     na = @"F:\\" + Counter + ".jpg"; 
     while (File.Exists(@"F:\\" + Counter + ".jpg")) 
     { 
      Counter++; 
     } 
     na = @"F:\\" + Counter + ".jpg"; 
     cr.Save(@"F:\\" + Counter++ + ".jpg", ImageFormat.Jpeg); 
     pictureBox1.Image = cr; 
     System.Diagnostics.Process prc = new System.Diagnostics.Process(); 
     prc.StartInfo.FileName = @"F:\\"; 
     prc.Start(); 
     this.PrintScreennotifyIcon.BalloonTipText = "Save to" + na; 
     this.PrintScreennotifyIcon.BalloonTipTitle = "Info"; 
     this.PrintScreennotifyIcon.Visible = true; 
     this.PrintScreennotifyIcon.ShowBalloonTip(3); 
    } 
} 
+2

請編輯問題並添加大寫,句號和其他標點符號,如問號。 – 2011-01-26 08:08:55

+0

@Erno:我做了一些嘗試修復 – abatishchev 2011-01-26 08:25:40

+2

取決於操作系統。這在Windows Server上將無法工作。 – Remy 2011-01-26 08:28:53

回答

1

您必須選中「允許服務與桌面交互」選項並選擇本地帳戶(服務屏幕上的所有內容)。

enter image description here

2

在某些Windows版本中你可以得到的服務,看桌面,但是其桌面應選擇它,如果有多人登錄?

基本上,如果您希望與桌面進行交互操作,您的解決方案無法作爲服務運行。另外,出於同樣的原因,您不應該產生消息框,因爲可能沒有人可以單擊「確定」並允許程序繼續執行。

0

對我來說,最簡單的方法是使應用程序在任務欄中最小化並在計算機啓動時啓動。

它對電腦的侵擾性非常小:)。