Copy UIElement as Image to Clipboard

Herry Markowitz

Well-known member
Joined
Oct 22, 2015
Messages
46
Programming Experience
1-3
I have got following C# code from this link.

https://elegantcode.com/2010/12/09/wpf-copy-uielement-as-image-to-clipboard/


VB.NET:
 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.


VB.NET:
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[COLOR=#333333]

[/COLOR]



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


 
Doubleclick the button to generate the Click event handler. Add the code to call the method, and pass in a UI element as argument.
CopyUIElementToClipboard(button1)
 
Is this correct?

VB.NET:
Class MainWindow 


    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Call CopyUIElementToClipboard()
    End Sub




    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


End Class
 
Is this correct?


VB.NET:
Class MainWindow 


    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim element As New 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
End Class
 
No, your post 3 is almost correct, but you have make the call as in my example in post 2.
 
Back
Top