动态更改图像编码?

发布于 2024-08-04 11:08:39 字数 139 浏览 4 评论 0原文

我想知道是否可以以编程方式动态更改图像编码而不将图像保存到文件中?

用例:当用户从源复制二进制图像时,是否可以将图像编码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北渚 2024-08-11 11:08:39

您可以更改图像的编码而不将其保存到文件中,但不能不将其保存到代码中的变量中。 Clipboard 类基本上只有一些 Get 和 Set 方法。更改剪贴板中内容的唯一方法是将 Get 方法之一调用到局部变量中,更改您刚刚获得的任何内容,然后调用 Set 方法之一,传入更改的对象。这会导致剪贴板对象发生变化,但并非没有将其“保存”到变量的中间步骤。

Clipboard 不公开任何直接操作剪贴板中对象内存的方法。即使公开了这样的方法,将图像的编码从二进制更改为 Base64 也会从根本上更改所有内存,因此它不会有太大价值。

更新:这里有一个方法,可以从剪贴板中获取图像,将其转换为 base64 字符串,然后将其放回剪贴板:

if (Clipboard.ContainsImage())
{
    using (MemoryStream memory = new MemoryStream())
    {
        using (Image img = Clipboard.GetImage())
        {
            img.Save(memory, img.RawFormat);
        }
        string base64 = Convert.ToBase64String(memory.ToArray());
        Clipboard.SetText(base64);
    }
}

并且您将需要这两个 using 语句:

using System.IO;
using System.Windows.Forms;

它未经测试(因为已经过了我的就寝时间),但它应该可以工作。它确实涉及到局部变量的使用,但这是不可避免的(也是正常的)。

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:

if (Clipboard.ContainsImage())
{
    using (MemoryStream memory = new MemoryStream())
    {
        using (Image img = Clipboard.GetImage())
        {
            img.Save(memory, img.RawFormat);
        }
        string base64 = Convert.ToBase64String(memory.ToArray());
        Clipboard.SetText(base64);
    }
}

And you'll need these two using statements:

using System.IO;
using System.Windows.Forms;

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).

萧瑟寒风 2024-08-11 11:08:39

在 WPF 中使用新的 ClipBoard 类

下面的示例从文件中读取流,但您可以使用任何流

        var image = new BitmapImage();

        image.BeginInit();
        image.StreamSource = File.Open("image.png", FileMode.Open);
        image.EndInit();

        System.Windows.Clipboard.SetImage(image);

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

        var image = new BitmapImage();

        image.BeginInit();
        image.StreamSource = File.Open("image.png", FileMode.Open);
        image.EndInit();

        System.Windows.Clipboard.SetImage(image);

http://msdn.microsoft.com/en-us/library/system.windows.clipboard.setimage.aspx

も星光 2024-08-11 11:08:39

我认为您可能会问如何拦截复制操作并用新内容替换剪贴板的逻辑内容(全部来自某些后台应用程序),而不是如何替换分配给原始复制操作的内存字节。

如果这是目的,您应该寻找 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文