Skip to content

Commit

Permalink
Add temporary workaround for System.Drawing bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Nov 17, 2024
1 parent 7ca1019 commit 3303a83
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task<Result> CreateSession(

var streamerPath = fileSystem
.GetFiles(streamerBin, AppConstants.StreamerFileName, SearchOption.AllDirectories)
.FirstOrDefault();
.LastOrDefault();

if (string.IsNullOrWhiteSpace(streamerPath))
{
Expand Down
16 changes: 10 additions & 6 deletions Libraries/ControlR.Libraries.ScreenCapture/BitmapUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public interface IBitmapUtility

public class BitmapUtility : IBitmapUtility
{
// TODO: This is throwing in .NET 9. Maybe need to replace with SkiaSharp.
//private readonly ImageCodecInfo _jpegEncoder = ImageCodecInfo
// .GetImageEncoders()
// .First(x => x.FormatID == ImageFormat.Jpeg.Guid);
private ImageCodecInfo? _jpegEncoder;

public Bitmap CropBitmap(Bitmap bitmap, Rectangle cropArea)
{
Expand All @@ -36,11 +33,18 @@ public byte[] Encode(Bitmap bitmap, ImageFormat format)

public byte[] EncodeJpeg(Bitmap bitmap, int quality)
{
if (_jpegEncoder is null)
{
// HACK: Temporary workaround for https://github.com/dotnet/winforms/issues/12494#issuecomment-2481430723
_ = Pens.Black;
_jpegEncoder = ImageCodecInfo
.GetImageEncoders()
.First(x => x.FormatID == ImageFormat.Jpeg.Guid);
}
using var ms = new MemoryStream();
using var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
//bitmap.Save(ms, _jpegEncoder, encoderParams);
bitmap.Save(ms, ImageFormat.Jpeg);
bitmap.Save(ms, _jpegEncoder, encoderParams);
return ms.GetBuffer();
}

Expand Down

0 comments on commit 3303a83

Please sign in to comment.