C# 矩形绘制及截图

发布于 2024-11-16 06:38:15 字数 2637 浏览 6 评论 0原文

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

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

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

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

非常感谢任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-11-23 06:38:15

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

温柔戏命师 2024-11-23 06:38:15

你可以尝试这样的事情:

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文