public void Capture(FrameworkElement element)
{
    double dpi = 96;
    double width = element.ActualWidth;
    double height = element.ActualHeight;

    DrawingVisual drawingVisual = new DrawingVisual();
    using (DrawingContext dc = drawingVisual.RenderOpen())
    {
        VisualBrush vb = new VisualBrush(element);
        dc.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), new System.Windows.Size(width, height)));
    }

    RenderTargetBitmap rtb = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), dpi, dpi, PixelFormats.Default);
    rtb.Render(drawingVisual);

    PngBitmapEncoder enc = new PngBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(rtb));
    using (Stream stm = File.Create("label.png"))
    {
        enc.Save(stm);
    }
}

카테고리:

업데이트:

댓글남기기