Optimize bitmap rendering in PluginWindow

This commit is contained in:
Stone_Red
2026-01-23 21:13:40 +01:00
parent 2853d49dd9
commit 1e9c74af52
2 changed files with 47 additions and 20 deletions
+1
View File
@@ -15,6 +15,7 @@
<TargetFramework>net8.0-windows7.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
+46 -20
View File
@@ -42,6 +42,9 @@ public partial class PluginWindow : Window, IPluginWindow
private System.Timers.Timer? reloadDebounceTimer;
private bool isReloading = false;
private WriteableBitmap? writeableBitmap;
private BitmapScalingMode lastBitmapScalingMode = BitmapScalingMode.Unspecified;
public bool IsRunning { get; private set; } = true;
public PluginMetadata PluginMetadata { get; private set; }
public string PluginFolderPath { get; private set; }
@@ -337,22 +340,52 @@ public partial class PluginWindow : Window, IPluginWindow
WindowPos.GetWindowLong(helper.Handle, WindowPos.GWL_EXSTYLE) | WindowPos.WS_EX_NOACTIVATE);
}
private static BitmapSource BitmapToImageSource(Bitmap bitmap)
private void UpdateImageFromBitmap(Bitmap bitmap, BitmapScalingMode scalingMode)
{
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, bitmap.PixelFormat);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
BitmapSource bitmapSource = BitmapSource.Create(
bitmapData.Width, bitmapData.Height,
bitmap.HorizontalResolution, bitmap.VerticalResolution,
PixelFormats.Bgra32, null,
bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);
try
{
Dispatcher.Invoke(() =>
{
if (lastBitmapScalingMode != scalingMode)
{
RenderOptions.SetBitmapScalingMode(image, scalingMode);
lastBitmapScalingMode = scalingMode;
}
bitmap.UnlockBits(bitmapData);
if (writeableBitmap == null || writeableBitmap.PixelWidth != bitmapData.Width || writeableBitmap.PixelHeight != bitmapData.Height)
{
writeableBitmap = new WriteableBitmap(
bitmapData.Width, bitmapData.Height,
bitmap.HorizontalResolution, bitmap.VerticalResolution,
PixelFormats.Bgra32, null);
image.Source = writeableBitmap;
}
bitmapSource.Freeze();
return bitmapSource;
writeableBitmap.Lock();
try
{
unsafe
{
Buffer.MemoryCopy(
bitmapData.Scan0.ToPointer(),
writeableBitmap.BackBuffer.ToPointer(),
writeableBitmap.BackBufferStride * writeableBitmap.PixelHeight,
bitmapData.Stride * bitmapData.Height);
}
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, bitmapData.Width, bitmapData.Height));
}
finally
{
writeableBitmap.Unlock();
}
});
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}
private void Window_ContentRendered(object? sender, EventArgs e)
@@ -826,14 +859,7 @@ public partial class PluginWindow : Window, IPluginWindow
_ => BitmapScalingMode.Unspecified
};
// Update Image
BitmapSource frozenSource = BitmapToImageSource(result);
_ = Dispatcher.BeginInvoke(() =>
{
RenderOptions.SetBitmapScalingMode(image, renderOptions);
image.Source = frozenSource;
});
UpdateImageFromBitmap(result, renderOptions);
}
}
}