Quantcast
Channel: VBForums - WPF, WCF, WF
Viewing all articles
Browse latest Browse all 256

WPF – Copy UIElement as Image to Clipboard

$
0
0
I have got following C# code from this link.

https://elegantcode.com/2010/12/09/w...-to-clipboard/

Code:

  public static void CopyUIElementToClipboard(FrameworkElement element)
    {
        double width = element.ActualWidth;
        double height = element.ActualHeight;
        RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), 96, 96, PixelFormats.Default);
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext dc = dv.RenderOpen())
        {
            VisualBrush vb = new VisualBrush(element);
            dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
        }
        bmpCopied.Render(dv);
        Clipboard.SetImage(bmpCopied);
    }

Following vb.net code is converted from above C# code.

Code:

Public Shared Sub CopyUIElementToClipboard(element As FrameworkElement)
        Dim width As Double = element.ActualWidth
        Dim height As Double = element.ActualHeight
        Dim bmpCopied As New RenderTargetBitmap(CInt(Math.Round(width)), CInt(Math.Round(height)), 96, 96, PixelFormats.[Default])
        Dim dv As New DrawingVisual()
        Using dc As DrawingContext = dv.RenderOpen()
                Dim vb As New VisualBrush(element)
                dc.DrawRectangle(vb, Nothing, New Rect(New Point(), New Size(width, height)))
        End Using
        bmpCopied.Render(dv)
        Clipboard.SetImage(bmpCopied)
End Sub

Please somebody tell me how to trigger above code via Button.Click() event.

Viewing all articles
Browse latest Browse all 256

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>