修复暂停字样不显示问题:优化UpdateMaskDisplay方法并确保maskImageBox控件顺序正确
This commit is contained in:
@@ -64,6 +64,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
this.maskImageBox.TabIndex = 1;
|
this.maskImageBox.TabIndex = 1;
|
||||||
this.maskImageBox.TabStop = false;
|
this.maskImageBox.TabStop = false;
|
||||||
this.maskImageBox.BackColor = System.Drawing.Color.Transparent;
|
this.maskImageBox.BackColor = System.Drawing.Color.Transparent;
|
||||||
|
this.maskImageBox.Visible = true;
|
||||||
//
|
//
|
||||||
// contextMenuStrip1
|
// contextMenuStrip1
|
||||||
//
|
//
|
||||||
@@ -187,7 +188,9 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
//
|
//
|
||||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
|
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
|
||||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||||
|
// 先添加imageBox基础层
|
||||||
this.Controls.Add(this.imageBox);
|
this.Controls.Add(this.imageBox);
|
||||||
|
// 再添加maskImageBox遮罩层,确保它显示在上面
|
||||||
this.Controls.Add(this.maskImageBox);
|
this.Controls.Add(this.maskImageBox);
|
||||||
this.Name = "Camera";
|
this.Name = "Camera";
|
||||||
this.Size = new System.Drawing.Size(512, 384);
|
this.Size = new System.Drawing.Size(512, 384);
|
||||||
|
|||||||
@@ -110,106 +110,70 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
{
|
{
|
||||||
Console.WriteLine($"UpdateMaskDisplay called, _isImageUpdatePaused: {_isImageUpdatePaused}");
|
Console.WriteLine($"UpdateMaskDisplay called, _isImageUpdatePaused: {_isImageUpdatePaused}");
|
||||||
|
|
||||||
|
// 在UI线程上操作
|
||||||
|
if (InvokeRequired)
|
||||||
|
{
|
||||||
|
Invoke(new Action(UpdateMaskDisplay));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (_isImageUpdatePaused)
|
if (_isImageUpdatePaused)
|
||||||
{
|
{
|
||||||
// 使用固定大小512*384,与图像层的src图像大小一致
|
// 使用固定大小512*384,与图像层的src图像大小一致
|
||||||
const int bitmapWidth = 512;
|
const int bitmapWidth = 512;
|
||||||
const int bitmapHeight = 384;
|
const int bitmapHeight = 384;
|
||||||
|
|
||||||
Console.WriteLine($"Creating bitmap with fixed size: {bitmapWidth}x{bitmapHeight} using double buffering");
|
Console.WriteLine($"Creating bitmap with fixed size: {bitmapWidth}x{bitmapHeight}");
|
||||||
|
|
||||||
// 创建一个支持双缓冲的位图
|
// 创建位图
|
||||||
Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight);
|
Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight);
|
||||||
|
|
||||||
// 使用双缓冲技术绘制
|
// 直接在bitmap上绘制
|
||||||
using (Graphics g = Graphics.FromImage(bitmap))
|
using (Graphics g = Graphics.FromImage(bitmap))
|
||||||
{
|
{
|
||||||
// 创建缓冲区Graphics对象以实现双缓冲
|
// 设置高质量绘制
|
||||||
BufferedGraphicsContext bufferedGraphicsContext = BufferedGraphicsManager.Current;
|
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||||||
using (BufferedGraphics bufferedGraphics = bufferedGraphicsContext.Allocate(g, new Rectangle(0, 0, bitmapWidth, bitmapHeight)))
|
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||||||
|
|
||||||
|
// 设置半透明背景
|
||||||
|
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
|
||||||
{
|
{
|
||||||
Graphics bufferGraphics = bufferedGraphics.Graphics;
|
g.FillRectangle(brush, 0, 0, bitmapWidth, bitmapHeight);
|
||||||
|
|
||||||
// 设置高质量绘制
|
|
||||||
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, bitmapWidth, bitmapHeight), format);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在UI线程上更新maskImageBox
|
// 释放旧资源并设置新图像
|
||||||
Console.WriteLine("Updating maskImageBox image");
|
if (maskImageBox.Image != null)
|
||||||
if (maskImageBox.InvokeRequired)
|
|
||||||
{
|
{
|
||||||
maskImageBox.Invoke(new Action(() =>
|
maskImageBox.Image.Dispose();
|
||||||
{
|
|
||||||
// 先释放之前的图像资源
|
|
||||||
if (maskImageBox.Image != null)
|
|
||||||
{
|
|
||||||
maskImageBox.Image.Dispose();
|
|
||||||
}
|
|
||||||
maskImageBox.Image = bitmap;
|
|
||||||
maskImageBox.Visible = true;
|
|
||||||
Console.WriteLine("maskImageBox updated via Invoke");
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 先释放之前的图像资源
|
|
||||||
if (maskImageBox.Image != null)
|
|
||||||
{
|
|
||||||
maskImageBox.Image.Dispose();
|
|
||||||
}
|
|
||||||
maskImageBox.Image = bitmap;
|
|
||||||
maskImageBox.Visible = true;
|
|
||||||
Console.WriteLine("maskImageBox updated directly");
|
|
||||||
}
|
}
|
||||||
|
maskImageBox.Image = bitmap;
|
||||||
|
maskImageBox.Visible = true;
|
||||||
|
Console.WriteLine("maskImageBox updated");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 清除遮罩
|
// 清除遮罩
|
||||||
Console.WriteLine("Clearing maskImageBox image");
|
Console.WriteLine("Clearing maskImageBox image");
|
||||||
if (maskImageBox.InvokeRequired)
|
if (maskImageBox.Image != null)
|
||||||
{
|
{
|
||||||
maskImageBox.Invoke(new Action(() =>
|
maskImageBox.Image.Dispose();
|
||||||
{
|
maskImageBox.Image = null;
|
||||||
if (maskImageBox.Image != null)
|
|
||||||
{
|
|
||||||
maskImageBox.Image.Dispose();
|
|
||||||
maskImageBox.Image = null;
|
|
||||||
}
|
|
||||||
Console.WriteLine("maskImageBox cleared via Invoke");
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (maskImageBox.Image != null)
|
|
||||||
{
|
|
||||||
maskImageBox.Image.Dispose();
|
|
||||||
maskImageBox.Image = null;
|
|
||||||
}
|
|
||||||
Console.WriteLine("maskImageBox cleared directly");
|
|
||||||
}
|
}
|
||||||
|
maskImageBox.Visible = false; // 非暂停时隐藏遮罩
|
||||||
|
Console.WriteLine("maskImageBox cleared");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user