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

graphics help

I need some help. I have an app that talks to a sonar board via the
serial port. In my sp_DataReceived I gather all the data and put it
into arrays. Then I want to draw the data in a pictureBox. I wrote a
function called Draw_sonar2 that does all the drawing. It usually
works but sometimes crashes saying the graphics object is already in
use. After digging I saw that I should be doing the Drawing in the
PB.paint, not a seperate function. My problem is how to move the code
from Draw_sonar2 to pb.paint.

Here are my current functions
private void DrawSonar2()
{
Graphics objGraphics;
objGraphics = Graphics.FromImage(m_objDrawingSurface);
objGraphics.Clear(SystemColors.Control);
int startX, startY, endX, endY;
int angle;
startX = m_objDrawingSurface.Width / 2;
startY = m_objDrawingSurface.Height - robotDiameter/2;

for (int x = 0; x <= 9; x++)
{
int Angle = SA.sensorAngle[x];
int BeamWidth = SA.sensorBeamWidth[x];
int Range = SA.Range[x]*int.Parse(textBox_scale.Text);
if (Range int.Parse(textBox_maxRange.Text) *
int.Parse(textBox_scale.Text))
Range = int.Parse(textBox_maxRange.Text) *
int.Parse(textBox_scale.Text);
for (angle = (Angle - (BeamWidth / 2)); angle < (Angle
+ (BeamWidth / 2)); angle += 2)
{
endX = startX + (int)(Range * Math.Cos(angle *
6.28 / 360));
endY = startY + (int)(Range * Math.Sin(angle *
6.28 / 360));
objGraphics.DrawLine(Pens.Green, startX, startY,
endX, endY);

}
}

objGraphics.Dispose();
PB.Invalidate();
}
private void PB_Paint(object sender, PaintEventArgs e)
{

/////////////////////////////////
Graphics objGraphics;
objGraphics = e.Graphics;
objGraphics.Clear(SystemColors.Control);

objGraphics.DrawImage(m_objDrawingSurface, 0, 0,
m_objDrawingSurface.Width, m_objDrawingSurface.Height);
// objGraphics.Dispose();
}
if I change the line in sp_DataReceived from
DrawSonar2();
to
PB.Invalidate();

And then move the code into PB_paint like this

private void PB_Paint(object sender, PaintEventArgs e)
{
Graphics objGraphics;
objGraphics = Graphics.FromImage(m_objDrawingSurface);
objGraphics.Clear(SystemColors.Control);
// Rectangle rectBounds;
int startX, startY, endX, endY;
int angle;
startX = m_objDrawingSurface.Width / 2;
startY = m_objDrawingSurface.Height - robotDiameter / 2;

objGraphics = Graphics.FromImage(m_objDrawingSurface);
for (int x = 0; x <= 9; x++)
{
int Angle = SA.sensorAngle[x];
int BeamWidth = SA.sensorBeamWidth[x];
int Range = SA.Range[x] *
int.Parse(textBox_scale.Text);
if (Range int.Parse(textBox_maxRange.Text) *
int.Parse(textBox_scale.Text))
Range = int.Parse(textBox_maxRange.Text) *
int.Parse(textBox_scale.Text);
for (angle = (Angle - (BeamWidth / 2)); angle < (Angle
+ (BeamWidth / 2)); angle += 2)
{
endX = startX + (int)(Range * Math.Cos(angle *
6.28 / 360));
endY = startY + (int)(Range * Math.Sin(angle *
6.28 / 360));
objGraphics.DrawLine(Pens.Green, startX, startY,
endX, endY);

}
}

objGraphics.Dispose();
}

Then nothing ever draws. What am I missing.

Is this the correct way to do it, or was I closer the first time with
Draw_sonar2?

Thanks
Ringo

Oct 1 '07 #1
1 1391
Ringo wrote:
[...]
Then nothing ever draws. What am I missing.

Is this the correct way to do it, or was I closer the first time with
Draw_sonar2?
Well, what is "m_objDrawingSurface"?

Based on the "before" code, it seems to be some sort of Image object
(Bitmap, Metafile, whatever). It seems to me that the original code was
not actually all that bad. The one obvious thing I see is that you
should not need to erase the background in the Paint handler. The
control will get a separate event to do that. (But you do need to erase
the background in your image object, of course).

But other than that, the original code is actually more efficient,
because you draw once into some cached image object, and use that to
update the control whenever it actually needs updating. This way you
only update the image object when the data changes, and otherwise if the
control needs redrawing for some reason other than that (overlapping
windows being moved, for example) all you have to do is copy the image
that's already been drawn.

However, if you really want to fix the "after" code, you're going to
need to draw to the Graphics object in the event rather than the
"m_objDrawingSurface" object. If you do it that way, you should just
get rid of the "m_objDrawingSurface" altogether. There's no need to
have it if you are going to redraw everything every time the control
needs updating. (Getting rid of that object will also eliminate the bug
you have where you get a Graphics object from the "m_objDrawingSurface"
twice :) ).

Pete
Oct 1 '07 #2

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

Similar topics

12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
14
by: Pmb | last post by:
At the moment I'm using Borland's C++ (http://www.borland.com/products/downloads/download_cbuilder.html#) I want to be able to take an array of points and plot them on the screen. Is there a way...
2
by: Tamer Abdalla via DotNetMonster.com | last post by:
Hello, everyone! I DO need some help in order to understand how to create graphics in VB.NET. I'm a little bit confused... I once knew a time when using Point & PSet was almost the only way to...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
9
by: she_prog | last post by:
Dear All, I need to save the content of a panel to a bitmap. The panel can have many child controls which also need to be saved. The problem would be solved if I could have the panel saved to a...
9
by: koschwitz | last post by:
Hi, I hope you guys can help me make this simple application work. I'm trying to create a form displaying 3 circles, which independently change colors 3 times after a random time period has...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.