473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to delete (clear) lines

Hello

How can I delete (clear) lines that were made useing Graphics.DrawLine() ?

Thanks in advance! Have a nice day!

Nov 15 '05 #1
2 20813
In VS 6.0 and in OpenGL, this is fairly simple, you just use the XOR
drawing mode, which draws a line normally the first time, and then
"erases" it the second time.

GDI+ doesn't have support for the pen modes anymore, but your task is
not too much more difficult.

There are a couple ways to do this:

1. you could keep a bitmap of what the clear, or erased state looks
like, and whenever you want to erase the line, you would do something like:
//...
private Image savedImage;
//...

// your standard paint method
private void My_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (something_has_changed_like_the_form_has_been_resi zed)
{
Rectangle rect = this.GetDrawingRectangle();
this.savedImage = new Bitmap(rect.Width, rect.Height);
Graphics gx = Graphics.FromImage(savedImage);
gx.Clear(this.BackColor);

// draw the normal, erased state
// ...
gx.Dispose();
}

Graphics g = e.Graphics;
g.DrawImageUnscaled(this.savedImage,0,0);

// now draw any of the lines, pictures, etc.
// that you want to be erased.
}
2. The previous method is good if you need to completely redraw every
element of your picture each time, but what if you have a bunch of lines
already drawn, and you only want to erase one of them? Here's where you
can use a TextureBrush as a sort of eraser.

//...
private Image lastImage;
private Brush eraserBrush;
//...

// your standard paint method
private void My_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (something_has_changed_like_the_form_has_been_resi zed)
{
// we may need to redraw everything...
Rectangle rect = this.GetDrawingRectangle();
this.lastImage = new Bitmap(rect.Width, rect.Height);
Graphics gx = Graphics.FromImage(this.lastImage);
gx.Clear(this.BackColor);

// draw the normal, erased state
// ...
// draw all of the elements that should be drawn...
gx.Dispose();
this.eraserBrush = new TextureBrush(this.lastImage);
}

// all of our lines, pictures, etc. are already stored
// inside lastImage, so we don't need to do anything here.

// let's say we kept track of some coordinates for a line,
// and also kept track of whether or not we need to do
// some erasing
if (bLineNeedsErasing)
{
// by drawing to this Graphics object, we're updating the
// bitmap image.
Graphics imageGraphics = Graphics.FromImage(this.lastImage);

// assuming the first line was drawn with a width of 1 (float)
Pen p = new Pen(this.eraserBrush, 1F);

// assuming we kept track of a couple System.Drawing.PointF
// points representing the ends of the line
imageGraphics.DrawLine(p, this.linePoint1, this.linePoint2);
imageGraphics.Dispose();
}

Graphics g = e.Graphics;
g.DrawImageUnscaled(this.lastImage,0,0);
}
This second way is almost always the better way to go.

One last note: you're going to notice the screen flickering if you're
doing any kind of drawing. To alleviate this, look up the SetStyle
method, and use something like:

this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

in your own initialization code.
Tomomichi Amano wrote:
Hello

How can I delete (clear) lines that were made useing Graphics.DrawLine() ?

Thanks in advance! Have a nice day!


Nov 15 '05 #2
Another option is to keep track of everything as you paint it in some sort
of collection, then have an engine that can paint all of those to the
picturebox / form /bitmap whatever. To erase a line, you remove it from the
collection, and repaint everything else. I suppose this could be slow if
there are a lot of lines (and you'll want to use the anti-flicker methods
that Ben listed)

Another option to look at is the ControlPaint class - there is a method
DrawReversibleLine. I haven't used it before, but you can surely find some
examples.

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
"Ben T." <be*@jbmsystems.com> wrote in message
news:io****************@nwrdny03.gnilink.net...
In VS 6.0 and in OpenGL, this is fairly simple, you just use the XOR
drawing mode, which draws a line normally the first time, and then
"erases" it the second time.

GDI+ doesn't have support for the pen modes anymore, but your task is
not too much more difficult.

There are a couple ways to do this:

1. you could keep a bitmap of what the clear, or erased state looks
like, and whenever you want to erase the line, you would do something like:

//...
private Image savedImage;
//...

// your standard paint method
private void My_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (something_has_changed_like_the_form_has_been_resi zed)
{
Rectangle rect = this.GetDrawingRectangle();
this.savedImage = new Bitmap(rect.Width, rect.Height);
Graphics gx = Graphics.FromImage(savedImage);
gx.Clear(this.BackColor);

// draw the normal, erased state
// ...
gx.Dispose();
}

Graphics g = e.Graphics;
g.DrawImageUnscaled(this.savedImage,0,0);

// now draw any of the lines, pictures, etc.
// that you want to be erased.
}
2. The previous method is good if you need to completely redraw every
element of your picture each time, but what if you have a bunch of lines
already drawn, and you only want to erase one of them? Here's where you
can use a TextureBrush as a sort of eraser.

//...
private Image lastImage;
private Brush eraserBrush;
//...

// your standard paint method
private void My_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (something_has_changed_like_the_form_has_been_resi zed)
{
// we may need to redraw everything...
Rectangle rect = this.GetDrawingRectangle();
this.lastImage = new Bitmap(rect.Width, rect.Height);
Graphics gx = Graphics.FromImage(this.lastImage);
gx.Clear(this.BackColor);

// draw the normal, erased state
// ...
// draw all of the elements that should be drawn...
gx.Dispose();
this.eraserBrush = new TextureBrush(this.lastImage);
}

// all of our lines, pictures, etc. are already stored
// inside lastImage, so we don't need to do anything here.

// let's say we kept track of some coordinates for a line,
// and also kept track of whether or not we need to do
// some erasing
if (bLineNeedsErasing)
{
// by drawing to this Graphics object, we're updating the
// bitmap image.
Graphics imageGraphics = Graphics.FromImage(this.lastImage);

// assuming the first line was drawn with a width of 1 (float)
Pen p = new Pen(this.eraserBrush, 1F);

// assuming we kept track of a couple System.Drawing.PointF
// points representing the ends of the line
imageGraphics.DrawLine(p, this.linePoint1, this.linePoint2);
imageGraphics.Dispose();
}

Graphics g = e.Graphics;
g.DrawImageUnscaled(this.lastImage,0,0);
}
This second way is almost always the better way to go.

One last note: you're going to notice the screen flickering if you're
doing any kind of drawing. To alleviate this, look up the SetStyle
method, and use something like:

this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

in your own initialization code.
Tomomichi Amano wrote:
Hello

How can I delete (clear) lines that were made useing Graphics.DrawLine() ?
Thanks in advance! Have a nice day!

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
6
by: Poppy | last post by:
I use the following code to append a line of text to a text file : Dim myFILENAME As String = Server.MapPath("/ABACUS/cvdocs/" & fileName) Dim objStreamWriter As StreamWriter objStreamWriter =...
5
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
15
by: batman | last post by:
i have a text file that is like: date = OCT0606 asdf sdaf asdfasdgsdgh asdfsdfasdg START-OF-DATA asdfasdfg asdfgdfgsfg
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.