如何从Clipboard类获取图像格式? [英] How to get the image format from Clipboard class?

查看:105
本文介绍了如何从Clipboard类获取图像格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#制作图像查看器。该功能是使用C#上的Clipboard类进行复制和粘贴的功能。

I'm making a image viewer in C#. that feature is copy and paste function using Clipboard class on C#.

我成功获取了BitmapSource的图像。但是无法检查图像格式(jpeg,png,bmp)。

And I succeeded to get image of BitmapSource. but it can not check image formats (jpeg, png, bmp).

这是C#支持的源
BitmapSource源=剪贴板.GetImage();

这些是我要使用的。
byte [] image = Clipboard.GetImage( image / png)
byte [] image = Clipboard.GetImage (图像/ bmp)

These are what i want to use. byte[] image = Clipboard.GetImage("image/png") byte[] image = Clipboard.GetImage("image/bmp")

我会说,如何从BitmapSource或剪贴板中检查图像格式?

I will say, How do I check image format from BitmapSource or Clipboard?

推荐答案

简单地说,不能。

图像以原始格式保存在剪贴板上位图。某些应用程序也将其以png格式显示,但这并不是为了保留原始格式,而只是以更好地支持alpha-transparency

Images are saved on the clipboard as raw bitmap. Some applications also put it on in png format, but that's not to preserve the original, but just to better support alpha-transparency.

但是,一般而言,剪贴板没有任何指示,表明该图像来自何处,或原始格式是什么。您还必须意识到剪贴板上的图像可能根本不是文件。这就像假设任何复制的文本必须是带有文件名的完整文本文件。该图像可能是直接从某些编辑器复制的,甚至可能是直接的[Pri​​ntScreen]按钮屏幕截图。

But, in general, the clipboard contains no indication at all of where that image came from, or what format it originally had. You also have to realize that that image on the clipboard might never have been a file at all. That's like assuming any copied piece of text must have been a complete text file, with a filename. That image might've been copied straight from some editor, or might even be a direct [PrintScreen]-button screenshot.

但是... >

已经注意到,当我从Chrome复制图像并将其粘贴到Discord中时,Discord会以某种方式知道它在Chrome中的文件名。这是因为您可以同时将多种格式的数据放在剪贴板上。

I have noticed that when I copy an image from Chrome and paste it in Discord, Discord somehow knows the file name it had in Chrome. This is because you can put data on the clipboard in multiple formats simultaneously.

此系统通常旨在确保每个应用程序都可以读取最适合的内容它。例如,对于从网页复制的内容,记事本将首选纯文本版本,而Microsoft Word将采用HTML(如果可用)。同样,正如我所提到的,应用程序通常会复制位图和png中的图像,以确保接收支持透明性的应用程序可以采用支持透明性的PNG版本,而不了解使用PNG的新趋势的应用程序可以采用经典位图。

This system is generally meant to make sure that every application can read the content that's most optimally suited for it. For example, for content copied from a web page, Notepad will prefer the plain text version, while Microsoft Word will take HTML if available. Likewise, as I mentioned, applications often copy images in bitmap and png to make sure receiving applications that support transparency can take the transparency-supporting PNG version, while applications that are not aware of the rather new trend to use PNG can take the classic bitmap.

但是许多应用程序也使用此功能将元数据添加到复制的内容中。当我在Chrome中复制头像的32x32缩略图时,剪贴板中的一件事是该文本片段被标识为 HTML格式

But a lot of applications also use this to add metadata to the copied content. When I copy the 32x32 thumbnail of your avatar in Chrome, one of the things in the clipboard was this text snippet identified as HTML Format:

Version:0.9
StartHTML:0000000105
EndHTML:0000000238
StartFragment:0000000141
EndFragment:0000000202
<html>
<body>
<!--StartFragment--><img src="https://i.stack.imgur.com/Eql2x.jpg?s=32&amp;g=1"/><!--EndFragment-->
</body>
</html>

(我相信指定HTML Fragment信息的标头可能是标准格式。 ,但是。)

(I believe that the header specifying the HTML Fragment information might be a standard format. Never looked into that, though.)

如您所见,其中包含文件名,从中通常可以得到一个扩展名,该扩展名可用于确定文件的原始格式。

As you see, this contains the file name, and from that, you usually got an extension that can be used to determine the original format the file was in.

,但是文件不再是这种格式。因为这只是剪贴板上的数据,而不是实际文件。复制图像后,Chrome剪贴板的完整转储包含以下格式:

But the file is no longer in that format. Because, it's just data on the clipboard, not an actual file. The full dump of the Chrome clipboard after copying an image contained the following formats:


  • System.Drawing.Bitmap (.Net位图对象。在WPF上显示的可能不同)

  • 位图(与 System.Drawing.Bitmap 我认为)

  • HTML格式(显示的摘录

  • DeviceIndependentBitmap (包含DIB v1图像字节的字节流;在剪贴板上传输图像的最常见方式; 因为它是32位RGB,经常被滥用为具有Alpha功能

  • Format17 (一个字节流,其中包含DIB v5图像的字节;比v1略高一些,并正式支持alpha。 17指的是旧的Windows版本中使用的旧的数字剪贴板格式)

  • System.Drawing.Bitmap (a .Net Bitmap object. This might be exposed differently on WPF)
  • Bitmap (Same as System.Drawing.Bitmap I think)
  • HTML Format (the snippet shown above)
  • DeviceIndependentBitmap (A byte stream containing bytes for a DIB v1 image; the most common way of transferring images on the clipboard; often abused as alpha-capable because it's 32 bit RGB)
  • Format17 (A byte stream containing bytes for a DIB v5 image; slightly more advanced than v1, and officially supports alpha. The '17' refers to the old numeric clipboard formats used in old windows versions)

如您所见,其中没有jpeg文件。因为在剪贴板级别上,永远不会保留原始文件格式。甚至设法读取并保留文件名的Discord也会将粘贴的文件另存为png,并根据需要调整文件扩展名。

As you see, there's no jpeg file in there. Because the original file format is never assumed to be preserved when you're at clipboard level. Even Discord, which manages to read and preserve the filename, will save the pasted file as png, adjusting the file extension if needed.

请注意,这正是Chrome的工作方式可以。无法保证来自其他应用程序甚至其他浏览器的剪贴板副本将发送元数据,并且如果发送的话,甚至是略微可比的格式。

Do note that this is just how Chrome does it. There are no guarantees that the clipboard copy from other applications, or even from other browsers, will send metadata, and if they do, if it is in an even slightly comparable format.

我用来分析剪贴板数据的代码:

The code I used to analyse the clipboard data:

(请注意,这是Windows窗体代码。我没有关于其WPF等效项的想法)

(Note, this is Windows Forms code. I have no idea about its WPF equivalents)

DataObject retrievedData = (DataObject)Clipboard.GetDataObject();
if (retrievedData == null)
    return;
String[] formats = retrievedData.GetFormats();
foreach (String format in formats)
{
    Object contents = retrievedData.GetData(format);
    MemoryStream ms = contents as MemoryStream;
    Byte[] bContents = ms == null ? null : ms.ToArray();
    String sContents = contents as String;

    // Check if bContents and sContents are null here, and analyse their contents

    // ...
}

这篇关于如何从Clipboard类获取图像格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆