【问题标题】:Picturebox refresh C# without redrawing the current drawingPicturebox刷新C#而不重绘当前绘图
【发布时间】:2016-03-20 04:18:27
【问题描述】:

我在网上找到了这段代码,我想用它在我的图纸上画一个蓝色的小盒子。但是当我点击图片框时,会出现蓝色框,但它会重绘我的整个图片框,这是一个问题,因为我有一张非常复杂的图片,重绘需要几秒钟。有什么办法可以绕过无效吗?也许是图片框内的图片框,背面图片框是我的复杂绘图,正面图片框是刷新的蓝色框?我似乎无法让前面的图片框背景颜色透明,有人可以帮我吗?

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    // Determine the initial rectangle coordinates...
    if (MeasureMentimported && !selectieBezig)
    {
        RectStartPoint = e.Location;
        //Invalidate();
        selectieBezig = true;
    }
}

// Draw Rectangle
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    //Als er niet links wordt geklikt, dan gewoon weer terug.
    if (e.Button != MouseButtons.Left)
        return;

    //Alleen als de measurementfile is geladen, gaan we kijken of er iets te selecteren valt.
    if (MeasureMentimported && selectieBezig)
    {
        Point tempEndPoint = e.Location;
        int X1 = Math.Min(RectStartPoint.X, tempEndPoint.X);
        int Y1 = Math.Min(RectStartPoint.Y, tempEndPoint.Y);
        int X2 = Math.Max(RectStartPoint.X, tempEndPoint.X);
        int Y2 = Math.Max(RectStartPoint.Y, tempEndPoint.Y);

        Rect.Location = new Point(X1, Y1);
        Rect.Size = new Size(X2 - X1, Y2 - Y1);

        picTekenvlak.Invalidate();

        //Rect.Location = new Point(
        //    Math.Min(RectStartPoint.X, tempEndPoint.X),
        //    Math.Min(0, 1000));
        //Rect.Size = new Size(
        //    Math.Abs(RectStartPoint.X - tempEndPoint.X),
        //    Math.Abs(1000));
        // MessageBox.Show("k");
    }
}

// Draw Area
//
private void pictureBox1_Paint1(object sender, System.Windows.Forms.PaintEventArgs e)
{
    if (MeasureMentimported)
    {
        if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
        {
            e.Graphics.FillRectangle(selectionBrush, Rect);
        }
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    selectieBezig = false;
    if (e.Button == MouseButtons.Right)
    {
        if (Rect.Contains(e.Location))
        {
            Console.WriteLine("Right click");
        }
    }
}

【问题讨论】:

  • 这里有一些提示可能会有所帮助 1. 您可以尝试将复杂图像缓存到缓冲区中,这样重新绘制不会花费太多时间; 2.据我所知,您需要将Windows(图像主机)背景画笔设置为透明,这样您就可以避免在重新绘制时闪烁(也许您必须覆盖重新绘制的行为)窗户); 3.也许你可以创建另一个矩形窗口而不是在顶部绘制,并将其放在图像容器的顶部
  • 要使 winforms 中的控件透明,它必须嵌套在下面的控件中。对于图片框,您需要在代码中执行此操作pbox1.parent = pbox0; - 您可以尝试仅使旧矩形和新矩形的外边界无效:invalidate(Rectangle.Union(oldRect, newRect)); 另外:什么是“复杂”图像??大的?多大?还有:picTekenvlak 是什么?

标签: c# picturebox transparent


【解决方案1】:

OnPaint 事件发送一个PaintEventArgs 对象,该对象描述了根据PaintEventArgs.ClipRectangle 重绘多少。您可以通过将调用中蓝色矩形的坐标发送到Invalidate 来控制它。

然后,您将需要使用这些坐标来确定您重绘了多少。这也将减少您的应用程序对任何类型的覆盖所做的工作量,例如将另一个应用程序拖到 PictureBox 上等。

您可能想尝试将复杂图像绘制到Bitmap,然后将该位图分配给PictureBox.Image。我不知道这是否可行,因为您直接在PictureBox 图像表面上绘图,但如果您可以使其正常工作,Windows 将处理剪辑以为您呈现图像,这是理想的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2013-02-13
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多