[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Paste a PNG format image with a transparent background from the clipboard in C#

Paste a PNG format image

Sometimes you may want to paste a PNG format image into your program. The clipboard provides a simple ContainsImage method to decide whether the clipboard contains image data and a simple GetImage method to get that image if it is there. Unfortunately these methods only work for bitmap data that doesn't use transparency. To get an image with transparent pixels, you can get PNG format data.

See the example Copy an irregular area from a picture to the clipboard in C# for information on copying PNG data to the clipboard.

The GetClipboardImage method shown in the following code tries to get image data from the clipboard. First it tries to get PNG data. If that fails, it tries to get bitmap data.

// Get a PNG from the clipboard if possible. // Otherwise try to get a bitmap. private Image GetClipboardImage() { // Try to paste PNG data. if (Clipboard.ContainsData("PNG")) { Object png_object = Clipboard.GetData("PNG"); if (png_object is MemoryStream) { MemoryStream png_stream = png_object as MemoryStream; return Image.FromStream(png_stream); } } // Try to paste bitmap data. if (Clipboard.ContainsImage()) { return Clipboard.GetImage(); } // We couldn't find anything useful. Return null. return null; }

This method uses the clipboard's ContainsData method to see if PNG data is available. If it is, the code uses the Clipboard object's GetData method to get that data, which should be in the form of a memory stream. It uses the stream to create an Image object and returns it.

If there is no PNG data available, the program uses the Clipboard object's ContainsImage and GetImage methods to get bitmap data. If that also fails, the method returns null.

When you click on the program's picture, the program uses the following code to paste the image data from the clipboard to the location you clicked.

// Paste image data from the clipboard. private void picOriginal_MouseClick(object sender, MouseEventArgs e) { // Try to get an image. Image clipboard_image = GetClipboardImage(); // If we failed. Beep to tell the user that // we cannot paste the available formats. if (clipboard_image == null) { System.Media.SystemSounds.Beep.Play(); return; } // Draw on the image. using (Graphics gr = Graphics.FromImage(picOriginal.Image)) { Rectangle source_rect = new Rectangle(0, 0, clipboard_image.Width, clipboard_image.Height); int x = e.X - clipboard_image.Width / 2; int y = e.Y - clipboard_image.Height / 2; Rectangle dest_rect = new Rectangle(x, y, clipboard_image.Width, clipboard_image.Height); gr.DrawImage(clipboard_image, dest_rect, source_rect, GraphicsUnit.Pixel); } picOriginal.Refresh(); }

The code calls GetClipboardImage to get the image data if it is available. If the data is available, the code makes a Graphics object associated with the PictureBox's image and draws the image data to the location you clicked.

Use the example Copy an irregular area from a picture to the clipboard in C# to test pasting images with transparent pixels. Use MS Paint to test pasting bitmap data without transparency data. Use its Free-Form Select tool to select an irregular area, copy it to the clipboard, and see what happens when you paste it into this program.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.