【问题标题】:Drawing rectangle on bitmap ( Mandelbrot)在位图上绘制矩形 (Mandelbrot)
【发布时间】:2014-11-08 00:10:23
【问题描述】:

您好,我接到了将 java 应用程序转换为 c# windows 窗体应用程序的任务。该程序显示一个 Mandelbrot,然后允许用户放大。我已经设法显示 Mandelbrot 甚至缩放。但是,当拖动框进行缩放时,框本身不会显示,这意味着用户无法看到他们将放大到哪个区域。

我认为我需要在拖动时调用绘制矩形的更新函数这是我认为相关的代码。

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    //e.consume();
    if (action)
    {
        xs = e.X;
        ys = e.Y;
    }
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    int z, w;

    //e.consume();
    if (action)
    {
        xe = e.X;
        ye = e.Y;
        if (xs > xe)
        {
            z = xs;
            xs = xe;
            xe = z;
        }
        if (ys > ye)
        {
            z = ys;
            ys = ye;
            ye = z;
        }
        w = (xe - xs);
        z = (ye - ys);
        if ((w < 2) && (z < 2))
            initvalues();
        else
        {
            if (((float)w > (float)z * xy))
                ye = (int)((float)ys + (float)w / xy);
            else
                xe = (int)((float)xs + (float)z * xy);
            xende = xstart + xzoom * (double)xe;
            yende = ystart + yzoom * (double)ye;
            xstart += xzoom * (double)xs;
            ystart += yzoom * (double)ys;
        }
        xzoom = (xende - xstart) / (double)x1;
        yzoom = (yende - ystart) / (double)y1;
        mandelbrot();
        rectangle = false;

        Refresh();
    }
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    //e.consume();
    if (action)
    {
        xe = e.X;
        ye = e.Y;
        rectangle = true;
    }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g1 = e.Graphics;
    g1.DrawImage(bitmap, 0, 0, x1, y1);
    g1.Dispose();
}

public void paint(Graphics g1)
{
    update(g1);
}

public void update(Graphics g1)
{
    Pen pen = new Pen(Color.White);
    g1.DrawImage(bitmap, 0, 0, x1, y1);
    if (rectangle)
    {

        if (xs < xe)
        {
            if (ys < ye) g1.DrawRectangle(pen, xs, ys, (xe - xs), (ye - ys));
            else g1.DrawRectangle(pen, xs, ye, (xe - xs), (ys - ye));
        }
        else
        {
            if (ys < ye) g1.DrawRectangle(pen, xe, ys, (xs - xe), (ye - ys));
            else g1.DrawRectangle(pen, xe, ye, (xs - xe), (ys - ye));
        }
        pen.Dispose();

    }
}

【问题讨论】:

标签: c# bitmap zooming fractals


【解决方案1】:

您需要在 mousemove 事件处理程序中调用 Form.Update()Form.Refresh()。您还需要在Form1_Paint 事件处理程序中调用绘制矩形函数。然后你就准备好了。

【讨论】:

  • 当你说调用绘制矩形函数你的意思是调用update();因为那是绘制矩形函数所在的地方,但这会导致错误,或者将绘制矩形代码从更新移动到绘制?
  • 您可以将“DrawRectangle”代码移动到“Form1_Paint”中,或者从“Form1_Paint”调用更新。重要的是您使用“PaintEventArgs”中的 Graphics 对象绘制矩形
  • 没有必要使用Update()Refresh()。只需在需要重绘 UI 时调用Invalidate(),然后在Paint 事件处理程序中绘制矩形。顺便说一句,这是重复问题中给出的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多