/// <summary> /// Get the icon image, can only be found in icons zip file. /// </summary> /// <param name="iconID">The iconID of the image to get.</param> /// <returns>The image or null if not found.</returns> public static Image GetIconImage(int iconID) { IconID icon = IconID.GetIconID(iconID); if (icon == null) { return(null); } string iconFile = icon.iconFile; int lastSlash = iconFile.LastIndexOf('/'); iconFile = "Icons/items/" + iconFile.Substring(lastSlash + 1); if (UserData.iconsZip?.Length > 0) { // Has the image been loaded? if (images.ContainsKey(iconFile)) { // Yes, use that. return(images[iconFile]); } // No, load images. Image img = GetImageFromZIP(UserData.iconsZip, iconFile); if (img != null) { images[iconFile] = img; return(img); } } return(null); }
public static string PreloadImages(BackgroundWorker worker) { if (UserData.typeIconZip == null || UserData.typeIconZip?.Length == 0) { // No zip file specified. return(null); } // Get inv types and icons. worker.ReportProgress(0, "Finding needed images."); List <int> types = new List <int>(); List <int> icons = new List <int>(); List <InvMarketGroup> groups = InvMarketGroup.GetRootGroups(); while (groups.Count > 0) { InvMarketGroup group = groups[0]; groups.Remove(group); // Add icon. icons.Add(group.iconID); // Add child groups to search list. groups.AddRange(InvMarketGroup.GetGroupChildren(group.marketGroupID)); // Get types for group. List <InvType> groupTypes = InvType.GetMarketGroupTypes(group.marketGroupID); // Add types to group. foreach (InvType type in groupTypes) { if (!type.published) { continue; } types.Add(type.typeID); } } List <string> files = new List <string>(); foreach (int typeID in types) { string zipImageFile = "Types/" + typeID.ToString() + "_32.png"; files.Add(zipImageFile); } int typesComplete = 0; worker.ReportProgress(0, "Opening types Zip..."); if (UserData.typeIconZip != null && File.Exists(UserData.typeIconZip)) { FileStream fileStream = File.OpenRead(UserData.typeIconZip); ZipArchive zip = new ZipArchive(fileStream); int totalFiles = types.Count; int failedFiles = 0; int skippedFiles = 0; foreach (int typeID in types) { ZipArchiveEntry zipFile = zip.GetEntry("Types/" + typeID.ToString() + "_64.png"); if (zipFile == null) { continue; } Stream zStream = zipFile.Open(); MemoryStream memStream = new MemoryStream(); zStream.CopyTo(memStream); memStream.Position = 0; try { Image img = Image.FromStream(memStream); images[zipFile.FullName] = img; } catch (ArgumentException) { failedFiles++; skippedFiles++; } typesComplete++; if (typesComplete % 10 == 0) { double pct = (double)typesComplete / (double)totalFiles; string msg = "Loading type image files... (" + (typesComplete - skippedFiles) + " of " + totalFiles + " complete)"; if (failedFiles > 0) { msg += " (" + failedFiles + " failed.)"; } worker.ReportProgress(Math.Min((int)(pct * 50.0), 100), msg); } } } int iconsComplete = 0; worker.ReportProgress(50, "Opening icons Zip..."); if (UserData.iconsZip != null && File.Exists(UserData.iconsZip)) { // Open the zip archive. FileStream fileStream = File.OpenRead(UserData.iconsZip); ZipArchive zip = new ZipArchive(fileStream); int totalFiles = icons.Count; foreach (int iconID in icons) { IconID icon = IconID.GetIconID(iconID); if (icon == null) { continue; } // Construct image path. string iconFile = icon.iconFile; int lastSlash = iconFile.LastIndexOf('/'); iconFile = "Icons/items/" + iconFile.Substring(lastSlash + 1); // Get the file. MemoryStream memStream = new MemoryStream(); ZipArchiveEntry zipFile = zip.GetEntry(iconFile); if (zipFile == null) { continue; } Stream zStream = zipFile.Open(); zStream.CopyTo(memStream); memStream.Position = 0; try { Image img = Image.FromStream(memStream); images[iconFile] = img; } catch (ArgumentException) { } iconsComplete++; if (iconsComplete % 10 == 0) { double pct = (double)iconsComplete / (double)totalFiles; string msg = "Loading Icon image files... (" + iconsComplete + " of " + totalFiles + " complete)"; worker.ReportProgress(Math.Min((int)(pct * 50.0) + 50, 100), msg); } } } worker.ReportProgress(100, "Loaded " + typesComplete + " type images and " + iconsComplete + " icon images loaded."); return(null); }