How do I backup and restore the system clipboard in C#?

It’s folly to try to do this. You cannot faithfully restore the clipboard to its prior state. There could be dozens of unrendered data formats present using “delayed rendering”, and if you attempt to render them all, you’ll cause the source app to run out of resources. It’s like walking into a resturaunt and saying “give me one of everything”.

Suppose that the user has selected 500 rows x 100 columns in Excel, and has copied that to the clipboard. Excel “advertises” that it can produce this data in about 25 different formats, including Bitmap. Once you paste it as a Bitmap, you force Excel to render it as a bitmap. That’s 50000 cells, and would be a bitmap approx 10,000 x 15,000 pixels. And you expect the user to wait around while Excel coughs that up, along with 24 other formats? Not feasible.

Furthermore, you’re going to be triggering WM_DrawClipboard events, which will impact other clipboard viewers.

Give up.

Leave a Comment