检测剪贴板上的任何图像

发布于 2024-11-13 09:11:20 字数 203 浏览 3 评论 0原文

现在我想阻止用户将任何带有任何类型图像的内容粘贴到富文本框中。 Clipboard.ContainsImage 不起作用,因为它只检测某些类型,并且如果剪贴板包含带有图像的文本,则不会检测到。

我需要的是一种方法来检测剪贴板内文本内的图像、单独的图像、多个图像......以便能够在发生这种情况时清除剪贴板。我需要避免这些繁重的内容到达数据库......

谢谢

Right now I want to prevent the user from pasting into a rich text box any content with an image of any type. Clipboard.ContainsImage won't work as it only detects some types and it won't be detected if the clipboard contains text with the image.

What I need is a way to detect an image inside a text, an image alone, multiple images... inside the clipboard to be able to clear the clipboard if this happens. I need to avoid this heavy content to reach the database...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

清旖 2024-11-20 09:11:20

问题不仅仅是阻止图像,因为 RTF 可以嵌入任何对象,包括 Word 文档或任意二进制数据。

我能想到的仅有的 3 个选项

  1. 限制允许保存到数据库的最大数据大小,此时低于该大小的图像不是问题,因为它在允许的大小内。

  2. 使用可让您解析和理解 RTF 内容的库,以便您可以检查除标准文本和格式之外的任何内容。然后您可以删除任何嵌入的二进制对象。或者,您可以处理 WM_PASTE 并检查剪贴板中的 RTF 文本。如果它有 RTF 文本,您可以解析它并在粘贴时而不是保存时剥离嵌入的对象。如果它没有 RTF 版本,请不要忘记检查它是否有纯文本版本。

  3. 拦截并处理WM_PASTE消息并使用RichTextBox.Paste(DateFormats) 控制可以粘贴哪些数据类型。然后,您可以将其限制为仅将纯文本版本的数据粘贴到剪贴板上,其中不能包含嵌入的对象。这里的缺点是用户无法从不同的应用程序粘贴格式化文本,所有格式都将丢失。

1 是最简单的,但接受用户能够嵌入小图像和其他小对象(例如 Word 文档或视频),只要其大小低于最大尺寸。

2 更复杂,但提供了最佳的用户体验。

3 比 2 简单,但用户体验较差,就好像您不关心格式化文本一样,您不会使用富文本框。除非您只想要纯文本并使用其更大的文本容量,但如果是这种情况,您只会获取纯文本版本以保存到数据库,其中不包含图像。

The problem is greater than simply preventing an image, since RTF can embed any object including word documents or arbitrary binary data.

The only 3 options I can think of

  1. Limit the maximum data size you allow saving to the database, at which point images under that size arn't a problem since its with in the allowable size.

  2. Use library that will let you parse and understand the RTF content so you can check it for anything other than standard text and formatting. Then you can strip out any embedded binary objects. Alternatively, you could handle WM_PASTE and check the clipboard for RTF text. If it has RTF text you can parse this and strip embedded objects when pasting rather than when saving. If it doesn't have an RTF version, don't forget to check if it has a plain text version instead.

  3. Intercept and handle the WM_PASTE message and use RichTextBox.Paste(DateFormats) to control which data types can be pasted. You can then limit this to only pasting the plain text version of the data on the clipboard, which cannot contain embedded objects. The disadvantage here is the user can't paste formatted text from a different application, all formatting will be lost.

1 is the simplest, but accepts that users will be able to embed small images and other small objects such as word documents or videos, so long as it is under the maximum size.

2 is more complex but offers the best user experience.

3 is simpler than 2, but offers a poor user experience, as if you didn't care about formatted text you wouldn't be using a rich text box. Unless you only want plain text and are using for its larger text capacity, but if this were the case you'd only be fetching the plain text version to save to the DB, which wouldn't contain images.

许仙没带伞 2024-11-20 09:11:20

下面显示什么?

 IDataObject clipData = Clipboard.GetDataObject();
 var formats = clipData.GetFormats();

我希望它包含像 DeviceIndependentBitmap 这样的东西作为可用格式之一,所以也许你可以寻找它并清除禁用粘贴这种情况。

或者,如果您查看剪贴板并仅查找文本,您会得到什么?例如,

 IDataObject clipData = Clipboard.GetDataObject();
... clipData.GetData(System.Windows.Forms.DataFormats.Text); 

我知道对于普通图像,这将为空,如果您的混合模式情况如此,也许您可​​以在这种情况下清除剪贴板。

What does the following show?

 IDataObject clipData = Clipboard.GetDataObject();
 var formats = clipData.GetFormats();

I would expect it to include something like DeviceIndependentBitmap as one of the available formats, so perhaps you can look for that and clear the disable the paste such cases.

Alternatively, what do you get if you peek at the clipboard and just look for text? eg

 IDataObject clipData = Clipboard.GetDataObject();
... clipData.GetData(System.Windows.Forms.DataFormats.Text); 

I know for ordinary images this will be null and if this is true for your mixed mode case, perhaps you can clear the clipboard in that case.

临走之时 2024-11-20 09:11:20

这个简单的控制台应用程序将向您显示剪贴板中的数据类型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // reference is added manually.

namespace ctt // Clipboard Types Tester
{
    class Program
    {
        [STAThread] // it is important! Without this Clipboard.GetDataObject() returns null. 
        static void Main()
        {
            // 'formatsAll' is from http://www.radsoftware.com.au/articles/clipboardmonitor.aspx
            string[] formatsAll = new string[] 
            {
                DataFormats.Bitmap,
                DataFormats.CommaSeparatedValue,
                DataFormats.Dib,
                DataFormats.Dif,
                DataFormats.EnhancedMetafile,
                DataFormats.FileDrop,
                DataFormats.Html,
                DataFormats.Locale,
                DataFormats.MetafilePict,
                DataFormats.OemText,
                DataFormats.Palette,
                DataFormats.PenData,
                DataFormats.Riff,
                DataFormats.Rtf,
                DataFormats.Serializable,
                DataFormats.StringFormat,
                DataFormats.SymbolicLink,
                DataFormats.Text,
                DataFormats.Tiff,
                DataFormats.UnicodeText,
                DataFormats.WaveAudio
            };

            IDataObject data = Clipboard.GetDataObject();

            if (data == null)
                System.Console.WriteLine("Error!");
            else
            {
                bool empty = true;
                foreach (string format in formatsAll)
                    if (data.GetDataPresent(format))
                    {
                        empty = false;
                        break;
                    }

                if (empty) System.Console.WriteLine("Now clipboard is empty.");
                else
                {
                    System.Console.WriteLine("Now clipboard contains the following types:");
                    System.Console.WriteLine();
                    foreach (string format in formatsAll)
                        if (data.GetDataPresent(format))
                            System.Console.WriteLine(format);
                }
            }

            System.Console.ReadKey();
        }
    }
}

This simple console application will show you what data types have you in clipboard:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // reference is added manually.

namespace ctt // Clipboard Types Tester
{
    class Program
    {
        [STAThread] // it is important! Without this Clipboard.GetDataObject() returns null. 
        static void Main()
        {
            // 'formatsAll' is from http://www.radsoftware.com.au/articles/clipboardmonitor.aspx
            string[] formatsAll = new string[] 
            {
                DataFormats.Bitmap,
                DataFormats.CommaSeparatedValue,
                DataFormats.Dib,
                DataFormats.Dif,
                DataFormats.EnhancedMetafile,
                DataFormats.FileDrop,
                DataFormats.Html,
                DataFormats.Locale,
                DataFormats.MetafilePict,
                DataFormats.OemText,
                DataFormats.Palette,
                DataFormats.PenData,
                DataFormats.Riff,
                DataFormats.Rtf,
                DataFormats.Serializable,
                DataFormats.StringFormat,
                DataFormats.SymbolicLink,
                DataFormats.Text,
                DataFormats.Tiff,
                DataFormats.UnicodeText,
                DataFormats.WaveAudio
            };

            IDataObject data = Clipboard.GetDataObject();

            if (data == null)
                System.Console.WriteLine("Error!");
            else
            {
                bool empty = true;
                foreach (string format in formatsAll)
                    if (data.GetDataPresent(format))
                    {
                        empty = false;
                        break;
                    }

                if (empty) System.Console.WriteLine("Now clipboard is empty.");
                else
                {
                    System.Console.WriteLine("Now clipboard contains the following types:");
                    System.Console.WriteLine();
                    foreach (string format in formatsAll)
                        if (data.GetDataPresent(format))
                            System.Console.WriteLine(format);
                }
            }

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