Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 图片框图像闪烁_C#_Image_Winforms_Visual Studio_Picturebox - Fatal编程技术网

C# 图片框图像闪烁

C# 图片框图像闪烁,c#,image,winforms,visual-studio,picturebox,C#,Image,Winforms,Visual Studio,Picturebox,我有一个包含多个picturebox的面板 我想让用户选择任何picturebox的任何部分 用户将通过鼠标选择它 我想在picturebox上画一个半透明的矩形,同时鼠标按照选择移动 代码运行正常,但矩形正在闪烁我想停止闪烁。 我试过使用双缓冲区 此外,使用 但不起作用。请帮忙 我的代码: private Brush selectionBrush = new SolidBrush(Color.FromArgb(70, 76, 255, 0)); private void Picture_Mo

我有一个包含多个picturebox的面板

我想让用户选择任何picturebox的任何部分

用户将通过鼠标选择它

我想在picturebox上画一个半透明的矩形,同时鼠标按照选择移动

代码运行正常,但矩形正在闪烁我想停止闪烁。

我试过使用双缓冲区

此外,使用

但不起作用。请帮忙

我的代码:

private Brush selectionBrush = new SolidBrush(Color.FromArgb(70, 76, 255, 0));

private void Picture_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left) return;

    PictureBox pb = (PictureBox)(sender);

    Point tempEndPoint = e.Location;
    Rect.Location = new Point(
        Math.Min(RecStartpoint.X, tempEndPoint.X),
        Math.Min(RecStartpoint.Y, tempEndPoint.Y));
    Rect.Size = new Size(
        Math.Abs(RecStartpoint.X - tempEndPoint.X),
        Math.Abs(RecStartpoint.Y - tempEndPoint.Y));

    pb.CreateGraphics().FillRectangle(selectionBrush, Rect);
    pb.Invalidate(Rect);
}

您指出双缓冲解决方案不适用于您,您找到原因了吗?因为以下自定义控件在picturebox上绘制了一个不闪烁的大矩形:

public class TryDoubleBufferAgain : PictureBox
{
    public TryDoubleBufferAgain()
    {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.UpdateStyles();
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        this.Refresh();
        base.OnMouseMove(e);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // Edit below to actually  draw a usefull rectangle
        e.Graphics.DrawRectangle(System.Drawing.Pens.Red, new System.Drawing.Rectangle(0, 0, Cursor.Position.X, Cursor.Position.Y));
    }
}

这并不能解决PictureBox问题,但可能会有所帮助

首先,我用面板替换了PictureBox,面板也可以用来画画。接下来,我激活了双缓冲:

在命名空间中的某个位置添加此类:

class DoubleBufferedPanel : Panel 
{ 
    public DoubleBufferedPanel() : base() 
    { 
        DoubleBuffered = true; 
    } 
 }
然后替换为 Form1.Designer.cs 所有“System.Windows.Forms.Panel”和“DoubleBufferedPanel”


这在我的许多图形应用程序中都可以正常工作。

如果删除
pb.Invalidate(Rect)
@LeiYang绘制的矩形没有透明度,不会在鼠标向上移动时消失。我想您需要在
paint
事件中绘制全部/部分,这样您可以在其他按钮事件中调用
Invalidate
。最好在paint handler中进行绘制。你应该改变你的方法。你昨天问的那个问题怎么样?我在
鼠标上给你打电话的那个?你会一直问问题直到有人给你写申请书?