1

So, let me just start out by showing you the code I have now:

        private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        currentPos = startPos = e.Location;
        drawing = true;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        currentPos = e.Location;
        //Calculate X Coordinates
        if (e.X < startPos.X)
        {
            CurrentTopLeft.X = e.X;
        }
        else
        {
            CurrentTopLeft.X = startPos.X;
        }

        //Calculate Y Coordinates
        if (e.Y < startPos.Y)
        {
            CurrentTopLeft.Y = e.Y;
        }
        else
        {
            CurrentTopLeft.Y = startPos.Y;
        }

        if (drawing)
            this.Invalidate();
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        if (drawing)
        {
            this.Hide();
            SaveScreen();
        }
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Color col = Color.FromArgb(75, 100, 100, 100);
        SolidBrush b = new SolidBrush(col);

        if (drawing) 
            e.Graphics.FillRectangle(b, getRectangle());
    }

My SaveScreen function:

        private void SaveScreen()
        {

        ScreenShot.CaptureImage(CurrentTopLeft, Point.Empty, getRectangle());

        }

The CaptureImage function:

        public static void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle)
    {
        string FilePath = "temp.jpg";
        using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
            }
            bitmap.Save(FilePath, ImageFormat.Jpeg);
        }

        string Filename = String.Format("{0:yyyy-M-d-HH-mm-ss}", DateTime.Now) + ".jpg";
        string Server = "";

        System.Net.WebClient Client = new System.Net.WebClient();
        Client.Headers.Add("Content-Type", "image/jpeg");
        byte[] result = Client.UploadFile(Server + "upload.php?filename=" + Filename + "", "POST", FilePath);
        string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);

        Program.mainForm.Notify(Server + Filename);

        File.Delete(FilePath);
    }

This is just the basic code I have for drawing a rectangle on the screen. When the rectangle is drawn, it takes an image, works perfectly. The problem is, that the drawing of the rectangle is not smooth at all. I have enabled doublebuffering and pretty much tried everything, but with no luck.

Also, I would like to grab the current screen, or freeze it, and then be able to draw on that frozen screen, instead of just drawing on top of the active screen if you understand me. How would this be done?

Any help is much appreciated!

Lazze
  • 323
  • 2
  • 7
  • 17

2 Answers2

1

Maybe that post will help you: How to draw directly on the Windows desktop, C#?

Community
  • 1
  • 1
GameScripting
  • 16,092
  • 13
  • 59
  • 98
  • That is not what I'm looking for unfortunately. I want to draw on a snapshot of the screen, not the current screen. – Lazze Jun 21 '11 at 15:53
0

You could try something like this:

int width =
    Screen.PrimaryScreen.Bounds.Width,
    height = Screen.PrimaryScreen.Bounds.Height;

Bitmap screen = default( Bitmap );

try
{
    screen = new Bitmap
    (
        width,
        height,
        Screen.PrimaryScreen.BitsPerPixel == 32 ?
            PixelFormat.Format32bppRgb :
                PixelFormat.Format16bppRgb565
    );

    using (Graphics graphics = Graphics.FromImage(screen))
    {
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        graphics.CopyFromScreen
        (
            new Point() { X = 0, Y = 0 },
            new Point() { X = 0, Y = 0 },
            new Size() { Width = width, Height = height },
            CopyPixelOperation.SourceCopy
        );

        // Draw over the "capture" with Graphics object
    }
}
finally
{
    if (screen != null)
    {
        screen.Dispose();
    }
}
Brandon Moretz
  • 7,512
  • 3
  • 33
  • 43
  • I could not seem to get that to work. I might just be blanking out, how would I get to display the screen as a background? – Lazze Jun 21 '11 at 16:39
  • @Lazze as a background of what? Your form? You have a handle to the "Graphics" object of the screen capture, you can use http://www.pinvoke.net/default.aspx/gdi32.bitblt to draw it to your forms "Graphics" object on paint if that's what you're looking for? – Brandon Moretz Jun 21 '11 at 16:44
  • Yes, exactly. My form is starting in fullscreen, and is ready to be drawn on. So instead of just making it transparent, I wanted to capture a snapshot of the screen and show that in fullscreen. Could you by any chance give me an example on how to draw the image onto my form? – Lazze Jun 21 '11 at 17:01
  • @Lazzy you want to execute the capture code before your form is displayed (with the above code ), store it in a instance variable of your main application class (make sure to dispose it on exit), then just copy from that to your On_Paint() graphics instance (you can modify the in memory capture at will and your change will get reflected on the next WM_Paint event). – Brandon Moretz Jun 21 '11 at 17:04