Getting Images from the Clipboard

Getting Images from the Clipboard
This example shows you how to get an image from the clipboard and display it in a PictureBox. When you run the code, you'll see a form that displays whatever image you currently have on the clipboard, along with its dimensions (displayed in the status bar at the bottom).

There is a callback on the form's Activate event, which updates the PictureBox whenever the form reacquires focus.

This code includes a conditional at the top that allows it to work with Python.NET.



import sys if sys.platform != 'cli': import pythoncom pythoncom.CoInitialize

import clr clr.AddReference('System.Windows.Forms') from System.Windows.Forms import (Clipboard, Form, StatusBar, DockStyle,                                 PictureBox)

f = Form f.Text = "Clipboard Image Example"

pb = PictureBox

sb = StatusBar

pb.Dock = DockStyle.Fill sb.Dock = DockStyle.Bottom f.Controls.Add(pb) f.Controls.Add(sb)
 * 1) Layout:

def OnActivated(sender, args): if Clipboard.ContainsImage: img = Clipboard.GetImage pb.Image = img
 * 1) Event handling:

sb.Text = '(%s x %s)' % (img.Width, img.Height) f.Activated += OnActivated

f.ShowDialog

Back to Contents.