473,396 Members | 1,693 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,396 software developers and data experts.

C#) How to keep drawing graphics by GDI

I have some quiestion...

I want to draw line,point,rectangles and etc... on the from
So I code like this..

public update()
{
g = this.CreateGraphics();
g.FillRectangle(Brushes.White, x1, y1, x2, y2);
}

But the rectangle is removed after window update (minimizer or overrap
by other window)
How can I keep my drawing??

Someone recommand to draw somethins in paint event but I have
question...

My question is... do I need to keep all draw information at some class
or variable to use by paint event???
For example..
If I draw 10 rectangles by using x1,x2, y1,y2, does paint event need
all 10 datas when redraw??

What I want to do is ...
1. A class send position data to B class
2. when B class receive data, B class draw on form by GDI
3. A glass update position data (goto 1)

looping like 1-2-3- 1-2-3- 1-2-3 about 1000 more times

in this case do I also need to draw something at paint event?
and does B class need to keep all data to use at redraw time??

please help me..

Jun 8 '07 #1
11 9248
To get redraw the rectangles or whatever your doing, you have to call
Invalidate() method. For minimizing or overrap... use the bellow
method.

protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x47)//WM_WINDOWPOSCHANGED
{
this.Invalidate();
}

base.DefWndProc(ref m);
}
As you've heard, you should paint on the Paint event. ( you'll have
the graphics into PaintEventArgs e properties). And maybe instead of a
form you should use a UserControl... :)\
Hope this helps

Jun 8 '07 #2
On 6 8 , 5 49 , eusebiu <MarcuEuse...@gmail.comwrote:
To get redraw the rectangles or whatever your doing, you have to call
Invalidate() method. For minimizing or overrap... use the bellow
method.

protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x47)//WM_WINDOWPOSCHANGED
{
this.Invalidate();
}

base.DefWndProc(ref m);
}

As you've heard, you should paint on the Paint event. ( you'll have
the graphics into PaintEventArgs e properties). And maybe instead of a
form you should use a UserControl... :)\
Hope this helps
Thanks for your reply..
Now I'm using UserControl to draw something...
but....
What I really really wonder is.... look at the below code..

namespace testWindow
{
public partial class UserControl1 : UserControl
{
public int x, y;
public UserControl1()
{
InitializeComponent();
x = 10;
y = 10;
}

public void update()
{
x = x + 2;
y = y + 2;
Invalidate();
}

private void UserControl1_Paint(object sender, PaintEventArgs
e)
{
Graphics g = e.Graphics;

Pen p = new Pen(Color.Black, 3);
g.DrawRectangle(p, x, y, x, y);
}
}
}

namespace testWindow
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
userControl11.update();
}
}
}

according to above code...
if user click button on form, user control draw rectangle in
10,10,10,10
if user click more more... then rectangle is change size (+2, +2,
+2....)
at this time, user control is draw only last one because Paint event
draw only one rectangles..

This is not what I wants..
I need all rectangles, so I draw on other function not paint event.
If user click button 4 times, the function draw 4 rectangles... that
good for me.
But.. the problem is that when window is updated (size change,
minimizer and etc...) the drawing is not all recovered

So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..

Is there any good idea or solution?



Jun 8 '07 #3
Why bother with class B, since class A has its position data, and presumably
knows what to draw, give it a Draw routine that takes a Graphics object as an
argument and draws what is required at the correct position. Make a
collection of the class A objects and in the forms Paint event handler
iterate through the collection calling the Draw routines and passing the
forms Graphics object to draw on.

"ct*****@gmail.com" wrote:
I have some quiestion...

I want to draw line,point,rectangles and etc... on the from
So I code like this..

public update()
{
g = this.CreateGraphics();
g.FillRectangle(Brushes.White, x1, y1, x2, y2);
}

But the rectangle is removed after window update (minimizer or overrap
by other window)
How can I keep my drawing??

Someone recommand to draw somethins in paint event but I have
question...

My question is... do I need to keep all draw information at some class
or variable to use by paint event???
For example..
If I draw 10 rectangles by using x1,x2, y1,y2, does paint event need
all 10 datas when redraw??

What I want to do is ...
1. A class send position data to B class
2. when B class receive data, B class draw on form by GDI
3. A glass update position data (goto 1)

looping like 1-2-3- 1-2-3- 1-2-3 about 1000 more times

in this case do I also need to draw something at paint event?
and does B class need to keep all data to use at redraw time??

please help me..

Jun 8 '07 #4
the usercontrol :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class UserControl1 : UserControl
{
public int x, y;

List<Rectangle_MyRectangleList = new List<Rectangle>();

public UserControl1()
{
InitializeComponent();

this.Paint+=new PaintEventHandler(UserControl1_Paint);
x = 10;
y = 10;

Rectangle r = new Rectangle(x, y, x, y);
this._MyRectangleList.Add(r);
}
public void update()
{
x = x + 2;
y = y + 2;

Rectangle r = new Rectangle(x, y, x, y);
this._MyRectangleList.Add(r);
this.Invalidate();
}

private void UserControl1_Paint(object sender, PaintEventArgs
e)
{
Graphics g = e.Graphics;

Pen p = new Pen(Color.Black, 3);
g.DrawRectangles(p, _MyRectangleList.ToArray());
}
}
}

the form is unchanged.

Jun 8 '07 #5
So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..
Use an in-memory Bitmap in the Control. When you call the Update method,
draw on the internal Bitmap. When the Paint event handler is called, draw
the Bitmap to the Graphics context.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<ct*****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
On 6 8 , 5 49 , eusebiu <MarcuEuse...@gmail.comwrote:
>To get redraw the rectangles or whatever your doing, you have to call
Invalidate() method. For minimizing or overrap... use the bellow
method.

protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x47)//WM_WINDOWPOSCHANGED
{
this.Invalidate();
}

base.DefWndProc(ref m);
}

As you've heard, you should paint on the Paint event. ( you'll have
the graphics into PaintEventArgs e properties). And maybe instead of a
form you should use a UserControl... :)\
Hope this helps

Thanks for your reply..
Now I'm using UserControl to draw something...
but....
What I really really wonder is.... look at the below code..

namespace testWindow
{
public partial class UserControl1 : UserControl
{
public int x, y;
public UserControl1()
{
InitializeComponent();
x = 10;
y = 10;
}

public void update()
{
x = x + 2;
y = y + 2;
Invalidate();
}

private void UserControl1_Paint(object sender, PaintEventArgs
e)
{
Graphics g = e.Graphics;

Pen p = new Pen(Color.Black, 3);
g.DrawRectangle(p, x, y, x, y);
}
}
}

namespace testWindow
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
userControl11.update();
}
}
}

according to above code...
if user click button on form, user control draw rectangle in
10,10,10,10
if user click more more... then rectangle is change size (+2, +2,
+2....)
at this time, user control is draw only last one because Paint event
draw only one rectangles..

This is not what I wants..
I need all rectangles, so I draw on other function not paint event.
If user click button 4 times, the function draw 4 rectangles... that
good for me.
But.. the problem is that when window is updated (size change,
minimizer and etc...) the drawing is not all recovered

So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..

Is there any good idea or solution?



Jun 8 '07 #6

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..

Use an in-memory Bitmap in the Control. When you call the Update method,
draw on the internal Bitmap. When the Paint event handler is called, draw
the Bitmap to the Graphics context.
Or just enable double-buffering, which does all the above for you, saving
your painting in memory and restoring the screen as needed.
>
--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

<ct*****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>On 6 8 , 5 49 , eusebiu <MarcuEuse...@gmail.comwrote:
>>To get redraw the rectangles or whatever your doing, you have to call
Invalidate() method. For minimizing or overrap... use the bellow
method.

protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x47)//WM_WINDOWPOSCHANGED
{
this.Invalidate();
}

base.DefWndProc(ref m);
}

As you've heard, you should paint on the Paint event. ( you'll have
the graphics into PaintEventArgs e properties). And maybe instead of a
form you should use a UserControl... :)\
Hope this helps

Thanks for your reply..
Now I'm using UserControl to draw something...
but....
What I really really wonder is.... look at the below code..

namespace testWindow
{
public partial class UserControl1 : UserControl
{
public int x, y;
public UserControl1()
{
InitializeComponent();
x = 10;
y = 10;
}

public void update()
{
x = x + 2;
y = y + 2;
Invalidate();
}

private void UserControl1_Paint(object sender, PaintEventArgs
e)
{
Graphics g = e.Graphics;

Pen p = new Pen(Color.Black, 3);
g.DrawRectangle(p, x, y, x, y);
}
}
}

namespace testWindow
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
userControl11.update();
}
}
}

according to above code...
if user click button on form, user control draw rectangle in
10,10,10,10
if user click more more... then rectangle is change size (+2, +2,
+2....)
at this time, user control is draw only last one because Paint event
draw only one rectangles..

This is not what I wants..
I need all rectangles, so I draw on other function not paint event.
If user click button 4 times, the function draw 4 rectangles... that
good for me.
But.. the problem is that when window is updated (size change,
minimizer and etc...) the drawing is not all recovered

So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..

Is there any good idea or solution?




Jun 8 '07 #7
On Fri, 08 Jun 2007 07:32:14 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
>Use an in-memory Bitmap in the Control. When you call the Update method,
draw on the internal Bitmap. When the Paint event handler is called,
draw
the Bitmap to the Graphics context.

Or just enable double-buffering, which does all the above for you, saving
your painting in memory and restoring the screen as needed.
Not really. With double-buffering, one is required to always be ready to
redraw the entire image at any moment. It's just like handling the Paint
event, except that the drawing is done off-screen first.

The OP wants to be able to draw and then just forget about everything he's
drawn, but keep the image that resulted. The basic idea behind Kevin's
suggestion is really the only way to accomplish this, because Windows
won't remember the graphics for you implicitly.

There are variations on the theme: drawing to a Metafile instead of a
Bitmap; using the Bitmap as the background image for the Form, or in a
PictureBox control; etc. But they all involve the shared general idea of
maintaining a description of the image itself somehow, and using *that* to
redraw the form or control when needed.

Note to the OP: no matter what, at some level you need to handle the Paint
event. You can do this by attaching your Image (Bitmap or Metafile) to
something that will draw for you, or you can draw the Image explicitly as
Kevin suggests. But the basic behavior is fundamental to Windows, and you
can't just draw something to a window (form) and have it stick around.
Windows just doesn't work that way.

Pete
Jun 8 '07 #8
On Fri, 08 Jun 2007 02:49:05 -0700, <ct*****@gmail.comwrote:
[...]
So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..
I do question your statement that "the drawing data is too much to keep in
my case". Most Windows applications do just that: keep all the drawing
data. In many cases, they are dealing with a very complex document,
whether text or graphics or some more abstract data that is reprented
on-screen by text or graphics (and admittedly, "text" is really just a
special-case of "graphics"). Every time the window needs to be redrawn,
they go through all the relevant data and redraw it.

It works fine. Now and then, an application will need to do some caching
of the results to improve performance, but this is generally limited to
specific aspects. And even in that case, the data required to draw
everything from scratch is generally maintained in memory; the caching is
with respect to the execution of the drawing commands, rather than
throwing out the source data altogether.

Pete
Jun 8 '07 #9
On 6 9 , 2 27 , "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Fri, 08 Jun 2007 02:49:05 -0700, <cty0...@gmail.comwrote:
[...]
So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..

I do question your statement that "the drawing data is too much to keep in
my case". Most Windows applications do just that: keep all the drawing
data. In many cases, they are dealing with a very complex document,
whether text or graphics or some more abstract data that is reprented
on-screen by text or graphics (and admittedly, "text" is really just a
special-case of "graphics"). Every time the window needs to be redrawn,
they go through all the relevant data and redraw it.

It works fine. Now and then, an application will need to do some caching
of the results to improve performance, but this is generally limited to
specific aspects. And even in that case, the data required to draw
everything from scratch is generally maintained in memory; the caching is
with respect to the execution of the drawing commands, rather than
throwing out the source data altogether.

Pete
I read all you guys reply (Thanks..)
But I still dont know how to maintain something drawed.

What I'm doing is realtime updating which is updated in a unit of
every 0.5~1 secs and the updating is a lot...
It's possible to keep a lot drawing information in a array, but i
don't know performance.. (Actually I do not want to keep the data..)
After update something, I want to remove the information in every
class..
Somebody tell me the OS system or Video card remember drawing
information in a cache or somewhere, So I do not need to keep that but
I don't know how to do it...
Does any body show me the short sample code..
Please help me.... . ;;;;


Jun 11 '07 #10
On Sun, 10 Jun 2007 17:43:15 -0700, <ct*****@gmail.comwrote:
I read all you guys reply (Thanks..)
But I still dont know how to maintain something drawed.
I'm not clear on why not, as it seems to me that the question was answered
correctly and more than once.

That said...
What I'm doing is realtime updating which is updated in a unit of
every 0.5~1 secs and the updating is a lot...
Updating every half second to second is child's play for a modern
computer, even when including a _lot_ of information in the update.
It's possible to keep a lot drawing information in a array, but i
don't know performance.. (Actually I do not want to keep the data..)
If you do not want to keep the data, then why do you want to keep
displaying it?

Something to keep in mind: if it is taking you so long to draw everything
you want from scratch, then it is likely that you are drawing more
information to the screen than any human user will be able to comprehend..

Conversely, if you have a mechanism in mind for "aging" the information
and throwing out old information, then you can apply that to stored data
as well as you can to a cached bitmap (better than, actually).
After update something, I want to remove the information in every class..
So, do that. No one said you had to save ALL of the information you ever
receive. You just have to save the information that will be important the
next time Windows asks you to redraw your form.
Somebody tell me the OS system or Video card remember drawing
information in a cache or somewhere, So I do not need to keep that but
I don't know how to do it...
Somebody tell you? If anyone told you that what you draw once is
automatically saved for you, they were wrong. That's just not part of the
standard Windows screen updating paradigm (nor of most other GUI operating
systems for that matter).
Does any body show me the short sample code.
Please help me.... . ;;;;
Here is some short code that should give you the idea:

private Bitmap _bmpCache;

void UpdatedData()
{
Graphics gfx = Graphics.FromImage(_bmpCache);

// use gfx Graphics instance to draw whatever you want to draw
}

protected override void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_bmpCache, 0, 0);
}

You need to make sure that _bmpCache is initialized to a Bitmap the
correct size for your form (or at least, the size you expect to draw to)..
You will need to clear the bitmap manually any time you think that is
appropriate, because everything you draw will just keep piling up in the
bitmap otherwise.

Hope that helps.

Pete
Jun 11 '07 #11
On Mon, 11 Jun 2007 00:40:34 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
void UpdatedData()
{
Graphics gfx = Graphics.FromImage(_bmpCache);

// use gfx Graphics instance to draw whatever you want to draw
}
And yes, I'm playing the "this is just demo code" card. You should, of
course, make sure to dispose the Graphics object once done with it, either
explicitly, or implicitly with a "using" statement.
Jun 11 '07 #12

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

Similar topics

9
by: Steve Long | last post by:
Hello, (total GDI newbie) I'm having trouble drawing just a simple line to display in a picturebox. I just want a straight, dotdash line. I have two methods, one works and one doesn't (it cause...
1
by: Paul Hoad | last post by:
I'm trying to use MeasureString() to determine the length in pixels of a string However to do I need a System.Drawing.Graphics object to do this I need to create a System.Drawing.Graphics...
4
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
5
by: Arthur Hsu | last post by:
Hello, I have an ImageButton that refers to an external image. How can I keep that image's aspect ratio when I set the ImageButton's size to 120x120? TIA, Arthur
13
by: Metallicraft | last post by:
I have a vb6 application. On the main form is a picture box with one or two images and several pieces of text displayed in it. These are created on the fly using gdi32 routines that are all in a...
2
by: Peter Proost | last post by:
Hi group, I got the following piece of code which draws a square with stars round it, now I want the stars to rotate round the square, I can do this with the mx.rotate and a timer and an...
0
by: Hasim AH | last post by:
Hi .. Just getting interested to learn C# and needs help. I want to write C# application so that the program will execute and draw graphics when the user select the drawing menu from the main...
2
by: Carl | last post by:
I'm new to C#, and I have only limited programming experience. I've been doing the video tutorials at MS's website, and they're very helpful, but I decided to experiment with GDI+ and have gotten...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.