Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add play gif example (C# + SixLabors.ImageSharp)
- Loading branch information
1 parent
28fd4d4
commit abd7d30
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\RPiRgbLEDMatrix.csproj" /> | ||
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using RPiRgbLEDMatrix; | ||
using System.Runtime.InteropServices; | ||
using Color = RPiRgbLEDMatrix.Color; | ||
|
||
Console.Write("GIF path: "); | ||
var path = Console.ReadLine()!; | ||
|
||
using var matrix = new RGBLedMatrix(32, 2, 1); | ||
var canvas = matrix.CreateOffscreenCanvas(); | ||
|
||
Configuration.Default.PreferContiguousImageBuffers = true; | ||
using var image = Image.Load<Rgb24>(path); | ||
image.Mutate(o => o.Resize(canvas.Width, canvas.Height)); | ||
|
||
var running = true; | ||
Console.CancelKeyPress += (s, e) => | ||
{ | ||
running = false; | ||
e.Cancel = true; // don't terminate, we need to dispose | ||
}; | ||
|
||
var frame = -1; | ||
// preprocess frames to get delays and pixel buffers | ||
var frames = image.Frames | ||
.Select(f => ( | ||
Pixels: f.DangerousTryGetSinglePixelMemory(out var memory) ? memory : throw new("Could not get pixel buffer"), | ||
Delay: f.Metadata.GetGifMetadata().FrameDelay * 10 | ||
)).ToArray(); | ||
|
||
// run until user presses Ctrl+C | ||
while (running) | ||
{ | ||
frame = (frame + 1) % frames.Length; | ||
|
||
var data = MemoryMarshal.Cast<Rgb24, Color>(frames[frame].Pixels.Span); | ||
canvas.SetPixels(0, 0, canvas.Width, canvas.Height, data); | ||
|
||
matrix.SwapOnVsync(canvas); | ||
Thread.Sleep(frames[frame].Delay); | ||
} |