动态更改图像编码?
我想知道是否可以以编程方式动态更改图像编码而不将图像保存到文件中?
用例:当用户从源复制二进制图像时,是否可以将图像编码从 binary
更改为 base64
?
I want to know whether it is possible to programmatically change an image encoding on the fly without saving the image to a file?
Use Case: When the user copies a binary image from a source, would it be possible to change the image encoding from binary
to base64
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以更改图像的编码而不将其保存到文件中,但不能不将其保存到代码中的变量中。 Clipboard 类基本上只有一些 Get 和 Set 方法。更改剪贴板中内容的唯一方法是将 Get 方法之一调用到局部变量中,更改您刚刚获得的任何内容,然后调用 Set 方法之一,传入更改的对象。这会导致剪贴板对象发生变化,但并非没有将其“保存”到变量的中间步骤。
Clipboard 不公开任何直接操作剪贴板中对象内存的方法。即使公开了这样的方法,将图像的编码从二进制更改为 Base64 也会从根本上更改所有内存,因此它不会有太大价值。
更新:这里有一个方法,可以从剪贴板中获取图像,将其转换为 base64 字符串,然后将其放回剪贴板:
并且您将需要这两个 using 语句:
它未经测试(因为已经过了我的就寝时间),但它应该可以工作。它确实涉及到局部变量的使用,但这是不可避免的(也是正常的)。
You could change the encoding of an image without saving it to a file, but not without saving it to a variable in your code. The Clipboard class basically just has some Get and Set methods. The only way to change what's in the clipboard is to call one of the Get methods into a local variable, change whatever it is you just got, and then call one of the Set methods, passing in your changed object. This results in a changed clipboard object, but not without the intermediate step of "saving" it to a variable.
Clipboard does not expose any methods for directly manipulating the memory of the object in the clipboard. Even if such a method were exposed, changing the encoding of an image from binary to Base64 involves fundamentally changing all of the memory, so there wouldn't be much value to it.
Update: here's a method that will take an image from the clipboard, convert it to a base64 string, and put it back into the clipboard:
And you'll need these two using statements:
It's untested (because it's past my bedtime), but it should work. It does involve the use of local variables, but this is unavoidable (as well as normal).
在 WPF 中使用新的 ClipBoard 类
下面的示例从文件中读取流,但您可以使用任何流
http://msdn.microsoft.com/en-us/library/system.windows.clipboard.setimage.aspx
Using the new ClipBoard class in WPF
The below example reads a stream from a file but you could use any stream
http://msdn.microsoft.com/en-us/library/system.windows.clipboard.setimage.aspx
我认为您可能会问如何拦截复制操作并用新内容替换剪贴板的逻辑内容(全部来自某些后台应用程序),而不是如何替换分配给原始复制操作的内存字节。
如果这是目的,您应该寻找 Win32 API 调用来挂接到剪贴板,以便后台应用程序可以在复制数据可用于粘贴之前对其进行处理。
本文可能会让您有所帮助:
http://www.radsoftware.com.au/articles /clipboardmonitor.aspx
I think that you might be asking how to intercept the copy operation and replace the logical contents of the clipboard with new contents, all from some background app, rather than how to replace the memory bytes allocated to the original copy operation.
If that's the intention, you should look for Win32 API calls to hook into the clipboard so that the background app can process the data copied before it is available for paste.
This article might get you going:
http://www.radsoftware.com.au/articles/clipboardmonitor.aspx