Capture a canvas em um bitmap

Eu quero capturar a canvas no meu código para obter uma imagem – como usar o botão ‘print screen’ no teclado.

Alguém tem uma ideia de como fazer isso? Eu não tenho ponto de partida.

Se estiver usando a estrutura do .NET 2.0 (ou posterior), você pode usar o método CopyFromScreen() detalhado aqui:

http://sofpt.miximages.com/c%23/ Save the screenshot to the specified path that the user has chosen. bmpScreenshot.Save(Screenshot.png”, ImageFormat.Png);

 // Use this version to capture the full extended desktop (ie multiple screens) Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height, PixelFormat.Format32bppArgb); Graphics screenGraph = Graphics.FromImage(screenshot); screenGraph.CopyFromScreen(SystemInformation.VirtualScreen.X, SystemInformation.VirtualScreen.Y, 0, 0, SystemInformation.VirtualScreen.Size, CopyPixelOperation.SourceCopy); screenshot.Save("Screenshot.png", System.Drawing.Imaging.ImageFormat.Png); 
 Bitmap memoryImage; //Set full width, height for image memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); Size s = new Size(memoryImage.Width, memoryImage.Height); Graphics memoryGraphics = Graphics.FromImage(memoryImage); memoryGraphics.CopyFromScreen(0, 0, 0, 0, s); string str = ""; try { str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Screenshot.png");//Set folder to save image } catch { }; memoryImage.save(str); 

Experimente este código

 Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics gr = Graphics.FromImage(bmp); gr.CopyFromScreen(0, 0, 0, 0, bmp.Size); pictureBox1.Image = bmp; bmp.Save("img.png",System.Drawing.Imaging.ImageFormat.Png);