2011-01-26 36 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

对我来说,最简单的方法是使应用程序在任务栏中最小化并在计算机启动时启动。

它对电脑的侵扰性非常小:)。