473,769 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#) How to keep drawing graphics by GDI

I have some quiestion...

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

public update()
{
g = this.CreateGrap hics();
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 9307
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_WINDOWPOSCHA NGED
{
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...@g mail.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_WINDOWPOSCHA NGED
{
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()
{
InitializeCompo nent();
x = 10;
y = 10;
}

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

private void UserControl1_Pa int(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()
{
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
userControl11.u pdate();
}
}
}

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,rect angles and etc... on the from
So I code like this..

public update()
{
g = this.CreateGrap hics();
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.Collecti ons.Generic;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows. Forms;

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

List<Rectangle_ MyRectangleList = new List<Rectangle> ();

public UserControl1()
{
InitializeCompo nent();

this.Paint+=new PaintEventHandl er(UserControl1 _Paint);
x = 10;
y = 10;

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

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

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

Pen p = new Pen(Color.Black , 3);
g.DrawRectangle s(p, _MyRectangleLis t.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.goo glegroups.com.. .
On 6 8 , 5 49 , eusebiu <MarcuEuse...@g mail.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_WINDOWPOSCHA NGED
{
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()
{
InitializeCompo nent();
x = 10;
y = 10;
}

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

private void UserControl1_Pa int(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()
{
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
userControl11.u pdate();
}
}
}

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**********@n othinks.comwrot e in message
news:%2******** ********@TK2MSF TNGP04.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.goo glegroups.com.. .
>On 6 8 , 5 49 , eusebiu <MarcuEuse...@g mail.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_WINDOWPOSCHA NGED
{
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()
{
InitializeCompo nent();
x = 10;
y = 10;
}

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

private void UserControl1_Pa int(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()
{
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
userControl11.u pdate();
}
}
}

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.nos pamwrote:
>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...@nn owslpianmk.comw rote:
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

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

Similar topics

9
2411
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 an exception to be thrown): This one works but it's not the results I want. private void CreateImage1() {
1
9577
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 object for which there is only two constructors System.Drawing.Graphics.FromHdc
4
12344
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 (centered in the label) on this label. My font on this label is Courier new - hence fixed width character cells.
1
3235
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 get the best performance for the utility class, its members, as well as the System.Drawing.Graphics object, are static:
5
3867
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
3348
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 referenced, custom dll. I call a PrintImage routine in the dll and pass it only the the Picturebox.hdc from the main form. The dll's print routine draws to the hdc and it shows up in the picturebox perfectly. I would like to do a similar thing...
2
2691
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 angle, but this rotates the whole drawing of the stars but what I realy want is for the stars to rotate round it's center point, is this possible? I hope I was clear enough in my explanation. Thanks in advance
0
3558
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 menu, SigDraw. Here is the codes:- using System; using System.Drawing; using System.Collections;
2
3105
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 stuck. I'm trying to draw a bitmap on my main form and then to draw a second bitmap, as if it were a sprite (e.g., a "unit" in a wargame), on top of the first. The main form renders fine; I handle the Paint event by creating a temporary Graphics...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.