Skip to content

Commit

Permalink
add image caching to fix #8 and #9
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Jul 5, 2020
1 parent 7efc29b commit da930e5
Showing 1 changed file with 51 additions and 13 deletions.
64 changes: 51 additions & 13 deletions TeddyBench/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using TeddyBench.Properties;
using TonieFile;
using Application = System.Windows.Forms.Application;
Expand Down Expand Up @@ -217,12 +218,6 @@ void SaveSettings()
Settings.Save("teddyBench.cfg");
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
SaveSettings();
base.OnFormClosing(e);
}

private void Proxmark3_DeviceFound(object sender, string e)
{
if (InvokeRequired)
Expand Down Expand Up @@ -352,6 +347,7 @@ protected override void OnDragDrop(DragEventArgs drgevent)
protected override void OnClosing(CancelEventArgs e)
{
StopThreads();
SaveSettings();
base.OnClosing(e);
}

Expand Down Expand Up @@ -623,14 +619,14 @@ private void AnalyzeMain()
}
if (!string.IsNullOrEmpty(info.Pic) && !lstTonies.LargeImageList.Images.ContainsKey(hash))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(info.Pic);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Image img = ResizeImage(Image.FromStream(response.GetResponseStream()), 128, 128);

this.BeginInvoke(new Action(() =>
Image img = GetImage(info.Pic, hash);
if (img != null)
{
lstTonies.LargeImageList.Images.Add(hash, img);
}));
this.BeginInvoke(new Action(() =>
{
lstTonies.LargeImageList.Images.Add(hash, img);
}));
}
}
image = hash;
}
Expand Down Expand Up @@ -678,6 +674,48 @@ private void AnalyzeMain()
}
}

private Image GetImage(string pic, string hash)
{
string cacheFileName = Path.Combine("cache", hash + ".png");

try
{
if(!Directory.Exists("cache"))
{
Directory.CreateDirectory("cache");
}

if (File.Exists(cacheFileName))
{
return Image.FromFile(cacheFileName);
}
}
catch (Exception ex)
{
}

try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pic);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Image img = ResizeImage(Image.FromStream(response.GetResponseStream()), 128, 128);

try
{
img.Save(cacheFileName);
}
catch (Exception ex)
{
}
return img;
}
catch (Exception ex)
{
}

return null;
}

private TonieAudio GetTonieAudio(string fileName)
{
FileInfo info = new FileInfo(fileName);
Expand Down

0 comments on commit da930e5

Please sign in to comment.