更新绘图,而不删除previous 1 [英] Update a drawing without deleting the previous one

查看:92
本文介绍了更新绘图,而不删除previous 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的绘图方法中创建了这个

I created this within a drawing method

private void draws()
{
 Bitmap bmp = new Bitmap(pictureBox17.Width, pictureBox17.Height);
 using (Graphics g = Graphics.FromImage(bmp))
            {
                //define area do pictureBox17 e preenche a branco
                Brush brush = new SolidBrush(Color.White);
                Rectangle area = new Rectangle(0, 0, pictureBox17.Width, pictureBox17.Height);
                g.FillRectangle(brush, area);
                //desenha as linhas do rectangulo
                g.DrawLine(new Pen(Color.Black), esp, esp, esp, yWcorrigidoesp);
// some more lines
}
 pictureBox17.Image = bmp;
}

和它吸引我想要什么。但是想象一下,这个我想以后更新完全相同的图纸,增加了几行字,而无需重新绘制所有,这可能吗?很显然,我的使用方法

And it draws exactly what i want. But imagine that after this i wanna update the exactly same drawing, ADDING a few lines, without having to draw all again, is this possible? Obviously i'm using the method

draws();

然后我想补充一点,任何提示?

And then i wanna add something, any tips?

推荐答案

要做到这一点的方法是创建一个拥有所需的东西全部数据的 DrawAction 类要绘制:在数据显示,等。

The way to do it is to create a DrawAction class which holds all the data needed for the things you want to draw: The Point data, the Pen or Brush etc.

然后创建和管理列表与LT; D​​rawAction> drawActions ,然后你有一个选择:

Then you create and manage a List<DrawAction> drawActions and then you have a choice:


  • 您要么做所有的绘图'活'在油漆图片框的事件面板(或任何油漆事件控制),通过循环在列表..

  • You either do all the drawing 'live' in the Paint event of the PictureBox or a Panel (or any control with a Paint event) by looping over the list..

..或者你添加新的操作位图图像你正在建立。

..Or you add the new Actions into the Bitmap Image you are building up.

什么是更好的真的取决于:你期望做动态绘制,说由用户操作?你想撤销/重做选项?接活绘制的控制面是一个小更适合。

What is better really depends: do you expect to do dynamic drawing, say by user actions? Do you want an undo/redo option? Then live drawing onto the control surface is a little better suited.

或者是事情的清单做,或从一组固定的数据得出,这意味着它们最终被保存到磁盘。这听起来更像绘画的的位图。

Or is the list of things to do fixed or derive from a fixed set of data and meant to be eventually saved to disk. That sound more like drawing into the bitmap.

两者也可以合并,也许收集了若干,同时保持撤消的选择动作(通过除去最后列表项),并提供了一​​个应用按钮将它们泵入位图..

Both can also be combined, maybe collect a number of actions while keeping the option of undoing (by removing the last list item) and offering an Apply button to pump them into the bitmap..

注意:,以画的东西,关键是为保持绘制数据准备在列表中这样你就可以当你再次需要使用它,扩展和删除列表甚至改变它:这将是一个简单的两班轮去了所有的行动,并修改颜色宽度的LineStyle 笔或移点> 稍微等等等等!

Note: The key to drawing stuff is to keep the drawing data ready in a list so you can use it when you need it again, expand and delete the list and even change it: It would be a simple two-liner to go over all the actions and to change the Color or the Width or the LineStyle of the Pen or shifting Points a little etc etc!

在创建 DrawAction 类它帮助,如果你可以决定你需要哪些操作。如果你不能,你仍然可以去有足够的成员更多的扩展类所有的许多选项上班图形类提供了: Drawxx Fillxxx 属性,颜色甚至放大。

When you create the DrawAction class it helps if you can decide which actions you will need. If you can't you still can go for a more extended class with enough members to work with all the many options the Graphics class offers: Drawxx, Fillxxx, Pen properties, Colors maybe even zoom..

对于初学者A型,列表&LT;点&GT; A 浮动PenWidth 颜色将尽..

For starters a Type, a List<Point> a float PenWidth and a Color will do..

下面是一个简单的类为例,油漆事件,code以添加一些测试行动和两个

Here is an example with a simple class, the Paint event, code to add a few test actions and both:


  • A键做现场绘图和..

  • ..一个适用的动作到应用的操作到图片框的位图图片

  • A button to do live drawing and..
  • ..one to apply the action to the apply the actions to the bitmap Image of a PictureBox.

测试数据有一个和一组折线

The test data are one Line and one set of Polylines.

您应该开始改善它通过定义一个枚举与所有类型要使用绘图行动!这将是更好,更容易理解的小气鬼字符类型我有codeD ;-)类型可以包括长方形,FilledRectangle,椭圆,FilledEllipse,线,线,多边形,FilledPolygon,文本,曲线,曲线,然后一些。随着一点点额外的你也可以用图像工作,GraphicsPath的,花键..等数据可以控制旋转,缩放,渐变,透明度

You should start improving on it by defining an Enum with all the types of drawing actions you want to use! This will be much better and easier to understand the the cheapo character type I have coded ;-) The types could include Rectangle, FilledRectangle, Ellipse, FilledEllipse, Line, Lines, Polygon, FilledPolygon, Text, Curve, Curves and then some. With a little extra you can also work with Image, GraphicsPath, Spline.. And other data could control Rotation, Scaling, Gradients, Transparency etc..

List<DrawAction> actions = new List<DrawAction>();

public class DrawAction
{
    public char type { get; set; }             // this should be an Enum!
    public Color color { get; set; }     
    public float penWidth { get; set; }        // only one of many Pen properties!
    public List<Point> points { get; set; }    // use PointF for more precision

    public DrawAction(char type_, Color color_, float penwidth_)
    {
        type = type_; color = color_; penWidth = penwidth_;
        points = new List<Point>();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Draw(e.Graphics, actions);
}


public void Draw(Graphics G, List<DrawAction> actions)
{
    foreach (DrawAction da in actions)
        if (da.type == 'L' && da.points.Count > 1)
            using (Pen pen = new Pen(da.color, da.penWidth))
                G.DrawLine(pen, da.points[0], da.points[1]);
        else if (da.type == 'P' && da.points.Count > 1)
            using (Pen pen = new Pen(da.color, da.penWidth))
                G.DrawLines(pen, da.points.ToArray());
    // else..


}

private void button1_Click(object sender, EventArgs e)
{
    AddTestActions();
    pictureBox1.Invalidate();
}

private void button2_Click(object sender, EventArgs e)
{
    AddTestActions();
    Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, 
                            pictureBox1.ClientSize.Height);
    using (Graphics G = Graphics.FromImage(bmp))  Draw(G, actions);
    pictureBox1.Image = bmp;
}

void AddTestActions()
{
    actions.Add(new DrawAction('L', Color.Blue, 3.3f));
    actions[0].points.Add(new Point(23, 34));
    actions[0].points.Add(new Point(23, 134));
    actions.Add(new DrawAction('P', Color.Red, 1.3f));
    actions[1].points.Add(new Point(11, 11));
    actions[1].points.Add(new Point(55, 11));
    actions[1].points.Add(new Point(55, 77));
    actions[1].points.Add(new Point(11, 77));
}

两者的结果看起来是一样的:

The result for both looks the same:

这篇关于更新绘图,而不删除previous 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆