0

I was examining a program code. It was writen in WinForms, but i tried to write it in WPF. Here is my code:

Graphics graphics = null;

var w = System.Windows.SystemParameters.PrimaryScreenWidth;
var h = System.Windows.SystemParameters.PrimaryScreenHeight;

graphics.CopyFromScreen(location.X, location.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

Visual Studio says "The name 'Screen' does not exist in the current context". What is the problem?

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
Jcode
  • 15
  • 2
  • perhaps you need [this wrapper](https://stackoverflow.com/a/2118993/4648586) to get `Screen` instance that seems to be included on WinForm. Specifically the usage of `WindowInteropHelper`. hope it helps. – Bagus Tesa Jan 19 '19 at 01:34

1 Answers1

2

It is obvious, because Screen is inside System.Windows.Forms and you do not have access to it from WPF application.

I suppose you are trying to take screenshot so it meight help you in WPF:

private void TakeScreenShot()
{
    double Left = SystemParameters.VirtualScreenLeft;
    double Top = SystemParameters.VirtualScreenTop;
    double ScreenWidth = SystemParameters.VirtualScreenWidth;
    double ScreenHeight = SystemParameters.VirtualScreenHeight;

    using (System.Drawing.Bitmap bmpScreen = new System.Drawing.Bitmap((int)ScreenWidth, (int)ScreenHeight))
    {
       using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bmpScreen))
       {
            graphic.CopyFromScreen((int)Left, (int)Top, 0, 0, bmpScreen.Size);
            bmpScreen.Save(@"D:\bitmap.bmp");
            IMG.Source = BitmapToImageSource(bmpScreen); // show bitmap in IMG (Image control)
        }
    }
}


BitmapImage BitmapToImageSource(Bitmap bitmap)
{
    using (MemoryStream memory = new MemoryStream())
    {
        bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
        memory.Position = 0;
        BitmapImage bitmapimage = new BitmapImage();
        bitmapimage.BeginInit();
        bitmapimage.StreamSource = memory;
        bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapimage.EndInit();
        return bitmapimage;
    }
}

you might need to add a reference to System.Drawing.dll

Updated the answer based on Comment from @Erno de Weerd. Besides a method to show a bitmap in the image control also added

M.Armoun
  • 1,075
  • 3
  • 12
  • 38
  • 1
    Please make sure you call Dispose on the Bitmap and the Graphics objects once the code is done with these. – Emond Dec 23 '18 at 09:13
  • 1
    Thank you. But it did not save :/ bmpScreen.Save("C:\\bitmap.bmp", System.Drawing.Imaging.ImageFormat.Bmp); I tried this. Well instead of saving, how can I put it on Image? – Jcode Dec 23 '18 at 09:21
  • Try another path in drive D (for example), i tested and it works – M.Armoun Dec 23 '18 at 09:51
  • Pls read my answer, I edited it when you mentioned you need put it in Image. and you can accept the answer if it worked (Just mentioned as it seems you are new here) – M.Armoun Dec 23 '18 at 10:49
  • I'm grateful to you :) thank you for your help :) my problem is solved. Yes I am new here. How can I accept the answer? – Jcode Dec 23 '18 at 12:57
  • it's a tick on the left side of answer below the vote up and vote down – M.Armoun Dec 23 '18 at 13:29