C#矩形绘图和屏幕截图

本文关键字:屏幕截图 绘图 | 更新日期: 2023-09-27 17:59:42

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

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

这只是我在屏幕上绘制矩形的基本代码。当绘制矩形时,它会拍摄一张图像,效果非常好。问题是,矩形的绘制一点也不平滑。我启用了双缓冲,几乎尝试了所有的方法,但都没有成功。

此外,如果你理解我的意思,我想抓取当前屏幕,或者冻结它,然后能够在冻结的屏幕上绘制,而不仅仅是在活动屏幕的顶部绘制。如何做到这一点?

非常感谢您的帮助!

C#矩形绘图和屏幕截图

也许这篇文章会对你有所帮助:如何直接在Windows桌面上绘制C#?

你可以试试这样的东西:

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