Results 1 to 1 of 1

Thread: WPF – Copy UIElement as Image to Clipboard

  1. #1

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    WPF – Copy UIElement as Image to Clipboard

    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.
    Last edited by Kram Kramer; Sep 11th, 2017 at 11:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width