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>
/// Browse for file button
/// Browse for game board file button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
@ -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);
}

View File

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

View File

@ -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;
}
/// <summary>
/// Retrieves a cropped area of the provided bitmap
/// </summary>
@ -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);
}
}
}
}