22

This similar question's answers all require the file to be saved. However, I'm trying to convert the file and then copy it to the clipboard.

How can I convert a Bitmap (or any image) to a PNG without saving it to the file system?

Update:
I'm trying to paste the image into an application (in this case Evernote). When you copy an image into the clipboard (e.g. via the browser), it remembers its image format and when you paste it in, it will create an image with the same exact format. For example, if you copy a PNG, it will paste a PNG. If you copy a JPG, it will paste a JPG, etc.

I am trying to take whatever image is currently in the clipboard, scale it to the size I want, and then keep it in the clipboard as a PNG, such that when it is pasted into Evernote, it will create a PNG.

When I copy a PNG image in my browser, I see the following formats: HTML FORMAT, CF_BITMAP, CF_DIB, CF_DIBV5. I'm not sure which of these Evernote is using for pasting. I was under the impression that it was CF_BITMAP, but after reading the comments below, I guess it's using one of the other formats.

How can I place an image in the clipboard which will be treated as a PNG when pasted?

Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465
  • where do you plan on pasting it? I'm thinking the clipboard doesn't store images in a file format like jpg/png. unless you are planning on pasting it into a directory as a file? – russau Aug 19 '10 at 02:32
  • @russau: I updated the question to clarify what I'm trying to do. – Senseful Aug 19 '10 at 11:57
  • _"When you copy an image into the clipboard (e.g. via the browser), it remembers its image format"_ - This is completely false. You are probably confusing this because some applications use a png memory stream to compensate for the fact the standard Windows clipboard has no transparency support. – Nyerguds Jan 05 '18 at 14:27

1 Answers1

35

Save the Bitmap to a MemoryStream

byte[] result = null;
using (MemoryStream stream = new MemoryStream())
{
    bitmap.Save(stream, ImageFormat.Png);
    result = stream.ToArray();
}
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
russau
  • 8,928
  • 6
  • 39
  • 49
  • 1
    Could you please include the code necessary to convert it to an `Image`? (I'm trying to figure this out myself right now and I'll post it as a comment so you can copy/paste) – Senseful Aug 19 '10 at 01:07
  • 3
    My best guess is `Image result = Image.FromStream(stream);` instead of the `result = ...` line. If you agree, could you please update the answer? – Senseful Aug 19 '10 at 01:11
  • 1
    @eagle: Am `Image` is an Image. It's only a Bmp/Png/Jpg/etc when it is written to a stream (file or memory). – James Curran Aug 19 '10 at 01:27
  • @eagle: You already have a `Bitmap`. `Bitmap` derives from `Image`, so it’s already an `Image` to begin with. – Timwi Aug 19 '10 at 02:10
  • @James, @Timwi: I updated the question to clarify what I'm trying to do. – Senseful Aug 19 '10 at 11:58
  • Byte arrays like png [need to be put on the clipboard as MemoryStream anyway](https://stackoverflow.com/a/46580838/395685); no need to convert it to array. – Nyerguds Jan 04 '18 at 17:53