473,805 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FromIm age(m_objDrawin gSurface);
objGraphics.Cle ar(SystemColors .Control);
int startX, startY, endX, endY;
int angle;
startX = m_objDrawingSur face.Width / 2;
startY = m_objDrawingSur face.Height - robotDiameter/2;

for (int x = 0; x <= 9; x++)
{
int Angle = SA.sensorAngle[x];
int BeamWidth = SA.sensorBeamWi dth[x];
int Range = SA.Range[x]*int.Parse(text Box_scale.Text) ;
if (Range int.Parse(textB ox_maxRange.Tex t) *
int.Parse(textB ox_scale.Text))
Range = int.Parse(textB ox_maxRange.Tex t) *
int.Parse(textB ox_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.Dra wLine(Pens.Gree n, startX, startY,
endX, endY);

}
}

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

/////////////////////////////////
Graphics objGraphics;
objGraphics = e.Graphics;
objGraphics.Cle ar(SystemColors .Control);

objGraphics.Dra wImage(m_objDra wingSurface, 0, 0,
m_objDrawingSur face.Width, m_objDrawingSur face.Height);
// objGraphics.Dis pose();
}
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.FromIm age(m_objDrawin gSurface);
objGraphics.Cle ar(SystemColors .Control);
// Rectangle rectBounds;
int startX, startY, endX, endY;
int angle;
startX = m_objDrawingSur face.Width / 2;
startY = m_objDrawingSur face.Height - robotDiameter / 2;

objGraphics = Graphics.FromIm age(m_objDrawin gSurface);
for (int x = 0; x <= 9; x++)
{
int Angle = SA.sensorAngle[x];
int BeamWidth = SA.sensorBeamWi dth[x];
int Range = SA.Range[x] *
int.Parse(textB ox_scale.Text);
if (Range int.Parse(textB ox_maxRange.Tex t) *
int.Parse(textB ox_scale.Text))
Range = int.Parse(textB ox_maxRange.Tex t) *
int.Parse(textB ox_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.Dra wLine(Pens.Gree n, startX, startY,
endX, endY);

}
}

objGraphics.Dis pose();
}

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 1403
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_objDrawingSu rface"?

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_objDrawingSu rface" object. If you do it that way, you should just
get rid of the "m_objDrawingSu rface" 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_objDrawingSu rface"
twice :) ).

Pete
Oct 1 '07 #2

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

Similar topics

12
2389
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 graphics object of the form/pictureBox. Should It be better if make a graphics object from my pictureBox in load event handler of the form and store it as member variable of the form , make
14
3105
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 to do this? E.g. I want to be able to graph a function. At this point I'm not up to a level in C++ where I want to start learning Visual C++ so I don't want to go that route. Thanks
2
1679
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 make some interesting apps which could tranform images (i.e. making saturation of colours "heavy", or gradually fade to grayscale, or "erasing" a colour... and so on), while nowadays it seems quite impossible. Now that I got .NET over...
8
3333
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 froggraphic As Graphics = Graphics.FromImage(frogbitmap) froggraphic.RotateTransform(90) frogbitmap.Save(Server.MapPath("images/frog2.gif"), Imaging.ImageFormat.Gif)
7
2584
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 stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
15
1854
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
4273
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 Graphics object, which is the same as if I'd need to print it. It'd be easy using Control.DrawToBitmap, but I also need the invisible part of the panel (which is hidden because of scrolling) and DrawToBitmap just takes a screenshot.
9
5067
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 passed. I'm struggling with making the delegate/invoke thing work, as I know GUI objects aren't thread-safe. I don't quite understand the concept I'm supposed to use to modify the GUI thread-safe. Below is my form and my Circle class. Currently,...
0
10609
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...
1
10366
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
10105
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
9185
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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
5542
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...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
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.