public string ConvertFileTo(FileViewModel file, string path) { try { switch (file.Extension.ToLower()) { case ".xmb": { var xdoc = XMBFile.Load(GetFileStream(file.Entry)).GetAsXDocument(); xdoc.Save(path); } break; case ".btx": case ".ddt": { RTS3Image srcImg = null; if (file.Extension.ToLower() == ".ddt") { srcImg = DDTImage.Load(GetFileStream(file.Entry)); } else if (file.Extension.ToLower() == ".btx") { srcImg = BTXImage.Load(GetFileStream(file.Entry)); } byte[] pixData = srcImg.Get32BitUncompressed(); if (pixData == null) { return(null); } var bmp = BitmapSource.Create(srcImg.Width, srcImg.Height, 96, 96, PixelFormats.Bgra32, null, pixData, srcImg.Width * 4); BitmapEncoder encoder = null; if (Path.GetExtension(path).ToLower() == ".png") { encoder = new PngBitmapEncoder(); } else if (Path.GetExtension(path).ToLower() == ".jpg") { encoder = new JpegBitmapEncoder(); } else if (Path.GetExtension(path).ToLower() == ".gif") { encoder = new GifBitmapEncoder(); } else if (Path.GetExtension(path).ToLower() == ".bmp") { encoder = new BmpBitmapEncoder(); } encoder.Frames.Add(BitmapFrame.Create(bmp)); using (var stream = File.Create(path)) { encoder.Save(stream); } } break; default: return(null); } } catch { return(null); } return(path); }
public void GetWPFTexture(string name, Action <ImageSource> callback) { if (string.IsNullOrWhiteSpace(name)) { return; } var cache = FindInCache(name); if (cache != null) { callback(cache); return; } ThreadPool.QueueUserWorkItem(delegate { RTS3Image image = null; lock (imageCache) { cache = FindInCache(name); if (cache != null) { callback(cache); return; } var file = GetStream(name); if (file == null) { var noext = DropExt(name); if (file == null) { file = GetStream(noext + ".ddt"); } if (file == null) { file = GetStream(noext + ".btx"); } } if (file != null) { var magic = file.ReadByte(); file.Position -= 1; if (magic == 'b') { try { image = BTXImage.Load(file); } catch { } } else { try { image = DDTImage.Load(file); } catch { } } } } if (image != null) { var pixData = image.Get32BitUncompressed(); App.Current.Dispatcher.BeginInvoke((Action) delegate { var bmp = BitmapSource.Create(image.Width, image.Height, 96, 96, PixelFormats.Bgra32, null, pixData, image.Width * 4); lock (imageCache) { if (!imageCache.ContainsKey(name)) { imageCache.Add(name, bmp); } } callback(bmp); }); } else { App.Current.Dispatcher.BeginInvoke((Action) delegate { callback(null); }); } }); }