BLOG.CSHARPHELPER.COM: Paste a PNG format image with a transparent background from the clipboard in C#
Paste a PNG format image with a transparent background from the clipboard in C#
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 and, 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(); }
This method uses the clipboard's ContainsData method to see if PNG data is available. If it is, the code uses the clipboard'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's ContainsImage and GetImage methods to get bitmap data. If that also fails, it returns null.
The following code shows how the program uses the GetClipboardImage method.
// Paste any image that's available. private void btnPaste_Click(object sender, EventArgs 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; }
// Copy the original image. Image new_image = new Bitmap(OriginalImage);
// Draw the image in the middle of the original image. using (Graphics gr = Graphics.FromImage(new_image)) { Rectangle source_rect = new Rectangle(0, 0, clipboard_image.Width, clipboard_image.Height); Rectangle dest_rect = new Rectangle( (OriginalImage.Width - clipboard_image.Width) / 2, (OriginalImage.Height - clipboard_image.Height) / 2, clipboard_image.Width, clipboard_image.Height); gr.DrawImage(clipboard_image, dest_rect, source_rect, GraphicsUnit.Pixel); }
// Display the result. picOriginal.Image = new_image; }
The code calls GetClipboardImage to get the image if it is available. It makes a copy of the program's original image and draws the clipboard image into the middle of it.
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.
Comments