C#截图工具服务 [英] C# snipping tool service

查看:21
本文介绍了C#截图工具服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先这个问题是为了教育目的.

First of all this question is for education purpose.

我的任务是提供服务来捕获屏幕的选择部分,如截图工具Win7我设法以win形式执行此操作并且它的工作很好但是当我在服务中执行它时返回黑屏我知道问题是服务在不同的会话中运行所以我的问题是如何使服务运行和返回用户桌面第二个问题是如何听取服务中的按键(我知道如何在表格中做到这一点)请提供任何帮助.慢慢来.我的表单代码:

My task is to do service that capture select part of screen like snipping tool in Win7 i manage to do this in win form and its work great but when i do it in service its return a black screen i know the problem which is the the service run in a different session so my question is how to make the service run and return the user desktop and the second question is how to listen to key press in service (i know how to do it in forms) any help please. And take your time. My form code:

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);
    }
}

推荐答案

在某些版本的 Windows 上,您也许可以让该服务看到一个桌面,但是如果有多人登录,它应该选择哪个桌面?

On some versions of Windows you may be able to get the service to see a desktop, however which desktop should it choose if there are multiple people logged in?

基本上,如果您想与桌面进行适当的交互,您的解决方案不能作为服务运行.

Basically, your solution can't run as a service if you want propert interaction with the desktop.

此外,出于同样的原因,您不应该生成消息框,因为可能没有人可以单击确定"并允许程序继续执行.

Also, for the same reason, you shouldn't be spawning message boxes, as there may be no one that can click OK and allow program execution to continue.

这篇关于C#截图工具服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆