private void UpdateImage(Uri uri, bool ignoreCache)
{
if (uri.IsAbsoluteUri)
{
System.Configuration.Configuration config = null;
string directory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
if (Directory.Exists(directory))
{
string fileName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
foreach (string s in from s in Directory.EnumerateFiles(directory, "*.config") where fileName.Equals(System.IO.Path.GetFileNameWithoutExtension(s)) select s)
{
System.Configuration.ExeConfigurationFileMap exeConfigurationFileMap = new System.Configuration.ExeConfigurationFileMap();
exeConfigurationFileMap.ExeConfigFilename = s;
config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, System.Configuration.ConfigurationUserLevel.None);
}
}
if (config == null)
{
config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
directory = null;
}
if (config.AppSettings.Settings["Cache"] == null)
{
if (uri.Scheme.Equals("data"))
{
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(uri.LocalPath, "image/(?:(?:x-)?bmp|gif|jpeg|png|tiff(?:-fx)?);base64,(?<1>.+)", System.Text.RegularExpressions.RegexOptions.CultureInvariant);
if (match.Success)
{
MemoryStream ms = new MemoryStream(Convert.FromBase64String(match.Groups[1].Value));
BitmapImage bi = null;
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
ms.Close();
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}
}
else if (uri.Scheme.Equals(Uri.UriSchemeFile) || uri.Scheme.Equals(Uri.UriSchemeFtp) || uri.Scheme.Equals(Uri.UriSchemeHttp) || uri.Scheme.Equals(Uri.UriSchemeHttps))
{
WebRequest webRequest = WebRequest.Create(uri);
if (config.AppSettings.Settings["Timeout"] != null)
{
if (config.AppSettings.Settings["Timeout"].Value.Length > 0)
{
webRequest.Timeout = Int32.Parse(config.AppSettings.Settings["Timeout"].Value, CultureInfo.InvariantCulture);
}
}
if (config.AppSettings.Settings["UserAgent"] != null)
{
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
if (httpWebRequest != null)
{
httpWebRequest.UserAgent = config.AppSettings.Settings["UserAgent"].Value;
}
}
Task.Factory.StartNew<MemoryStream>(delegate (object state)
{
MemoryStream ms = null;
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
WebRequest request = (WebRequest)state;
WebResponse response = null;
Stream s = null;
BufferedStream bs = null;
try
{
response = request.GetResponse();
if (System.Text.RegularExpressions.Regex.IsMatch(response.ContentType, "image/((x-)?bmp|gif|jpeg|png|tiff(-fx)?)", System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
s = response.GetResponseStream();
bs = new BufferedStream(s);
s = null;
ms = new MemoryStream();
int i;
while ((i = bs.ReadByte()) != -1)
{
ms.WriteByte((byte)i);
}
ms.Seek(0, SeekOrigin.Begin);
}
}
catch
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
finally
{
if (bs != null)
{
bs.Close();
}
if (s != null)
{
s.Close();
}
if (response != null)
{
response.Close();
}
}
}
return ms;
}, webRequest, TaskCreationOptions.LongRunning).ContinueWith(delegate (Task<MemoryStream> task)
{
BitmapImage bi = null;
if (task.Result != null)
{
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = task.Result;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
task.Result.Close();
}
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
else
{
System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
StringBuilder stringBuilder = new StringBuilder();
foreach (byte b in sha1.ComputeHash(Encoding.UTF8.GetBytes(uri.AbsoluteUri)))
{
stringBuilder.Append(b.ToString("x2", System.Globalization.CultureInfo.InvariantCulture));
}
stringBuilder.Append(System.IO.Path.GetExtension(uri.AbsolutePath));
if (stringBuilder.ToString().IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) < 0)
{
string path1 = directory == null ? config.AppSettings.Settings["Cache"].Value : System.IO.Path.Combine(directory, config.AppSettings.Settings["Cache"].Value);
string path2 = System.IO.Path.Combine(path1, stringBuilder.ToString());
if (!ignoreCache && File.Exists(path2))
{
Task.Factory.StartNew<MemoryStream>(delegate
{
MemoryStream ms = null;
FileStream fs = null;
try
{
fs = new FileStream(path2, FileMode.Open, FileAccess.Read, FileShare.Read);
ms = new MemoryStream();
byte[] buffer = new byte[fs.Length];
int bytesRead;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytesRead);
}
ms.Seek(0, SeekOrigin.Begin);
}
catch
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
finally
{
if (fs != null)
{
fs.Close();
}
}
return ms;
}).ContinueWith(delegate (Task<MemoryStream> task)
{
BitmapImage bi = null;
if (task.Result != null)
{
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = task.Result;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
task.Result.Close();
}
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
else if (uri.Scheme.Equals("data"))
{
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(uri.LocalPath, "image/(?:(?:x-)?bmp|gif|jpeg|png|tiff(?:-fx)?);base64,(?<1>.+)", System.Text.RegularExpressions.RegexOptions.CultureInvariant);
if (match.Success)
{
byte[] bytes = Convert.FromBase64String(match.Groups[1].Value);
Task.Factory.StartNew<MemoryStream>(delegate
{
FileStream fs = null;
MemoryStream ms = null;
try
{
if (!Directory.Exists(path1))
{
Directory.CreateDirectory(path1);
}
fs = new FileStream(path2, FileMode.Create, FileAccess.Write, FileShare.None);
ms = new MemoryStream();
foreach (byte b in bytes)
{
fs.WriteByte(b);
ms.WriteByte(b);
}
fs.Flush();
ms.Seek(0, SeekOrigin.Begin);
}
catch
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
finally
{
if (fs != null)
{
fs.Close();
}
}
return ms;
}, TaskCreationOptions.LongRunning).ContinueWith(delegate (Task<MemoryStream> task)
{
BitmapImage bi = null;
if (task.Result != null)
{
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = task.Result;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
task.Result.Close();
}
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
else if (uri.Scheme.Equals(Uri.UriSchemeFile) || uri.Scheme.Equals(Uri.UriSchemeFtp) || uri.Scheme.Equals(Uri.UriSchemeHttp) || uri.Scheme.Equals(Uri.UriSchemeHttps))
{
WebRequest webRequest = WebRequest.Create(uri);
if (config.AppSettings.Settings["Timeout"] != null)
{
if (config.AppSettings.Settings["Timeout"].Value.Length > 0)
{
webRequest.Timeout = Int32.Parse(config.AppSettings.Settings["Timeout"].Value, CultureInfo.InvariantCulture);
}
}
if (config.AppSettings.Settings["UserAgent"] != null)
{
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
if (httpWebRequest != null)
{
httpWebRequest.UserAgent = config.AppSettings.Settings["UserAgent"].Value;
}
}
Task.Factory.StartNew<MemoryStream>(delegate (object state)
{
MemoryStream ms = null;
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
WebRequest request = (WebRequest)state;
WebResponse response = null;
Stream s = null;
BufferedStream bs = null;
FileStream fs = null;
try
{
response = request.GetResponse();
if (System.Text.RegularExpressions.Regex.IsMatch(response.ContentType, "image/((x-)?bmp|gif|jpeg|png|tiff(-fx)?)", System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
s = response.GetResponseStream();
bs = new BufferedStream(s);
s = null;
if (!Directory.Exists(path1))
{
Directory.CreateDirectory(path1);
}
fs = new FileStream(path2, FileMode.Create, FileAccess.Write, FileShare.None);
ms = new MemoryStream();
int i;
while ((i = bs.ReadByte()) != -1)
{
byte b = (byte)i;
fs.WriteByte(b);
ms.WriteByte(b);
}
fs.Flush();
ms.Seek(0, SeekOrigin.Begin);
}
}
catch
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
finally
{
if (fs != null)
{
fs.Close();
}
if (bs != null)
{
bs.Close();
}
if (s != null)
{
s.Close();
}
if (response != null)
{
response.Close();
}
}
}
return ms;
}, webRequest, TaskCreationOptions.LongRunning).ContinueWith(delegate (Task<MemoryStream> task)
{
BitmapImage bi = null;
if (task.Result != null)
{
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = task.Result;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
task.Result.Close();
}
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
else if (uri.Scheme.Equals("data"))
{
System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(uri.LocalPath, "image/(?:(?:x-)?bmp|gif|jpeg|png|tiff(?:-fx)?);base64,(?<1>.+)", System.Text.RegularExpressions.RegexOptions.CultureInvariant);
if (match.Success)
{
MemoryStream ms = new MemoryStream(Convert.FromBase64String(match.Groups[1].Value));
BitmapImage bi = null;
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
ms.Close();
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}
}
else if (uri.Scheme.Equals(Uri.UriSchemeFile) || uri.Scheme.Equals(Uri.UriSchemeFtp) || uri.Scheme.Equals(Uri.UriSchemeHttp) || uri.Scheme.Equals(Uri.UriSchemeHttps))
{
WebRequest webRequest = WebRequest.Create(uri);
if (config.AppSettings.Settings["Timeout"] != null)
{
if (config.AppSettings.Settings["Timeout"].Value.Length > 0)
{
webRequest.Timeout = Int32.Parse(config.AppSettings.Settings["Timeout"].Value, CultureInfo.InvariantCulture);
}
}
if (config.AppSettings.Settings["UserAgent"] != null)
{
HttpWebRequest httpWebRequest = webRequest as HttpWebRequest;
if (httpWebRequest != null)
{
httpWebRequest.UserAgent = config.AppSettings.Settings["UserAgent"].Value;
}
}
Task.Factory.StartNew<MemoryStream>(delegate (object state)
{
MemoryStream ms = null;
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
WebRequest request = (WebRequest)state;
WebResponse response = null;
Stream s = null;
BufferedStream bs = null;
try
{
response = request.GetResponse();
if (System.Text.RegularExpressions.Regex.IsMatch(response.ContentType, "image/((x-)?bmp|gif|jpeg|png|tiff(-fx)?)", System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
s = response.GetResponseStream();
bs = new BufferedStream(s);
s = null;
ms = new MemoryStream();
int i;
while ((i = bs.ReadByte()) != -1)
{
ms.WriteByte((byte)i);
}
ms.Seek(0, SeekOrigin.Begin);
}
}
catch
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
finally
{
if (bs != null)
{
bs.Close();
}
if (s != null)
{
s.Close();
}
if (response != null)
{
response.Close();
}
}
}
return ms;
}, webRequest, TaskCreationOptions.LongRunning).ContinueWith(delegate (Task<MemoryStream> task)
{
BitmapImage bi = null;
if (task.Result != null)
{
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = task.Result;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
task.Result.Close();
}
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
else
{
Task.Factory.StartNew<MemoryStream>(delegate (object state)
{
MemoryStream ms = null;
FileStream fs = null;
try
{
fs = new FileStream((string)state, FileMode.Open, FileAccess.Read, FileShare.Read);
ms = new MemoryStream();
byte[] buffer = new byte[fs.Length];
int bytesRead;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytesRead);
}
ms.Seek(0, SeekOrigin.Begin);
}
catch
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
finally
{
if (fs != null)
{
fs.Close();
}
}
return ms;
}, uri.ToString()).ContinueWith(delegate (Task<MemoryStream> task)
{
BitmapImage bi = null;
if (task.Result != null)
{
try
{
bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = task.Result;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.None;
bi.EndInit();
}
catch
{
bi = null;
}
finally
{
task.Result.Close();
}
}
if (this.imageUriHashSet.Contains(uri))
{
if (this.imageDictionary.ContainsKey(uri))
{
this.imageDictionary[uri] = bi;
}
else
{
this.imageDictionary.Add(uri, bi);
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}