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