반응형
public static void CaptureScreen(UIElement source, string saveImageFilePath)
{
    Uri destination = new Uri(saveImageFilePath);
 
    double Height, renderHeight, Width, renderWidth;
 
    Height = renderHeight = source.RenderSize.Height;
    Width = renderWidth = source.RenderSize.Width;
 
    //Specification for target bitmap like width/height pixel etc.
    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
 
    //creates Visual Brush of UIElement
    VisualBrush visualBrush = new VisualBrush(source);
 
    DrawingVisual drawingVisual = new DrawingVisual();
    using (DrawingContext drawingContext = drawingVisual.RenderOpen())
    {
        //draws image of element
        drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(0, 0), new Point(Width, Height)));
    }
 
    //renders image
    renderTarget.Render(drawingVisual);
 
    //PNG encoder for creating PNG file
    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(renderTarget));
    using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
    {
        encoder.Save(stream);
    }
}
반응형

+ Recent posts