Convert BMP to PNG in memory for Clipboard pasting in .Net

我怕爱的太早我们不能终老 提交于 2019-12-01 13:44:29

问题


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?


回答1:


Save the Bitmap to a MemoryStream

byte[] result = null;
using (MemoryStream stream = new MemoryStream())
{
    bitmap.Save(stream, ImageFormat.Png);
    result = stream.ToArray();
}


来源:https://stackoverflow.com/questions/3517965/convert-bmp-to-png-in-memory-for-clipboard-pasting-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!