Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v6

- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: 10.0.x

- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
queries: +security-extended

- name: Autobuild
run: |
Expand Down
11 changes: 10 additions & 1 deletion src/COM/DesktopWallpaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public struct COLORREF
public byte B;
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

public enum DesktopSlideshowOptions
{
DSO_SHUFFLEIMAGES = 0x01,
Expand Down Expand Up @@ -59,7 +68,7 @@ public interface IDesktopWallpaper

uint GetMonitorDevicePathCount();

System.Windows.Rect GetMonitorRECT([MarshalAs(UnmanagedType.LPWStr)] string monitorID);
RECT GetMonitorRECT([MarshalAs(UnmanagedType.LPWStr)] string monitorID);

void SetBackgroundColor([MarshalAs(UnmanagedType.U4)] COLORREF color);

Expand Down
160 changes: 160 additions & 0 deletions src/Skia/ImageCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using SkiaSharp;

namespace WinDynamicDesktop.Skia
{
sealed class ImageCache
{
readonly int maxWidth;
readonly int maxHeight;
readonly object cacheLock = new object();
readonly Dictionary<Uri, SKImage> images = new Dictionary<Uri, SKImage>();

public SKImage this[Uri uri]
{
get
{
lock (cacheLock)
{
if (images.TryGetValue(uri, out var image))
{
return image;
}

var img = CreateImage(uri);
if (img != null)
{
images.Add(uri, img);
}
return img;
}
}
}

public void Clear()
{
lock (cacheLock)
{
foreach (var image in images.Values)
{
image?.Dispose();
}
images.Clear();
}
GC.Collect();
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to 'GC.Collect()'.

Suggested change
GC.Collect();

Copilot uses AI. Check for mistakes.
}

public ImageCache(bool limitDecodeSize = true)
{
if (limitDecodeSize)
{
int maxArea = 0;
foreach (Screen screen in Screen.AllScreens)
{
int area = screen.Bounds.Width * screen.Bounds.Height;
if (area > maxArea)
{
maxArea = area;
maxWidth = screen.Bounds.Width;
maxHeight = screen.Bounds.Height;
}
}
}
else
{
maxWidth = int.MaxValue;
maxHeight = int.MaxValue;
}
}

private SKImage CreateImage(Uri uri)
{
try
{
Stream stream = null;

if (uri.IsAbsoluteUri && uri.Scheme == "file")
{
string path = uri.LocalPath;
if (File.Exists(path))
{
stream = File.OpenRead(path);
}
}
else if (!uri.IsAbsoluteUri)
{
// Embedded resource
stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(uri.OriginalString);
}

if (stream == null)
{
return null;
}

using (stream)
{
using (var codec = SKCodec.Create(stream))
{
if (codec == null)
{
return null;
}

var info = codec.Info;

// Calculate target dimensions
int targetWidth = info.Width;
int targetHeight = info.Height;

if (info.Width > maxWidth || info.Height > maxHeight)
{
float scale = Math.Min((float)maxWidth / info.Width, (float)maxHeight / info.Height);
targetWidth = (int)(info.Width * scale);
targetHeight = (int)(info.Height * scale);
}

// Decode at native size
using (var sourceBitmap = new SKBitmap(info))
{
if (codec.GetPixels(sourceBitmap.Info, sourceBitmap.GetPixels()) != SKCodecResult.Success)
{
return null;
}

// If scaling is needed, create scaled version with high quality
if (targetWidth != sourceBitmap.Width || targetHeight != sourceBitmap.Height)
{
using (var scaledBitmap = new SKBitmap(targetWidth, targetHeight, info.ColorType, info.AlphaType))
{
sourceBitmap.ScalePixels(scaledBitmap, new SKSamplingOptions(SKCubicResampler.Mitchell));
var image = SKImage.FromBitmap(scaledBitmap);
return image;
Comment on lines +140 to +141
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing null check after SKImage.FromBitmap() calls. If the bitmap conversion fails, this could return null but the code doesn't handle this case. Consider adding null checks before returning the image, or use a guard clause to ensure the image is not null before adding it to the cache.

Copilot uses AI. Check for mistakes.
}
}
else
{
// No scaling needed
var image = SKImage.FromBitmap(sourceBitmap);
return image;
Comment on lines +147 to +148
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing null check after SKImage.FromBitmap() call. If the bitmap conversion fails, this could return null but the code doesn't handle this case. Consider adding a null check before returning the image.

Copilot uses AI. Check for mistakes.
}
}
}
}
}
catch
{
return null;
}
}
}
}
Loading
Loading