Segue código funcional do WinForms
:
Bitmap pic = new Bitmap(label1.Width, label1.Height);
Rectangle rect = new Rectangle(0, 0, label1.Width, label1.Height);
rect = label1.ClientRectangle;
label1.DrawToBitmap(pic, rect);
// pic.Save("C:\\, ImageFormat.Jpeg);
O código acima tira printscreen do label1
e salva a imagem no local.
Como faço a mesma do WinForms
para WPF
?
Segue o código da tentativa do WPF
:
Rect bounds = VisualTreeHelper.GetDescendantBounds(label1);
RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(label1);
context.DrawRectangle(visualBrush, null, new Rect(new System.Windows.Point(), bounds.Size));
}
renderTarget.Render(visual);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (Stream stm = File.Create(@"C:\Users\Matheus Miranda\Desktop\TESTE"))
{
bitmapEncoder.Save(stm);
}
Alguma solução ?
@"C:\Users\Matheus Miranda\Desktop\TESTE"
para@"C:\Users\Matheus Miranda\Desktop\TESTE"\nomedafoto.jpg