public static string GetHtml() { if (!Clipboard.ContainsText()) { return(null); } if (Clipboard.ContainsText(TextDataFormat.Html)) { var html = ClipboardHtmlOutput.FromClipboard(); if (html != null) { return(html.Fragment); } } if (Clipboard.GetDataObject().GetDataPresent("text/html")) { var data = Clipboard.GetData("text/html"); if (data is Stream) { var stream = (Stream)data; var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); return(Encoding.Unicode.GetString(buffer)); } } return(Clipboard.GetText()); }
static ClipboardHtmlOutput ParseData(byte[] buffer) { var html = new ClipboardHtmlOutput(); html.InputData = buffer; string text = Encoding.Default.GetString(buffer); //string pattern = @"Version:(?<version>[0-9]+(?:\.[0-9]*)?).+StartHTML:(?<startH>\d*).+EndHTML:(?<endH>\d*).+StartFragment:(?<startF>\d+).+EndFragment:(?<endF>\d*).+SourceURL:(?<source>f|ht{1}tps?://[-a-zA-Z0-9@:%_\+.~#?&//=]+)"; string pattern = @"Version:(?<version>[0-9]+(?:\.[0-9]*)?)[\s\S]*?StartHTML:(?<startH>\d*)[\s\S]*?EndHTML:(?<endH>\d*)[\s\S]*?StartFragment:(?<startF>\d+)[\s\S]*?EndFragment:(?<endF>\d*)[\s\S]*?"; var match = Regex.Match(text, pattern, RegexOptions.Singleline); if (match.Success) { try { //html.Input = text; html.Version = Double.Parse(match.Groups["version"].Value, CultureInfo.InvariantCulture); html.Source = match.Groups["source"].Value; html.startHTML = int.Parse(match.Groups["startH"].Value); html.endHTML = int.Parse(match.Groups["endH"].Value); html.startFragment = int.Parse(match.Groups["startF"].Value); html.endFragment = int.Parse(match.Groups["endF"].Value); } catch (Exception) { return(null); } return(html); } return(null); }
public static void SetHtml(DataObject dataObject, string html) { dataObject.SetData(DataFormats.Html, ClipboardHtmlOutput.Package(html)); //dataObject.SetText(html, TextDataFormat.Html); }