From 7dda2df54b4f0d1afd98460e3a0825cf352d7a1b Mon Sep 17 00:00:00 2001 From: Yeechan Lu Date: Sun, 23 Mar 2014 04:22:57 +0800 Subject: [PATCH] Support data URI as Icon --- Wox/Helper/DataWebRequestFactory.cs | 72 +++++++++++++++++++++++++++++ Wox/ImagePathConverter.cs | 6 +++ Wox/MainWindow.xaml.cs | 2 +- Wox/Wox.csproj | 1 + 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Wox/Helper/DataWebRequestFactory.cs diff --git a/Wox/Helper/DataWebRequestFactory.cs b/Wox/Helper/DataWebRequestFactory.cs new file mode 100644 index 0000000000..7e474bd4b2 --- /dev/null +++ b/Wox/Helper/DataWebRequestFactory.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; + +namespace Wox.Helper +{ + public class DataWebRequestFactory : IWebRequestCreate + { + class DataWebRequest : WebRequest + { + private readonly Uri m_uri; + + public DataWebRequest(Uri uri) + { + m_uri = uri; + } + + public override WebResponse GetResponse() + { + return new DataWebResponse(m_uri); + } + } + + class DataWebResponse : WebResponse + { + private readonly string m_contentType; + private readonly byte[] m_data; + + public DataWebResponse(Uri uri) + { + string uriString = uri.AbsoluteUri; + + int commaIndex = uriString.IndexOf(','); + var headers = uriString.Substring(0, commaIndex).Split(';'); + m_contentType = headers[0]; + string dataString = uriString.Substring(commaIndex + 1); + m_data = Convert.FromBase64String(dataString); + } + + public override string ContentType + { + get { return m_contentType; } + set + { + throw new NotSupportedException(); + } + } + + public override long ContentLength + { + get { return m_data.Length; } + set + { + throw new NotSupportedException(); + } + } + + public override Stream GetResponseStream() + { + return new MemoryStream(m_data); + } + } + + public WebRequest Create(Uri uri) + { + return new DataWebRequest(uri); + } + } +} diff --git a/Wox/ImagePathConverter.cs b/Wox/ImagePathConverter.cs index c6122869b1..395dda6dc7 100644 --- a/Wox/ImagePathConverter.cs +++ b/Wox/ImagePathConverter.cs @@ -8,6 +8,7 @@ using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; namespace Wox { @@ -40,6 +41,11 @@ namespace Wox if (values[0] == null) return null; string path = values[0].ToString(); + if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) + { + return new System.Windows.Media.Imaging.BitmapImage(new Uri(path)); + } + string pluginDirectory = values[1].ToString(); string fullPath = Path.Combine(pluginDirectory, path); string ext = Path.GetExtension(path).ToLower(); diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index aa6289809f..abafd9b3d3 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -50,7 +50,7 @@ namespace Wox InitializeComponent(); initialized = true; - + System.Net.WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; progressBar.ToolTip = toolTip; diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 3cefced8cf..f3b1bc0a7c 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -107,6 +107,7 @@ + ProgramSourceSetting.xaml