C#矩形图和截图

时间:2011-06-21 15:23:22

标签: c# drawing screenshot

所以,让我先向您展示我现在的代码:

        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());
    }

我的SaveScreen功能:

        private void SaveScreen()
        {

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

        }

CaptureImage功能:

        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);
    }

这只是我在屏幕上绘制矩形的基本代码。绘制矩形时,它会拍摄图像,效果很好。 问题是,矩形的绘制根本不平滑。我已经启用了双重缓冲,并且几乎尝试了一切,但没有运气。

此外,我想抓住当前屏幕,或者冻结它,然后能够在冻结的屏幕上绘图,而不是仅仅在您了解我的情况下在活动屏幕上绘图。怎么会这样做?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

也许该帖子可以帮助您: How to draw directly on the Windows desktop, C#?

答案 1 :(得分:0)

您可以尝试这样的事情:

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();
    }
}