为遮罩层实现双缓冲机制并将bitmap大小固定为512*384

This commit is contained in:
zqm
2025-10-30 08:52:53 +08:00
parent 8ea1829cf2
commit f9404dd259

View File

@@ -108,41 +108,53 @@ namespace JoyD.Windows.CS.Toprie
{ {
try try
{ {
Console.WriteLine($"UpdateMaskDisplay called, _isImageUpdatePaused: {_isImageUpdatePaused}, maskImageBox: {maskImageBox}, maskImageBox.Size: {maskImageBox.Width}x{maskImageBox.Height}"); Console.WriteLine($"UpdateMaskDisplay called, _isImageUpdatePaused: {_isImageUpdatePaused}");
if (_isImageUpdatePaused) if (_isImageUpdatePaused)
{ {
// 确保maskImageBox大小有效 // 使用固定大小512*384与图像层的src图像大小一致
int width = Math.Max(100, maskImageBox.Width); const int bitmapWidth = 512;
int height = Math.Max(100, maskImageBox.Height); const int bitmapHeight = 384;
Console.WriteLine($"Creating bitmap with size: {width}x{height}"); Console.WriteLine($"Creating bitmap with fixed size: {bitmapWidth}x{bitmapHeight} using double buffering");
// 创建一个位图 // 创建一个支持双缓冲的位图
Bitmap bitmap = new Bitmap(width, height); Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight);
// 使用双缓冲技术绘制
using (Graphics g = Graphics.FromImage(bitmap)) using (Graphics g = Graphics.FromImage(bitmap))
{ {
// 设置高质量绘制 // 创建缓冲区Graphics对象以实现双缓冲
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; BufferedGraphicsContext bufferedGraphicsContext = BufferedGraphicsManager.Current;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; using (BufferedGraphics bufferedGraphics = bufferedGraphicsContext.Allocate(g, new Rectangle(0, 0, bitmapWidth, bitmapHeight)))
// 设置半透明背景
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
{ {
g.FillRectangle(brush, 0, 0, bitmap.Width, bitmap.Height); Graphics bufferGraphics = bufferedGraphics.Graphics;
// 设置高质量绘制
bufferGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
bufferGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
// 设置半透明背景
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
{
bufferGraphics.FillRectangle(brush, 0, 0, bitmapWidth, bitmapHeight);
}
// 设置文本样式
Font font = new Font("微软雅黑", 48, FontStyle.Bold);
Brush textBrush = new SolidBrush(Color.White);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// 绘制"暂停"文字
Console.WriteLine("Drawing '暂停' text");
bufferGraphics.DrawString("暂停", font, textBrush,
new Rectangle(0, 0, bitmapWidth, bitmapHeight), format);
// 将缓冲区内容绘制到目标位图
bufferedGraphics.Render(g);
} }
// 设置文本样式
Font font = new Font("微软雅黑", 48, FontStyle.Bold);
Brush textBrush = new SolidBrush(Color.White);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// 绘制"暂停"文字
Console.WriteLine("Drawing '暂停' text");
g.DrawString("暂停", font, textBrush,
new Rectangle(0, 0, bitmap.Width, bitmap.Height), format);
} }
// 在UI线程上更新maskImageBox // 在UI线程上更新maskImageBox
@@ -151,6 +163,11 @@ namespace JoyD.Windows.CS.Toprie
{ {
maskImageBox.Invoke(new Action(() => maskImageBox.Invoke(new Action(() =>
{ {
// 先释放之前的图像资源
if (maskImageBox.Image != null)
{
maskImageBox.Image.Dispose();
}
maskImageBox.Image = bitmap; maskImageBox.Image = bitmap;
maskImageBox.Visible = true; maskImageBox.Visible = true;
Console.WriteLine("maskImageBox updated via Invoke"); Console.WriteLine("maskImageBox updated via Invoke");
@@ -158,6 +175,11 @@ namespace JoyD.Windows.CS.Toprie
} }
else else
{ {
// 先释放之前的图像资源
if (maskImageBox.Image != null)
{
maskImageBox.Image.Dispose();
}
maskImageBox.Image = bitmap; maskImageBox.Image = bitmap;
maskImageBox.Visible = true; maskImageBox.Visible = true;
Console.WriteLine("maskImageBox updated directly"); Console.WriteLine("maskImageBox updated directly");
@@ -171,13 +193,21 @@ namespace JoyD.Windows.CS.Toprie
{ {
maskImageBox.Invoke(new Action(() => maskImageBox.Invoke(new Action(() =>
{ {
maskImageBox.Image = null; if (maskImageBox.Image != null)
{
maskImageBox.Image.Dispose();
maskImageBox.Image = null;
}
Console.WriteLine("maskImageBox cleared via Invoke"); Console.WriteLine("maskImageBox cleared via Invoke");
})); }));
} }
else else
{ {
maskImageBox.Image = null; if (maskImageBox.Image != null)
{
maskImageBox.Image.Dispose();
maskImageBox.Image = null;
}
Console.WriteLine("maskImageBox cleared directly"); Console.WriteLine("maskImageBox cleared directly");
} }
} }