private void PreviewObject(TPFTexInfo tex)
{
// KFreon: Clear old image
ClearPreview();
// Clean cache if required
if (Previews.Keys.Count > 10)
{
var img = Previews.First();
img.Value.Dispose();
Previews.Remove(img.Key);
}
// KFreon: Gather from cache if available
if (Previews.ContainsKey(tex.PreviewKey))
{
Bitmap img = Previews[tex.PreviewKey];
try
{
this.Invoke(new Action(() => PreviewBox.Image = img));
}
catch (ObjectDisposedException)
{ } // DOn't care - it's when the form is closing. The form can become disposed while the preview is trying to be shown.
DisappearTextBox(true);
return;
}
// KFreon: Get data
byte[] data = tex.Extract(null, true);
if (data == null)
return;
// KFreon: Load new one
if (tex.isDef)
{
DisappearTextBox(false);
try
{
string message = Encoding.UTF8.GetString(data);
this.Invoke(new Action(() => texmodPreviewBox.Text = message));
}
catch (Exception e)
{
DebugOutput.PrintLn("Unable to get text from data: " + e.Message);
}
}
else
{
Bitmap img = null;
using (MemoryStream ms = new MemoryStream(data))
using (ImageEngineImage image = new ImageEngineImage(ms, null, 512, false))
img = image.GetGDIBitmap(true, false, 512);
this.Invoke(new Action(() => PreviewBox.Image = img));
Previews.Add(tex.PreviewKey, img);
DisappearTextBox(true);
}
}