private byte[] PrepareTexture(string filename, string textureEffect) { var effect = GetEffect(textureEffect); if (effect == null) { return(File.ReadAllBytes(filename)); } var image = BetterImage.LoadBitmapSource(filename); var size = new Size(image.Width, image.Height); var result = new Image { Width = image.Width, Height = image.Height, Source = image.ImageSource, Effect = effect, }; result.Measure(size); result.Arrange(new Rect(size)); result.ApplyTemplate(); result.UpdateLayout(); var bmp = new RenderTargetBitmap(image.Width, image.Height, 96, 96, PixelFormats.Pbgra32); bmp.Render(result); return(bmp.ToBytes(ImageFormat.Png)); }
public override void Set(IniFile file) { ActionExtension.InvokeInMainThreadAsync(() => { var s = Stopwatch.StartNew(); try { var trackId = file["RACE"].GetNonEmpty("TRACK"); var configurationId = file["RACE"].GetNonEmpty("CONFIG_TRACK"); var track = TracksManager.Instance.GetLayoutById(trackId ?? string.Empty, configurationId); if (track == null) { return; } var outline = track.OutlineImage; var outlineCropped = Path.Combine(Path.GetDirectoryName(track.OutlineImage) ?? "", "outline_cropped.png"); if (!File.Exists(outline) || File.Exists(outlineCropped)) { return; } var image = BetterImage.LoadBitmapSource(outline); var size = new Size(256, 256); var result = new BetterImage { Width = 256, Height = 256, Source = image.ImageSource, CropTransparentAreas = true }; result.Measure(size); result.Arrange(new Rect(size)); result.ApplyTemplate(); result.UpdateLayout(); var bmp = new RenderTargetBitmap(256, 256, 96, 96, PixelFormats.Pbgra32); bmp.Render(result); File.WriteAllBytes(outlineCropped, bmp.ToBytes(ImageFormat.Png)); } catch (Exception e) { Logging.Error(e); } finally { Logging.Write($"Time taken: {s.Elapsed.TotalMilliseconds:F2} ms"); } }); }