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?
The way to do it is to create a
DrawAction
class which holds all the data needed for the things you want to draw: ThePoint
data, thePen
orBrush
etc.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 thePictureBox
or aPanel
(or any control with aPaint
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..
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 theWidth
or theLineStyle
of thePen
or shiftingPoints
a little etc etc!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 theGraphics
class offers:Drawxx
,Fillxxx
,Pen
properties,Colors
maybe even zoom..For starters a Type, a
List<Point>
afloat PenWidth
and aColor
will do..Here is an example with a simple class, the
Paint
event, code to add a few test actions andboth
:Image
of aPictureBox
.The test data are one
Line
and one set ofPolylines
.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 includeRectangle, 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 controlRotation, Scaling, Gradients, Transparency
etc..The result for both looks the same:
Try creating the Bitmap outside of the function to preserve it, the way you are doing it now disposes of the Bitmap element after the function has completed.
you could then do something like
Just to get you started.. :)
Alternatively you can pass the bmp into the draw function if you use this method to draw multiple bmp at different times