From ea5e564352f1035da06c3263f085f11af5efde49 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Thu, 23 Sep 2021 19:20:26 +1200 Subject: [PATCH] Added the WPF funcitonality --- PDGServer_WPF/MainWindow.xaml.cs | 10 ++---- .../Pokemon Drinking Game.csproj | 6 ---- RBG.Helpers/ImageProcessing.cs | 32 ++++++++++++------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/PDGServer_WPF/MainWindow.xaml.cs b/PDGServer_WPF/MainWindow.xaml.cs index 58717a1..7c274ae 100644 --- a/PDGServer_WPF/MainWindow.xaml.cs +++ b/PDGServer_WPF/MainWindow.xaml.cs @@ -36,7 +36,7 @@ namespace RBG_Server.WPF } /// - /// Browse for file button + /// Browse for game board file button /// /// /// @@ -384,14 +384,8 @@ namespace RBG_Server.WPF { try { - /* - // Must write-out the stream to - MediaElement gifMedia = new(); - gifMedia.LoadedBehavior = MediaState.Play; - gifMedia.Source = new Uri(item.Key); - _ = LoadedSprites.Children.Add(gifMedia); - */ Image spriteImage = new(); + // Generate the animatedBitmap RBG.Helpers.AnimatedBitmapImage animatedBitmap = new(new MemoryStream(item.Value, false), spriteImage, Application.Current.Dispatcher); _ = LoadedSprites.Children.Add(spriteImage); } diff --git a/Pokemon Drinking Game/Pokemon Drinking Game.csproj b/Pokemon Drinking Game/Pokemon Drinking Game.csproj index 3db3da3..d7aeab9 100644 --- a/Pokemon Drinking Game/Pokemon Drinking Game.csproj +++ b/Pokemon Drinking Game/Pokemon Drinking Game.csproj @@ -11,10 +11,4 @@ - - - PreserveNewest - - - diff --git a/RBG.Helpers/ImageProcessing.cs b/RBG.Helpers/ImageProcessing.cs index 09a2444..dfd3bc2 100644 --- a/RBG.Helpers/ImageProcessing.cs +++ b/RBG.Helpers/ImageProcessing.cs @@ -51,6 +51,8 @@ namespace RBG.Helpers int g = (pixel >> 8) & 0xFF; int b = pixel & 0xFF; // Calc new values & clamp to a max of 0xFF (8 bits per channel) + // By casting to unsigned, negative numbers will become very large positive numbers, + // so clamping to 255 will ensure it is 0-255 int newB = (int)Math.Min((uint)(b * blueRate), 255); int newG = (int)Math.Min((uint)(g * greenRate), 255); int newR = (int)Math.Min((uint)(r * redRate), 255); @@ -66,6 +68,8 @@ namespace RBG.Helpers return wbmp; } + + /// /// Retrieves a cropped area of the provided bitmap /// @@ -92,7 +96,8 @@ namespace RBG.Helpers public class AnimatedBitmapImage { - public BitmapSource BaseImage { get; set; } + // Underlying image that + //public BitmapSource BaseImage { get; set; } BitmapFrame[] frames; int framerate; int currentFrame; @@ -102,34 +107,37 @@ namespace RBG.Helpers GifBitmapDecoder decoder = new(source, BitmapCreateOptions.None, BitmapCacheOption.Default); frames = new BitmapFrame[decoder.Frames.Count]; decoder.Frames.CopyTo(frames, 0); - - byte[] frameratebytes = new byte[2]; - source.Position = 0x324; - frameratebytes[0] = (byte)source.ReadByte(); - frameratebytes[1] = (byte)source.ReadByte(); framerate = 33; - - BaseImage = frames[0]; // cycle through frames frameChange = new Timer((t) => { + // Ensure frame index is in bounds currentFrame++; currentFrame %= frames.Length; - BaseImage = frames[currentFrame]; + // Set the WPF image to the frame at the new index + BitmapSource currentFrameSource = frames[currentFrame]; _ = dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => { try { - target.Source = BaseImage; + // actually set the WPF Image content to our BitmapImage + target.Source = currentFrameSource; } catch { - + // Exceptions are mostly irrelevant } })); },null, framerate, framerate); } - } + public AnimatedBitmapImage(Stream source, Image target, Dispatcher dispatcher, double redRate = 1.0, double greenRate = 1.0, double blueRate = 1.0, double alphaRate = 1.0) : this (source, target, dispatcher) + { + for (int i = 0; i < frames.Length; i++) + { + frames[i] = (BitmapFrame)ImageProcessing.UpdatePixelColours(frames[i], redRate, greenRate, blueRate, alphaRate); + } + } + } }