alternetsoft / AlternetUI

MIT License
22 stars 2 forks source link

Add wxScreenDC and Graphics.CopyFromScreen #113

Closed generalloki closed 4 months ago

generalloki commented 6 months ago

https://docs.wxwidgets.org/3.2/classwx_screen_d_c.html

CopyFromScreen is present in standard .Net8.0 https://learn.microsoft.com/fr-fr/dotnet/api/system.drawing.graphics.copyfromscreen?view=dotnet-plat-ext-8.0

generalloki commented 5 months ago

Added Graphics.FromScreen. It creates canvas which allows to draw on screen.

neoxeo commented 5 months ago

@generalloki

I have compiled 0.9.516 and Graphics.FromScreen not present/available .

generalloki commented 5 months ago

It is strange. There is such method: public static Graphics FromScreen() and also a demo in \AlternetUI\Source\Samples\ControlsSample\InternalSamples\NinePatchDrawing\ (button 'Draw on Screen')

generalloki commented 5 months ago

BTW Ubuntu doesn't allow to draw on screen. It worked only on Windows.

neoxeo commented 5 months ago

Ok, thanks for your answer.

I have tried to use like this :

  private void TakeScreenShot()
  {
      Bitmap bitmap = new Bitmap(Display.Primary.Bounds.Width, Display.Primary.Bounds.Height);
      Graphics graphics = Graphics.FromImage(bitmap);

      graphics.FromScreen(0, 0, 0, 0, bitmap.Size);

      bitmap.Save("screenshot.png", BitmapType.Png);
      graphics.Dispose();
}
generalloki commented 5 months ago

Graphics.FromScreen is static method that create Graphics, so currently it is not like in WinForms. There will be method GetAsBitmapI in Graphics which gets the bitmap. But currently there are some problems with this even on Windows and I am not sure it will work on Linux

neoxeo commented 5 months ago

@generalloki

I found these links if it can be usefull to find a solution : https://forums.wxwidgets.org/viewtopic.php?t=43803 https://forums.wxwidgets.org/viewtopic.php?f=23&t=49318&p=212830 https://forums.wxwidgets.org/viewtopic.php?f=23&t=42979&p=174659 https://forums.wxwidgets.org/viewtopic.php?f=1&t=5016&p=22887

generalloki commented 5 months ago

One of these links worked. I added Image.FromScreen (static function). It works like this:

               var bitmap = Image.FromScreen();
                if (bitmap is not null)
                {
                    bitmap?.Rescale((800, 600));
                    picturePopup.MainControl.Image = bitmap;
                    picturePopup.ShowPopup(button2);
                }
                else
                    Application.LogError("Screen picture is null");

I beleive we don't need Graphics.CopyFromScreen for now, it is enough to have Image.FromScreen, maybe will add CopyFromScreen later.

Current problem is that this works under Windows but don't work under Ubuntu, so still need to be improved

neoxeo commented 5 months ago

Test Ok, perfect !

Thanks a lot