Skip to content

Commit

Permalink
fix: remove System.Drawing.Common & make ScreenshotService.cs work on…
Browse files Browse the repository at this point in the history
… linux
  • Loading branch information
alexyakunin committed Oct 11, 2023
1 parent e8661f1 commit ceb2c82
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 56 deletions.
8 changes: 0 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@ WORKDIR /samples
RUN dotnet publish -c:Release --no-build --no-restore src/Blazor/Server/Server.csproj

FROM mcr.microsoft.com/dotnet/aspnet:8.0 as runtime
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
apt-utils \
libc6-dev \
libgdiplus \
libx11-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true
WORKDIR /samples
COPY --from=publish /samples .
Expand Down
2 changes: 0 additions & 2 deletions src/Blazor/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<InvariantGlobalization>true</InvariantGlobalization>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
Expand All @@ -31,7 +30,6 @@
<PackageReference Include="Stl.Fusion.Server" Version="6.5.1" />
<PackageReference Include="Stl.Fusion.EntityFramework" Version="6.5.1" />
<PackageReference Include="Stl.RestEase" Version="6.5.1" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0-rc.2.23479.14" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 1 addition & 5 deletions src/Blazor/Server/Services/DirectBitmap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
Expand All @@ -12,7 +10,6 @@ public sealed class DirectBitmap : IDisposable
{
private int _isDisposed;
private GCHandle _gcHandle;
public Bitmap Bitmap { get; }
public Image<Bgra32> Image { get; }
public Bgra32[] Buffer { get; }
public int Height { get; }
Expand All @@ -28,7 +25,6 @@ public DirectBitmap(int width, int height)
Height = height;
Buffer = GC.AllocateUninitializedArray<Bgra32>(width * height, true);
_gcHandle = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppArgb, _gcHandle.AddrOfPinnedObject());
Image = SixLabors.ImageSharp.Image.WrapMemory(Buffer.AsMemory(), width, height);
}

Expand All @@ -38,7 +34,7 @@ public void Dispose()
{
if (0 != Interlocked.Exchange(ref _isDisposed, 1))
return;
Bitmap.Dispose();

Image.Dispose();
_gcHandle.Free();
GC.SuppressFinalize(this);
Expand Down
35 changes: 0 additions & 35 deletions src/Blazor/Server/Services/ScreenshotService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using Stl.OS;
using Samples.Blazor.Abstractions;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
Expand All @@ -13,7 +9,6 @@
using SixLabors.ImageSharp.Processing;
using Stl.Rpc;
using Color = SixLabors.ImageSharp.Color;
using Encoder = System.Drawing.Imaging.Encoder;
using Image = SixLabors.ImageSharp.Image;
using PointF = SixLabors.ImageSharp.PointF;
using Point = SixLabors.ImageSharp.Point;
Expand All @@ -33,8 +28,6 @@ public class ScreenshotService : IScreenshotService
private readonly Image<Bgra32> _sun;
private Task<DirectBitmap>? _currentProducer;

private bool UseScreenCapture => false; // OSInfo.IsWindows;

public ScreenshotService()
{
var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "";
Expand All @@ -46,16 +39,6 @@ public ScreenshotService()

_unixJpegEncoder = _unixJpegEncoder = new JpegEncoder() { Quality = 50 };
_jpegEncoder = (source, stream) =>source.Image.Save(stream, _unixJpegEncoder);
if (UseScreenCapture) {
var winJpegEncoder = ImageCodecInfo
.GetImageDecoders()
.Single(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
var winJpegEncoderParameters = new EncoderParameters(1) {
Param = new [] {new EncoderParameter(Encoder.Quality, 50L)}
};
_jpegEncoder = (source, stream) =>
source.Bitmap.Save(stream, winJpegEncoder, winJpegEncoderParameters);
}
}

public virtual async Task<RpcStream<Screenshot>> StreamScreenshots(int width, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -95,16 +78,7 @@ protected virtual Task<DirectBitmap> GetScreenshot(CancellationToken cancellatio
private DirectBitmap TakeScreenshot()
{
var (w, h) = (1280, 720);
if (UseScreenCapture) {
var dd = DisplayInfo.PrimaryDisplayDimensions;
(w, h) = (dd?.Width ?? 1280, dd?.Height ?? 720);
}
var screen = new DirectBitmap(w, h);
if (UseScreenCapture) {
using var gScreen = Graphics.FromImage(screen.Bitmap);
gScreen.CopyFromScreen(0, 0, 0, 0, screen.Bitmap.Size);
return screen;
}

// Unix & Docker version renders the Sun, since screen capture doesn't work there
var now = (CpuTimestamp.Now - _startedAt).TotalSeconds;
Expand Down Expand Up @@ -143,15 +117,6 @@ private Screenshot CreateScreenshotFromBitmap(DirectBitmap source, int width)
using var stream = new MemoryStream(100000);
if (source.Width == width)
_jpegEncoder.Invoke(source, stream);
else if (UseScreenCapture) {
using var target = new DirectBitmap(width, height);
using var gTarget = Graphics.FromImage(target.Bitmap);
gTarget.CompositingQuality = CompositingQuality.HighSpeed;
gTarget.InterpolationMode = InterpolationMode.Bilinear;
gTarget.CompositingMode = CompositingMode.SourceCopy;
gTarget.DrawImage(source.Bitmap, 0, 0, target.Width, target.Height);
_jpegEncoder.Invoke(target, stream);
}
else {
using var iTarget = source.Image.Clone();
iTarget.Mutate(x => x.Resize(width, height));
Expand Down
5 changes: 0 additions & 5 deletions src/Blazor/Server/runtimeconfig.template.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/Blazor/UI/UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- Publish & runtime properties -->
<PropertyGroup>
<RunAOTCompilation>false</RunAOTCompilation>
<PublishTrimmed>true</PublishTrimmed>
<PublishTrimmed>false</PublishTrimmed>
<TrimMode>partial</TrimMode>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
Expand Down

0 comments on commit ceb2c82

Please sign in to comment.