472,993 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Drawing xor line on picture box. HELP!

Hi,
Please help me, how I can make a line drawing on PictureBox similar to
MS paint. I thinking about this: click and hold button, move mouse and
relase button.

I'm trying useing this g.DrawLine(..), but I have a lot of line.

In C++ is a parameter XORPUT, but in C# I can't find it.
Maciek
Jun 27 '08 #1
5 12736
On Wed, 07 May 2008 13:14:11 -0700, Macias <ma************@gmail.com>
wrote:
Please help me, how I can make a line drawing on PictureBox similar to
MS paint. I thinking about this: click and hold button, move mouse and
relase button.

I'm trying useing this g.DrawLine(..), but I have a lot of line.

In C++ is a parameter XORPUT, but in C# I can't find it.
..NET has essentially implicitly deprecated XOR drawing. The only drawing
modes (Graphics.CompositingMode) are "copy" and "blend". This appears to
be basically inherited from GDI+, which as near as I can tell has the same
limitation.

If you really need XOR, you'll have to use p/invoke to use GDI directly on
a DC you get from your Graphics instance. However, my experience has been
that XOR drawing isn't actually needed. The usual reason for using XOR
was to avoid having to redraw large portions of your window; rather than
invalidating and redrawing, you could just XOR a line, then XOR again to
erase it.

But computers are MUCH faster these days and with double-buffered
windows/controls the most expensive graphical operation would happen even
if you're XORing. Done properly it's usually plenty fast to invalidate an
area that's changed and draw it again from scratch.

If you have a concise-but-complete example of code now that's not
performing as you'd like while trying to draw lines without XOR, and you
post that concise-but-complete example here, then we can try to help find
ways to improve that code in a purely .NET way. It's not impossible to
delegate the drawing to GDI via p/invoke, but it's probably more trouble
than you really need to go to. You're probably better off getting advice
on "the .NET way". :)

Pete
Jun 27 '08 #2
I make drawing like this:

private void pb_MouseMove(object sender, MouseEventArgs e)
{
Point CurentCoord = new Point(e.X, e.Y);
if (md)
{

using (Graphics g = Graphics.FromImage(img))
{
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality =
CompositingQuality.HighSpeed;
Rectangle rect = this.ClientRectangle;
using (Pen p = new Pen(ColorBox.BackColor, 2))
{
g.DrawLine(p, ClickCoord, CurentCoord);
pb.Image = img;
}
}
}

but still I've a lot of lines when I moveing mouse. But I want to have
only one.
Please correct my pice of program.

Maciek

On 7 Maj, 22:58, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Wed, 07 May 2008 13:14:11 -0700, Macias <maciej.bojc...@gmail.com>
wrote:
Please help me, how I can make a line drawing on PictureBox similar to
MS paint. I thinking about this: click and hold button, move mouse and
relase button.
I'm trying useing this g.DrawLine(..), but I have a lot of line.
In C++ is a parameter XORPUT, but in C# I can't find it.

.NET has essentially implicitly deprecated XOR drawing. The only drawing
modes (Graphics.CompositingMode) are "copy" and "blend". This appears to
be basically inherited from GDI+, which as near as I can tell has the same
limitation.

If you really need XOR, you'll have to use p/invoke to use GDI directly on
a DC you get from your Graphics instance. However, my experience has been
that XOR drawing isn't actually needed. The usual reason for using XOR
was to avoid having to redraw large portions of your window; rather than
invalidating and redrawing, you could just XOR a line, then XOR again to
erase it.
But computers are MUCH faster these days and with double-buffered
windows/controls the most expensive graphical operation would happen even
if you're XORing. Done properly it's usually plenty fast to invalidate an
area that's changed and draw it again from scratch.

If you have a concise-but-complete example of code now that's not
performing as you'd like while trying to draw lines without XOR, and you
post that concise-but-complete example here, then we can try to help find
ways to improve that code in a purely .NET way. It's not impossible to
delegate the drawing to GDI via p/invoke, but it's probably more trouble
than you really need to go to. You're probably better off getting advice
on "the .NET way". :)

Pete
Jun 27 '08 #3
On Wed, 07 May 2008 14:36:31 -0700, Macias <ma************@gmail.com
wrote:
I make drawing like this:

private void pb_MouseMove(object sender, MouseEventArgs e)
{
Point CurentCoord = new Point(e.X, e.Y);
if (md)
{

using (Graphics g = Graphics.FromImage(img))
{
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality =
CompositingQuality.HighSpeed;
Rectangle rect = this.ClientRectangle;
using (Pen p = new Pen(ColorBox.BackColor, 2))
{
g.DrawLine(p, ClickCoord, CurentCoord);
pb.Image = img;
}
}
}

but still I've a lot of lines when I moveing mouse. But I want to have
only one.
Please correct my pice of program.
What is the precise reason for using a PictureBox to contain the image?
Are you sure you really need that? Or would it suffice to override
OnPaint() in your form or a custom control class? I ask, because the use
of the PictureBox is going to necessarily impose additional overhead on
the redrawing.

The basic issue you've got is that you've apparently started with a given
Image instance, and to that you want to add a line. However, since you're
adding a new line every time the mouse moves, you get a lot of lines,
instead of just the one for the most recent mouse position.

One of the simplest approaches I see based on the code you posted would be
to not draw into the original Image, but rather create a new one:

private void pb_MouseMove(object sender, MouseEventArgs e)
{
Point CurentCoord = new Point(e.X, e.Y);
if (md)
{
Image imgT = new Bitmap(img);

using (Graphics g = Graphics.FromImage(imgT))
{
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality =
CompositingQuality.HighSpeed;
Rectangle rect = this.ClientRectangle;
using (Pen p = new Pen(ColorBox.BackColor, 2))
{
g.DrawLine(p, ClickCoord, CurentCoord);
pb.Image = imgT;
}
}
}

Optionally (and most likely) in the MouseUp event you'd replace the "img"
reference with the Image currently in the PictureBox.

This is far from an ideal solution though. It can get very expensive to
be creating a new bitmap every time the mouse moves. I haven't use the
PictureBox control much; it's possible you could create one temporary
image that you repeatedly copy your original image into, and then reassign
that to the PictureBox.Image property. That would at least avoid the
constant instantiation of Bitmaps, but you'd still have a little extra
overhead every time you copy the original image into the temporary image..

It would be much better if you weren't using the PictureBox, but instead
were drawing the Image directly yourself in the appropriate place (form or
custom control). This way you could simply draw both the base Image and
then the line each time the OnPaint() method was called. You'd still
override OnMouseMove() but the only thing you'd do there is update your
line coordinates and invalidate the area of the form or control containing
the old position of the line and the new position of the line.

This way, rather than creating new bitmaps every time the mouse moves, all
you have to do is redraw the relevant area based on the current mouse
position.

Without knowing more about the specific goal you have for your program,
it's hard to offer more detailed advice than the above. Hopefully that
gets you going in the right direction.

Pete
Jun 27 '08 #4
On May 7, 3:14 pm, Macias <maciej.bojc...@gmail.comwrote:
Hi,
Please help me, how I can make a line drawing on PictureBox similar to
MS paint. I thinking about this: click and hold button, move mouse and
relase button.

I'm trying useing this g.DrawLine(..), but I have a lot of line.

In C++ is a parameter XORPUT, but in C# I can't find it.

Maciek
Have you looked at the ControlPaint.DrawReversibleLine method?
Perhaps that will do what you need.

Chris
Jun 27 '08 #5
On Thu, 08 May 2008 07:02:12 -0700, Chris Dunaway <du******@gmail.com>
wrote:
Have you looked at the ControlPaint.DrawReversibleLine method?
Perhaps that will do what you need.
Interesting...I've never seen that class before.

I'm a bit confused though; maybe you can elaborate on the purpose of that
class. All of the examples that I looked at in the class documentation
show using the class to draw directly in a non-painting event handler.
Normally this would be a major no-no, since drawing done in that context
isn't persistent.

So then I thought "well, maybe this is some special kind of drawing the
ControlPaint class internally persists somehow". But then I see the
methods take a Graphics instance just like other drawing methods, and that
seems to suggest ControlPaint's not doing anything other than adding some
new features to Graphics (in fact, I suppose they might have been
extension methods had extension methods been around when ControlPaint was
designed).

Are the examples bogus? Or is there something more to ControlPaint than
just adding some extra ways to draw to a Graphics instance?

(Yeah, I could use Reflector and look myself, but I don't have Windows
running at the moment and I figure it'd be better to ask someone who might
have more practical experience with the class anyway).

Pete
Jun 27 '08 #6

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

Similar topics

19
by: Atif | last post by:
Hello all, In my html page I want to add an image say of 800x600. Now I want that when ever I am given two coordinates on this image say (x1, y1)=(50, 100) and (x2, y2)=(200, 300), the java script...
3
by: Bob | last post by:
Hello, I am trying to draw a line across a page. The problem is that it is affecting the text subsequent text. I may be doing something wrong with the html but I am not sure. You can see the...
2
by: Anand Ganesh | last post by:
Hi All, In my application I am allowing the user to draw a line. But when the user clicks the start point and starts moving the mouse there is a series of line generated. When the mouse is up...
3
by: jack | last post by:
hi all can any one help me in this i want to draw a default picture on a picture box whenever i click on a new button. please help ...
11
by: darrel | last post by:
I'm modyfying an image upload and resizing script that we've had laying around for a long while. I'm getting a NullReferenceException error (see full error at bottom) from this line: Dim g...
3
by: Evo | last post by:
Below is my code,it passed the compile and link under TC2.0 correctly. But the line it draws is obviously wrong. I think it's somewhere I thought wrong. But I really can't find it out. Can someone...
0
by: SurajPrabhu | last post by:
Hi All, I am tying to build a ASP.net 2.0 based web page which displays a Analog Guage ( 0 to 180 degrees) with an Arrow in the middle. The application accepts the Percentage as input from a...
2
by: rkgarimella9 | last post by:
Friends, i'm facing with a severe problem while coding an windows application. the problem is that i'm using a usercontrol consisting of three labels upon which three images are placed. the problem...
6
by: Orin | last post by:
Hi! If anybody knows how to output formatted HTML code in command line with help of PHP (or other way), please help me. With best regards, Orin
10
Mas Juliza Alias
by: Mas Juliza Alias | last post by:
I have a table (MSFlexgrid1) in a form (frmCalculate) containing 2 columns of Depth and Volume data. I want to draw a line graph of Depth Vs Volume in a new form (frmGraph) by using MSChart control....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.