如何在c#中制作一个窗口的位图

本文关键字:一个 窗口 位图 | 更新日期: 2023-09-27 18:07:10

我的目标是能够制作一个窗口的位图,而且只是那个窗口。我还需要能够在该窗口内制作某些像素区域的位图。这些区域不是windows窗体或类似的东西。

我想从窗口句柄制作一个屏幕,然后将屏幕上我需要的区域写入位图,这将是解决这个问题的好方法。

我是这样做的:

 Process[] processlist = Process.GetProcesses();
        String[] process = null;
        process = new string[200];
        int i = 0;
        IntPtr handle = IntPtr.Zero;
        foreach (Process theprocess in processlist)
        {
            process[i] = theprocess.MainWindowTitle;
            if (process[i].Contains("Title of Process"))
            {
                handle = theprocess.MainWindowHandle;
                MessageBox.Show("Handle Set");
                break;
            }
            i++;
        }
        //Sets new screen from Handle
        Screen thescreen = Screen.FromHandle(handle);
        //Bitmap stored
        Bitmap bmpScreenshot = new Bitmap(thescreen.Bounds.Width, thescreen.Bounds.Height);
        //Graphics object to draw screen in bitmap
        Graphics g = Graphics.FromImage(bmpScreenshot);
        //Copy from screen to bitmap
        g.CopyFromScreen(0, 0, 0, 0, thescreen.Bounds.Size);

然而,这只是获得整个屏幕的位图。我怎样才能把它缩小到一个窗口的大小呢?

谢谢。

如何在c#中制作一个窗口的位图

你可以这样做:

Bitmap bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height);
DrawToBitmap(bmpScreenshot, this.DisplayRectangle);
pictureBox1.Image = bmpScreenshot;

我尝试了上面的代码,它工作良好