public ImageSource GetImageSourceFromBlpPath(string filePath) { ImageSource source; var exists = _ImageMap.TryGetValue(filePath, out source); if (exists) { return(source); } try { using (var fileStream = new FileStream(filePath, FileMode.Open)) { using (var blpImage = new SereniaBLPLib.BlpFile(fileStream)) { using (var bit = blpImage.getBitmap(0)) { var handle = bit.GetHbitmap(); try { source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); // Freeze so that it can be accessed on any thread source.Freeze(); _ImageMap.TryAdd(filePath, source); return(source); } finally { DeleteObject(handle); } } } } } catch (Exception e) { // Logging full exception is quite costly here Console.WriteLine($"[BlpManager] WARNING Unable to load image: {filePath} - {e.Message}"); // Making the choice here to not try to load the resource again until the program is restarted _ImageMap.TryAdd(filePath, null); } return(null); }
public async void UpdateMainWindowIcons(double margin) { if (adapter == null || main.selectedID == 0) { // adapter.query below caused unhandled exception with main.selectedID as 0. return; } DataRow res; try { res = adapter.query(string.Format("SELECT `SpellIconID`,`ActiveIconID` FROM `{0}` WHERE `ID` = '{1}'", adapter.Table, main.selectedID)).Rows[0]; } catch (Exception) { return; } UInt32 iconInt = UInt32.Parse(res[0].ToString()); UInt32 iconActiveInt = UInt32.Parse(res[1].ToString()); UInt32 selectedRecord = UInt32.MaxValue; for (UInt32 i = 0; i < header.RecordCount; ++i) { if (body.records[i].ID == iconInt) { selectedRecord = i; break; } if (body.records[i].ID == iconActiveInt) { selectedRecord = i; break; } } string icon = ""; int offset = 0; try { if (selectedRecord == UInt32.MaxValue) { throw new Exception("The icon for this spell does not exist in the SpellIcon.dbc"); } offset = (int)body.records[selectedRecord].Name; while (body.StringBlock[offset] != '\0') { icon += body.StringBlock[offset++]; } if (!File.Exists(icon + ".blp")) { throw new Exception("File could not be found: " + "Icons\\" + icon + ".blp"); } } catch (Exception ex) { main.Dispatcher.Invoke(new Action(()=>main.HandleErrorMessage(ex.Message))); return; } FileStream fileStream = new FileStream(icon + ".blp", FileMode.Open); SereniaBLPLib.BlpFile image; image = new SereniaBLPLib.BlpFile(fileStream); Bitmap bit = image.getBitmap(0); await Task.Factory.StartNew(() => { main.CurrentIcon.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bit.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); }, CancellationToken.None, TaskCreationOptions.None, main.UIScheduler); image.close(); fileStream.Close(); if (!loadedAllIcons) { loadedAllIcons = true; int currentOffset = 1; string[] icons = body.StringBlock.Split('\0'); int iconIndex = 0; int columnsUsed = icons.Length / 11; int rowsToDo = columnsUsed / 2; for (int j = -rowsToDo; j <= rowsToDo; ++j) { for (int i = -5; i < 6; ++i) { ++iconIndex; if (iconIndex >= icons.Length - 1) { break; } int this_icons_offset = currentOffset; currentOffset += icons[iconIndex].Length + 1; if (!File.Exists(icons[iconIndex] + ".blp")) { Console.WriteLine("Warning: Icon not found: " + icons[iconIndex] + ".blp"); continue; } fileStream = new FileStream(icons[iconIndex] + ".blp", FileMode.Open); image = new SereniaBLPLib.BlpFile(fileStream); bit = image.getBitmap(0); await Task.Factory.StartNew(() => { System.Windows.Controls.Image temp = new System.Windows.Controls.Image(); temp.Width = 32; temp.Height = 32; temp.Margin = new System.Windows.Thickness(margin, 0, 0, 0); temp.VerticalAlignment = VerticalAlignment.Top; temp.HorizontalAlignment = HorizontalAlignment.Left; temp.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bit.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); temp.Name = "Index_" + this_icons_offset; temp.MouseDown += this.ImageDown; main.IconGrid.Children.Add(temp); }, CancellationToken.None, TaskCreationOptions.None, main.UIScheduler); image.close(); fileStream.Close(); } } } }
public async void updateMainWindowIcons() { if (spell == null) return; UInt32 iconInt = spell.body.records[main.selectedID].record.SpellIconID; UInt32 selectedRecord = UInt32.MaxValue; for (UInt32 i = 0; i < header.record_count; ++i) { if (body.records[i].ID == iconInt) { selectedRecord = i; break; } } string icon = ""; int offset = 0; try { if (selectedRecord == UInt32.MaxValue) throw new Exception("The icon for this spell does not exist in the SpellIcon.dbc"); offset = (int)body.records[selectedRecord].name; while (body.StringBlock[offset] != '\0') { icon += body.StringBlock[offset++]; } if (!File.Exists(icon + ".blp")) throw new Exception("File could not be found: " + "Icons\\" + icon + ".blp"); } catch (Exception ex) { main.ERROR_STR = ex.Message; return; } FileStream file = new FileStream(icon + ".blp", FileMode.Open); SereniaBLPLib.BlpFile image; image = new SereniaBLPLib.BlpFile(file); Bitmap bit = image.getBitmap(0); await Task.Factory.StartNew(() => { main.CurrentIcon.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bit.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); }, CancellationToken.None, TaskCreationOptions.None, main.uiScheduler); image.close(); file.Close(); if (!loadedAllIcons) { loadedAllIcons = true; int currentOffset = 1; string[] icons = body.StringBlock.Split('\0'); int iconIndex = 0; int columnsUsed = icons.Length / 11; int rowsToDo = columnsUsed / 2; for (int j = -rowsToDo; j <= rowsToDo; ++j) // Rows { for (int i = -5; i < 6; ++i) // Columns { ++iconIndex; if (iconIndex >= icons.Length - 1) break; int thisIconsOffset = currentOffset; currentOffset += icons[iconIndex].Length + 1; if (!File.Exists(icons[iconIndex] + ".blp")) { Console.WriteLine("Warning: Icon not found: " + icons[iconIndex] + ".blp"); continue; } file = new FileStream(icons[iconIndex] + ".blp", FileMode.Open); image = new SereniaBLPLib.BlpFile(file); bit = image.getBitmap(0); await Task.Factory.StartNew(() => { System.Windows.Controls.Image temp = new System.Windows.Controls.Image(); temp.Width = 64; temp.Height = 64; temp.Margin = new System.Windows.Thickness(139 * i, 139 * j, 0, 0); temp.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bit.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); temp.Name = "Index_" + thisIconsOffset; temp.MouseDown += this.imageDown; main.IconGrid.Children.Add(temp); }, CancellationToken.None, TaskCreationOptions.None, main.uiScheduler); image.close(); file.Close(); } } } }
public async void UpdateMainWindowIcons() { if (mySQL == null) { return; } var res = mySQL.query(String.Format("SELECT `SpellIconID`,`ActiveIconID` FROM `{0}` WHERE `ID` = '{1}'", mySQL.Table, main.selectedID)).Rows[0]; UInt32 iconInt = UInt32.Parse(res[0].ToString()); UInt32 iconActiveInt = UInt32.Parse(res[1].ToString()); UInt32 selectedRecord = UInt32.MaxValue; for (UInt32 i = 0; i < header.RecordCount; ++i) { if (body.records[i].ID == iconInt) { selectedRecord = i; break; } if (body.records[i].ID == iconActiveInt) { selectedRecord = i; break; } } string icon = ""; int offset = 0; try { if (selectedRecord == UInt32.MaxValue) { throw new Exception("The icon for this spell does not exist in the SpellIcon.dbc"); } offset = (int)body.records[selectedRecord].Name; while (body.StringBlock[offset] != '\0') { icon += body.StringBlock[offset++]; } if (!File.Exists(icon + ".blp")) { throw new Exception("File could not be found: " + "Icons\\" + icon + ".blp"); } } catch (Exception ex) { main.HandleErrorMessage(ex.Message); return; } FileStream fileStream = new FileStream(icon + ".blp", FileMode.Open); SereniaBLPLib.BlpFile image; image = new SereniaBLPLib.BlpFile(fileStream); Bitmap bit = image.getBitmap(0); await Task.Factory.StartNew(() => { main.CurrentIcon.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bit.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); }, CancellationToken.None, TaskCreationOptions.None, main.UIScheduler); image.close(); fileStream.Close(); if (!loadedAllIcons) { loadedAllIcons = true; int currentOffset = 1; string[] icons = body.StringBlock.Split('\0'); int iconIndex = 0; int columnsUsed = icons.Length / 11; int rowsToDo = columnsUsed / 2; for (int j = -rowsToDo; j <= rowsToDo; ++j) { for (int i = -5; i < 6; ++i) { ++iconIndex; if (iconIndex >= icons.Length - 1) { break; } int this_icons_offset = currentOffset; currentOffset += icons[iconIndex].Length + 1; if (!File.Exists(icons[iconIndex] + ".blp")) { Console.WriteLine("Warning: Icon not found: " + icons[iconIndex] + ".blp"); continue; } fileStream = new FileStream(icons[iconIndex] + ".blp", FileMode.Open); image = new SereniaBLPLib.BlpFile(fileStream); bit = image.getBitmap(0); await Task.Factory.StartNew(() => { System.Windows.Controls.Image temp = new System.Windows.Controls.Image(); temp.Width = 64; temp.Height = 64; temp.Margin = new System.Windows.Thickness(139 * i, 139 * j, 0, 0); temp.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bit.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bit.Width, bit.Height)); temp.Name = "Index_" + this_icons_offset; temp.MouseDown += this.ImageDown; main.IconGrid.Children.Add(temp); }, CancellationToken.None, TaskCreationOptions.None, main.UIScheduler); image.close(); fileStream.Close(); } } } }