Changed Player class to inherit from Grid

Added .gif support
This commit is contained in:
2021-08-30 19:42:01 +12:00
parent 70ca03a101
commit 34db81f143
5 changed files with 227 additions and 61 deletions
+49
View File
@@ -1,6 +1,12 @@
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Collections.Generic;
namespace RBG.Helpers
{
@@ -83,4 +89,47 @@ namespace RBG.Helpers
}
public class AnimatedBitmapImage
{
public BitmapSource BaseImage { get; set; }
BitmapFrame[] frames;
int framerate;
int currentFrame;
Timer frameChange;
public AnimatedBitmapImage(Stream source, Image target, Dispatcher dispatcher)
{
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) =>
{
currentFrame++;
currentFrame %= frames.Length;
BaseImage = frames[currentFrame];
_ = dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
{
try
{
target.Source = BaseImage;
}
catch
{
}
}));
},null, framerate, framerate);
}
}
}