Added the WPF funcitonality

This commit is contained in:
Brychan Dempsey 2021-09-23 19:20:26 +12:00
parent a0af5286f8
commit ea5e564352
3 changed files with 22 additions and 26 deletions

View File

@ -36,7 +36,7 @@ namespace RBG_Server.WPF
} }
/// <summary> /// <summary>
/// Browse for file button /// Browse for game board file button
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
@ -384,14 +384,8 @@ namespace RBG_Server.WPF
{ {
try 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(); Image spriteImage = new();
// Generate the animatedBitmap
RBG.Helpers.AnimatedBitmapImage animatedBitmap = new(new MemoryStream(item.Value, false), spriteImage, Application.Current.Dispatcher); RBG.Helpers.AnimatedBitmapImage animatedBitmap = new(new MemoryStream(item.Value, false), spriteImage, Application.Current.Dispatcher);
_ = LoadedSprites.Children.Add(spriteImage); _ = LoadedSprites.Children.Add(spriteImage);
} }

View File

@ -11,10 +11,4 @@
<None Remove="pokemon_v3.png" /> <None Remove="pokemon_v3.png" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="pokemon_v3.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
</Project> </Project>

View File

@ -51,6 +51,8 @@ namespace RBG.Helpers
int g = (pixel >> 8) & 0xFF; int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF; int b = pixel & 0xFF;
// Calc new values & clamp to a max of 0xFF (8 bits per channel) // 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 newB = (int)Math.Min((uint)(b * blueRate), 255);
int newG = (int)Math.Min((uint)(g * greenRate), 255); int newG = (int)Math.Min((uint)(g * greenRate), 255);
int newR = (int)Math.Min((uint)(r * redRate), 255); int newR = (int)Math.Min((uint)(r * redRate), 255);
@ -66,6 +68,8 @@ namespace RBG.Helpers
return wbmp; return wbmp;
} }
/// <summary> /// <summary>
/// Retrieves a cropped area of the provided bitmap /// Retrieves a cropped area of the provided bitmap
/// </summary> /// </summary>
@ -92,7 +96,8 @@ namespace RBG.Helpers
public class AnimatedBitmapImage public class AnimatedBitmapImage
{ {
public BitmapSource BaseImage { get; set; } // Underlying image that
//public BitmapSource BaseImage { get; set; }
BitmapFrame[] frames; BitmapFrame[] frames;
int framerate; int framerate;
int currentFrame; int currentFrame;
@ -102,34 +107,37 @@ namespace RBG.Helpers
GifBitmapDecoder decoder = new(source, BitmapCreateOptions.None, BitmapCacheOption.Default); GifBitmapDecoder decoder = new(source, BitmapCreateOptions.None, BitmapCacheOption.Default);
frames = new BitmapFrame[decoder.Frames.Count]; frames = new BitmapFrame[decoder.Frames.Count];
decoder.Frames.CopyTo(frames, 0); 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; framerate = 33;
BaseImage = frames[0];
// cycle through frames // cycle through frames
frameChange = new Timer((t) => frameChange = new Timer((t) =>
{ {
// Ensure frame index is in bounds
currentFrame++; currentFrame++;
currentFrame %= frames.Length; 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(() => _ = dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
{ {
try try
{ {
target.Source = BaseImage; // actually set the WPF Image content to our BitmapImage
target.Source = currentFrameSource;
} }
catch catch
{ {
// Exceptions are mostly irrelevant
} }
})); }));
},null, framerate, framerate); },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);
}
}
}
} }