Exemplo n.º 1
0
        private static Texture getDesktopTexture(IrrlichtDevice device)
        {
            int screenX      = 0;
            int screenY      = 0;
            int screenWidth  = device.VideoDriver.ScreenSize.Width;
            int screenHeight = device.VideoDriver.ScreenSize.Height;

            Point p = new Point();

            GetCursorPos(ref p);
            screenX = p.X - screenWidth / 2;
            screenY = p.Y - screenHeight / 2;

            // validate grabbing rect (note: works fine without validation too) {{

            if (screenX < 0)
            {
                screenX = 0;
            }

            if (screenX + screenWidth > SystemInformation.VirtualScreen.Width)
            {
                screenX = SystemInformation.VirtualScreen.Width - screenWidth;
            }

            if (screenY < 0)
            {
                screenY = 0;
            }

            if (screenY + screenHeight > SystemInformation.VirtualScreen.Height)
            {
                screenY = SystemInformation.VirtualScreen.Height - screenHeight;
            }

            // }}

            Bitmap b = new Bitmap(
                screenWidth,
                screenHeight,
                PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(b);

            g.CopyFromScreen(
                screenX,
                screenY,
                0, 0,
                new Size(screenWidth, screenHeight),
                CopyPixelOperation.SourceCopy);

            MemoryStream s = new MemoryStream();

            b.Save(s, ImageFormat.Bmp);

            byte[] c = s.ToArray();
            s.Close();

            LogLevel o = device.Logger.LogLevel;

            device.Logger.LogLevel = LogLevel.Error;             // we hide all those "Loaded texture" messages in console {{

            ReadFile f = device.FileSystem.CreateMemoryReadFile("screenTexture", c);
            Texture  t = device.VideoDriver.GetTexture(f);

            f.Drop();

            device.Logger.LogLevel = o;             // }}

            return(t);
        }