C# 獲取剪切(粘貼)板的內容(圖片、文字)

Clipboard類、 Clipboard.GetDataObject() 、iData.GetDataPresent()…

DataFormats類型:
Bitmap 指定 Microsoft Windows 位圖數據格式。
CommaSeparatedValue 指定以逗號分隔的值 (CSV) 數據格式。
Dib 指定 device-independent bitmap (DIB) 數據格式。
Dif 指定 Windows 數據交換格式 (DIF) 數據格式。
EnhancedMetafile 指定 Windows 增強型圖元文件格式。
FileDrop 指定 Windows 文件放置格式。
Html 指定 HTML 數據格式。
Locale 指定 Windows 區域設置(區域性)數據格式。
MetafilePicture 指定 Windows 圖元文件圖片數據格式。
OemText 指定標準 Windows OEM 文本數據格式。
Palette 指定 Windows 調色板數據格式。
PenData 指定 Windows 鋼筆數據格式。
Riff 指定 Resource Interchange File Format (RIFF) 音頻數據格式。
Rtf 指定 Rich Text Format (RTF) 數據格式。
Serializable 指定封裝任何類型的可序列化數據對象的數據格式。
StringFormat 指定 common language runtime (CLR) 字符串類數據格式。
SymbolicLink 指定 Windows 符號鏈接數據格式。
Text 指定 ANSI 文本數據格式。
Tiff 指定 Tagged Image File Format (TIFF) 數據格式。
UnicodeText 指定 Unicode 文本數據格式。
WaveAudio 指定波形音頻數據格式。
Xaml 指定 Extensible Application Markup Language (XAML) 數據格式。
XamlPackage 指定 Extensible Application Markup Language (XAML) 包數據格式。

獲取圖片:

			IDataObject iData = Clipboard.GetDataObject(); 
            if (iData.GetDataPresent(DataFormats.MetafilePict))
            {
                var img = Clipboard.GetImage();
                picBox.Tag = Guid.NewGuid();
                picBox.Image = img; 
            }
            else if (iData.GetDataPresent(DataFormats.FileDrop))
            {
                var files = Clipboard.GetFileDropList();
                if (files.Count == 0) { return; }
                picBox.Tag = Guid.NewGuid();
                picBox.Image = Image.FromFile(files[0]); 
            }

            else if (iData.GetDataPresent(DataFormats.Text))
            {
                var path = (String)iData.GetData(DataFormats.Text); 
                var chars = Path.GetInvalidPathChars();
                if (path.IndexOfAny(chars)>=0) {
                    F.error("路徑中包含非法字符!");
                    return;
                }
               if (System.IO.File.Exists(path))
                {
                    var name = Path.GetFileNameWithoutExtension(path);
                    var extension = path.Substring(path.LastIndexOf("."));
                    string imgType = ".png|.jpg|.jpeg";
                    if (imgType.Contains(extension.ToLower()))
                    {
                        picBox.Image = Image.FromFile(path);
                        picBox.Tag = Guid.NewGuid();
                        IsChanged = true;
                    }
                    else {
                        F.error("格式錯誤!");
                    }
                  
                }
                else {
                    F.error("文件不存在!");
                }
            }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章