繁体   English   中英

C#矩形绘图和屏幕截图

[英]C# rectangle drawing and 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);
    }

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

另外,我想抓取当前的屏幕或冻结它,然后能够在该冻结的屏幕上绘图,如果您理解我的话,不仅可以在活动屏幕的顶部进行绘图。 怎么做?

任何帮助深表感谢!

也许该文章将对您有所帮助: 如何在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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM