C#矩形图和屏幕截图

C#矩形图和屏幕截图,c#,drawing,screenshot,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) {

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

        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());
    }
CaptureImage功能:

        private void SaveScreen()
        {

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

        }
        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);
    }
这只是我在屏幕上绘制矩形的基本代码。绘制矩形时,它会拍摄一幅图像,效果完美。 问题是,矩形的绘制一点也不平滑。我启用了双缓冲,几乎尝试了所有方法,但没有成功

另外,我想抓取当前屏幕,或将其冻结,然后能够在冻结的屏幕上绘图,而不是在活动屏幕上绘图(如果您理解我的话)。如何做到这一点


非常感谢您的帮助

也许那篇文章会帮助你:

也许那篇文章会帮助你:

您可以尝试以下方法:

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

您可以尝试以下方法:

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

不幸的是,这不是我想要的。我想画屏幕的快照,而不是当前屏幕。不幸的是,这不是我想要的。我想在屏幕快照上绘制,而不是当前屏幕。您可能没有以正确的分辨率绘制。您可能没有以正确的分辨率绘制。我似乎无法使其正常工作。我可能只是空白,我如何才能将屏幕显示为背景?@Lazze作为什么的背景?你的表格?你有一个屏幕截图的“图形”对象的句柄,你可以用它把它画到画图上的“图形”对象上,如果这就是你要找的?是的,没错。我的表单正在全屏启动,并准备好绘制。所以,我不想让它变得透明,而是想捕捉屏幕的快照并全屏显示。您能给我一个如何将图像绘制到我的表单上的示例吗?@Lazzy您想在显示表单之前执行捕获代码(使用上述代码),将其存储在主应用程序类的实例变量中(确保在退出时进行处理),然后将其复制到on_Paint()graphics实例中(您可以随意修改内存捕获,您的更改将反映在下一个WM_Paint事件中)。我似乎无法实现这一点。我可能只是在空白,我如何才能将屏幕显示为背景?@Lazze作为什么的背景?您的表单?您有“图形”的句柄对象,您可以使用它将其绘制到窗体“图形”画图上的对象,如果这是你要找的?是的,没错。我的表单从全屏开始,准备在上面绘制。因此,我不只是让它透明,而是想捕获屏幕快照并在全屏上显示。你能给我一个如何在我的表单上绘制图像的示例吗?@Lazzy你想执行吗在显示表单之前使用捕获代码(使用上面的代码),将其存储在主应用程序类的实例变量中(确保在退出时对其进行处理),然后将其复制到on_Paint()图形实例中(您可以随意修改内存中的捕获,并且您的更改将反映在下一个WM_Paint事件中)。