ME3Explorer.Texplorer2.UpdateThumbnailDisplays C# (CSharp) Method

UpdateThumbnailDisplays() private method

Loads thumbnails from disk, generated during treescan.
private UpdateThumbnailDisplays ( myTreeNode nod ) : void
nod myTreeNode Node to load textures into.
return void
        private void UpdateThumbnailDisplays(myTreeNode nod)
        {
            if (nod == null)
                return;

            if (!MainListView.InvokeRequired)
            {
                Task.Run(new Action(() => UpdateThumbnailDisplays(nod)));
                return;
            }
            myTreeNode SelectedNode = nod;
            List<Image> thumbs = new List<Image>();
            List<int> thuminds = new List<int>();

            for (int i = 0; i < SelectedNode.TexInds.Count; i++)
            {
                TreeTexInfo tex = Tree.GetTex(SelectedNode.TexInds[i]);
                int ind = 0;

                bool FoundImage = false;

                // KFreon: If no thumbnail, try again
                if (tex?.ThumbnailPath == null)
                    continue;
                else if (File.Exists(tex.ThumbnailPath))
                {
                    FoundImage = true;
                    ind = i + 1;
                }
                else if (tex.ThumbnailPath.Contains("placeholder.ico") && !tex.TriedThumbUpdate)
                {
                    try
                    {
                        using (Bitmap img = tex.GetImage(pathBIOGame))
                        {

                            using (PCCObjects.IPCCObject pcc = PCCObjects.Creation.CreatePCCObject(tex.Files[0], WhichGame))
                            {
                                using (Textures.ITexture2D tex2D = pcc.CreateTexture2D(tex.ExpIDs[0], pathBIOGame))
                                {
                                    if (img != null)
                                    {
                                        string savepath = Path.Combine(ThumbnailPath, tex.ThumbName);
                                        if (File.Exists(savepath))
                                        {
                                            ind = i + 1;
                                            tex.ThumbnailPath = savepath;
                                            FoundImage = true;
                                        }
                                        else
                                        {
                                            try
                                            {
                                                byte[] data = UsefulThings.WinForms.Imaging.GetPixelDataFromBitmap(img);
                                                ImageEngine.GenerateThumbnailToFile(new MemoryStream(data), savepath, 128);
                                                tex.ThumbnailPath = savepath;
                                                FoundImage = true;
                                                ind = i + 1;
                                            }
                                            catch
                                            {
                                                // IGNORE
                                            }
                                        }
                                        tex.TriedThumbUpdate = true;
                                        Tree.ReplaceTex(SelectedNode.TexInds[i], tex);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        thumbs.Add(new Bitmap(1, 1));
                    }
                }

                Image thumb;
                if (FoundImage)
                    try
                    {
                        thumb = UsefulThings.WinForms.Imaging.PadImageToSquare(tex.ThumbnailPath, 128);
                        thumbs.Add(thumb);
                    }
                    catch
                    {
                        FoundImage = false;  // Kfreon: Set flag for later denoting that the image failed to work properly
                    }

                // KFreon: Double I know, but this way, I can set the image to the placefinder image for a variety of situations
                if (!FoundImage)
                {
                    DebugOutput.PrintLn("Thumbnail: " + tex.ThumbnailPath + "  not found.");
                    ind = 0;
                }
                thuminds.Add(ind);
            }

            // KFreon: Set images to show in ListView
            if (!MainListView.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    ListViewImageList.Images.Clear();
                    var placeholder = new Bitmap(Path.Combine(ExecFolder, "placeholder.ico"));
                    ListViewImageList.Images.Add(placeholder);
                    ListViewImageList.Images.AddRange(thumbs.ToArray());

                    placeholder.Dispose();
                    thumbs.ForEach(t => t.Dispose());
                    thumbs = null;


                    for (int i = 0; i < MainListView.Items.Count; i++)
                    {
                        try
                        {
                            MainListView.Items[i].ImageIndex = thuminds[i];
                        }
                        catch (Exception e)
                        {
                            DebugOutput.PrintLn("Error setting thumbnails in display: " + e.Message);
                        }
                    }

                    MainListView.Refresh();
                }));
            }
        }
Texplorer2