Is there a way to run c# forms application without showing a gui window (like a console application)

£可爱£侵袭症+ 提交于 2019-12-08 05:00:51

问题


Is there a way to run c# forms application without showing a gui window (like a console application). I'm trying to take screen shorts of webpages loaded into a webkit.net control.

Everything works fine and I can get screenshots but if there is a way to run the application without showing the window, it will be much cleaner to include it in scripts to automate webpage capture.

I have tried: form.hide() method but some how it doesn't hide anything at all.


回答1:


Inside your application, when you create a new WinForms application, delete Form1.cs and the designer file.

Then inside the Program.cs main method you have the following code:

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());

Remove these lines, and put what you need instead. Make sure that the Main method never returns if you want to keep the program running by adding some sort of loop inside it.

You can also consider creating a NotifyIcon so that your program will show a notification icon inside the notification area of task bar. This way you can add a menu to the notification icon so the user has some way of interacting, and closing, the application.




回答2:


I'm not familiar with the webkit.net API, but you mentioned, that it provides a Windows Forms control. There is a System.Windows.Forms.Control.DrawToBitmap, which paints the control to a bitmap. And it does not require to put the control into a Form.

Using Øyvind Knobloch-Bråthen's approach, a sample Main method would look like this:

static void Main()
{
    // Leave these lines here!
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // change this in order to use the webkit control
    Control ctrl = new Button { Width = 100, Height = 23, Text = "abc" };

    using (Bitmap bitmap = new Bitmap(ctrl.Width, ctrl.Height, PixelFormat.Format32bppRgb))
    {
        ctrl.DrawToBitmap(bitmap, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
        bitmap.Save("test.png");
    }
}

Note that I used a button - you should modify it to paint your webkit.net control.




回答3:


I think what you want to do here might be unsupported by the API. My suggestion would be to search the WebKit Library for some way to make a screenshot without the need of a WinForms control.

If you want to take this path to the solution you might try to replace Application.Run(new Form1()); in "Program.cs" with the following:

using (Form1 form = new Form1())
{
  form1.webKit1.DoScreenshot() // Just guessing on the API here
}

I can't guarantee this will actually work. Or that it will keep working in new future versions of webkit.net




回答4:


It's extremely unlikely that you'll be able to get a screen-grab from a 3rd-party control in your own application without it being visible to the user - if a window is not visible on screen (not-opened, hidden, opened offscreen or even just behind something else) then it simply won't render.

If the webkit has a specific mechanism for rendering to an offscreen bitmap for you, then you may be able to use that to generate the image you need without showing the control - or as @Stephan has pointed out, DrawToBitmap may work. Beyond that you might be into the complicated realms of running the application on a hidden Desktop or other hackery.



来源:https://stackoverflow.com/questions/9413511/is-there-a-way-to-run-c-sharp-forms-application-without-showing-a-gui-window-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!