C#
private void S57Control_PaintOverlaysEvent(object sender, Graphics gr)
{
// Initialize usvTrail if not already done
if (usvTrail == null)
{
usvTrail = new List>();
}
// USV Position
if (DataRead._USVGeoCoordinate != null)
{
usvPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = DataRead._USVGeoCoordinate.Latitude,
Lon = DataRead._USVGeoCoordinate.Longitude
});
//var usvGeoPoint = m_draw.GeoPix.GeoPointFromPixelPoint(usvPixelPoint);
if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView)
{
var currentusvGeocordinate = new GeoCoordinate(DataRead._USVGeoCoordinate.Latitude, DataRead._USVGeoCoordinate.Longitude);
if (usvTrail.Count > 0)
{
lastPosition = usvTrail[usvTrail.Count - 1].Item1;
lastPositionAngle = usvTrail[usvTrail.Count - 1].Item2;
float distance = DataRead.DistanceBetweenPoints(lastPosition, currentusvGeocordinate);
if (distance >= 10)
{
usvTrail.Add(new Tuple(currentusvGeocordinate, DataRead.USV_Angle));
}
}
else
{
usvTrail.Add(new Tuple(currentusvGeocordinate, DataRead.USV_Angle));
}
if (DataRead._USVGeoCoordinate != null)
{
//Draw the USV at the current position
usvRectangle = new Rectangle(Convert.ToInt32(usvPixelPoint.X - 14),
Convert.ToInt32(usvPixelPoint.Y - 14), 30, 30);
}
//Draw Circle around USV
Color transparentOrange = Color.FromArgb(50, Color.DarkOrange);
var usvPixelPerMeters = m_draw.PixelSizeMetres * m_draw.GeoPix.DisplayScale;
int radius = (int)(600 / (2 * Math.PI) / usvPixelPerMeters);
using (Brush brush = new SolidBrush(transparentOrange))
{
gr.FillEllipse(brush, usvPixelPoint.X - radius, usvPixelPoint.Y - radius, radius * 2, radius * 2);
}
Font distFont = new Font("Calibri", 11,FontStyle.Bold);
Brush textBrush = Brushes.Black;
PointF topPoint = new PointF(usvPixelPoint.X - 30, usvPixelPoint.Y - radius - 15);
gr.DrawString("Safe Zone", distFont, textBrush, topPoint);
if (_rotatechartbuttonChecked)
{
m_draw.SetDirectionOfUp(DataRead.USV_Angle, _defaultPixelPoint);
//Draw USVImage
Bitmap rotatedImage = RotateImage(_usvImage, 0);
gr.DrawImage(rotatedImage, usvRectangle);
gr.SmoothingMode = SmoothingMode.AntiAlias;
//Draw trail
foreach (var point in usvTrail)
{
var trailPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = point.Item1.Latitude,
Lon = point.Item1.Longitude
});
trailRectangle = new Rectangle(Convert.ToInt32(trailPixelPoint.X), Convert.ToInt32(trailPixelPoint.Y), 10, 10);
RotateRectangle(gr, trailRectangle, point.Item2 - DataRead.USV_Angle);
if (point.Item2 >= 0 && point.Item2 = 90 && point.Item2 = 180 && point.Item2 270 && point.Item2 = 0 && point.Item2 = 90 && point.Item2 = 180 && point.Item2 270 && point.Item2 >();
}
if (DataRead._AISGeoCoordinate != null)
{
aisPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = DataRead._AISGeoCoordinate.Latitude,
Lon = DataRead._AISGeoCoordinate.Longitude
});
//var usvGeoPoint = m_draw.GeoPix.GeoPointFromPixelPoint(usvPixelPoint);
if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView)
{
var currentaisGeocordinate = new GeoCoordinate(DataRead._AISGeoCoordinate.Latitude,
DataRead._AISGeoCoordinate.Longitude);
if (aisTrail.Count > 0)
{
lastaisPosition = aisTrail[aisTrail.Count - 1].Item2;
lastaisPositionAngle = aisTrail[aisTrail.Count - 1].Item1;
float aisdistance = DataRead.DistanceBetweenPoints(lastaisPosition, currentaisGeocordinate);
if (aisdistance >= 10)
{
aisTrail.Add(new Tuple(DataRead.AIS_Angle,currentaisGeocordinate));
}
}
else
{
aisTrail.Add(new Tuple(DataRead.AIS_Angle, currentaisGeocordinate));
}
if (DataRead._AISGeoCoordinate != null)
{
//Draw the USV at the current position
aisRectangle = new Rectangle(Convert.ToInt32(aisPixelPoint.X - 14),
Convert.ToInt32(aisPixelPoint.Y - 14), 30, 30);
}
//Draw AIS Image
Bitmap rotatedAISImage = RotateImage(_aisImage, DataRead.AIS_Angle);
gr.DrawImage(rotatedAISImage, aisRectangle);
gr.SmoothingMode = SmoothingMode.AntiAlias;
// Draw trail
foreach (var aispoint in aisTrail)
{
var trailaisPixelPoint = m_draw.GeoPix.Point(new GeoPoint
{
Lat = aispoint.Item2.Latitude,
Lon = aispoint.Item2.Longitude
});
trailaisRectangle = new Rectangle(Convert.ToInt32(trailaisPixelPoint.X),
Convert.ToInt32(trailaisPixelPoint.Y), 10, 10);
RotateRectangle(gr, trailaisRectangle, aispoint.Item1);
if (aispoint.Item1 >= 0 && aispoint.Item1 = 90 && aispoint.Item1 = 180 && aispoint.Item1 270 && aispoint.Item1 < 360)
{
gr.DrawRectangle(Pens.Black, trailaisRectangle.X, trailaisRectangle.Y, 3, 10);
}
}
gr.SmoothingMode = SmoothingMode.Default;
}
}
}here in this code i am drawing two images usv and ais. but when i am changing the angle of usv and speed of usv its effecting the drawing of ais image so i dont want call drawais(gr) inside that paintoverlay event any other way i can call that method somewhere?

NaveenRank #6,081 · 277 reputationPosted Jul 15, 2024, 4:31 AM EDT
Hi Chetan,
Thank you for your answer,but when i tried this its not drawing AIS image at all.actually i tried this way before also but its not drawing
Chetan SanghaniRank #244 · 10.2K reputationPosted Jul 13, 2024, 2:20 AM EDT
To separate the drawing of the USV and AIS images so that changes to the USV do not affect the AIS drawing, you can handle the drawing of the AIS in a different event or method. Here are the steps to achieve this:
DrawAis(gr)call fromS57Control_PaintOverlaysEvent.Step-by-Step Implementation
Remove
DrawAis(gr)call fromS57Control_PaintOverlaysEvent:private void S57Control_PaintOverlaysEvent(object sender, Graphics gr) { // Existing code for drawing USV... // Remove this line // if (windowsUIButtonPanel4.Buttons["AIS"].Properties.Checked) // { // DrawAis(gr); // } S57Control1.Invalidate(); }Create a separate method for AIS drawing:
private void DrawAis(Graphics gr) { //AIS Position if (aisTrail == null) //Initialize usvTrail if not already done { aisTrail = new List>(); } if (DataRead._AISGeoCoordinate != null) { aisPixelPoint = m_draw.GeoPix.Point(new GeoPoint { Lat = DataRead._AISGeoCoordinate.Latitude, Lon = DataRead._AISGeoCoordinate.Longitude }); if (m_draw.GeoPix.PointInView || !m_draw.GeoPix.PointInView) { var currentaisGeocordinate = new GeoCoordinate(DataRead._AISGeoCoordinate.Latitude, DataRead._AISGeoCoordinate.Longitude); if (aisTrail.Count > 0) { lastaisPosition = aisTrail[aisTrail.Count - 1].Item2; lastaisPositionAngle = aisTrail[aisTrail.Count - 1].Item1; float aisdistance = DataRead.DistanceBetweenPoints(lastaisPosition, currentaisGeocordinate); if (aisdistance >= 10) { aisTrail.Add(new Tuple(DataRead.AIS_Angle, currentaisGeocordinate)); } } else { aisTrail.Add(new Tuple(DataRead.AIS_Angle, currentaisGeocordinate)); } if (DataRead._AISGeoCoordinate != null) { //Draw the AIS at the current position aisRectangle = new Rectangle(Convert.ToInt32(aisPixelPoint.X - 14), Convert.ToInt32(aisPixelPoint.Y - 14), 30, 30); } //Draw AIS Image Bitmap rotatedAISImage = RotateImage(_aisImage, DataRead.AIS_Angle); gr.DrawImage(rotatedAISImage, aisRectangle); gr.SmoothingMode = SmoothingMode.AntiAlias; // Draw trail foreach (var aispoint in aisTrail) { var trailaisPixelPoint = m_draw.GeoPix.Point(new GeoPoint { Lat = aispoint.Item2.Latitude, Lon = aispoint.Item2.Longitude }); trailaisRectangle = new Rectangle(Convert.ToInt32(trailaisPixelPoint.X), Convert.ToInt32(trailaisPixelPoint.Y), 10, 10); RotateRectangle(gr, trailaisRectangle, aispoint.Item1); if (aispoint.Item1 >= 0 && aispoint.Item1 = 90 && aispoint.Item1 = 180 && aispoint.Item1 270 && aispoint.Item1 < 360) { gr.DrawRectangle(Pens.Black, trailaisRectangle.X, trailaisRectangle.Y, 3, 10); } } gr.SmoothingMode = SmoothingMode.Default; } } }Create a separate event or method to call
DrawAis:You can create a new Paint event for AIS or call
DrawAisfrom another appropriate location in your code. For instance, you can use the form's Paint event or create a timer to periodically update the AIS drawing.Example: Using a Timer to Update AIS Drawing
Add a Timer to your form:
private System.Windows.Forms.Timer aisTimer;Initialize and start the Timer in your form's constructor or load event:
public YourForm() { InitializeComponent(); aisTimer = new System.Windows.Forms.Timer(); aisTimer.Interval = 1000; // Set the interval (in milliseconds) aisTimer.Tick += AisTimer_Tick; aisTimer.Start(); } private void AisTimer_Tick(object sender, EventArgs e) { // Invalidate the control to trigger a repaint S57Control1.Invalidate(); }Handle the control's Paint event and call
DrawAis:private void S57Control1_Paint(object sender, PaintEventArgs e) { Graphics gr = e.Graphics; // Call DrawAis to draw AIS-related graphics if (windowsUIButtonPanel4.Buttons["AIS"].Properties.Checked) { DrawAis(gr); } }Final Step
Make sure to bind the Paint event of
S57Control1to the new event handler.By following these steps, you separate the drawing logic for USV and AIS, ensuring that changes to USV do not affect AIS drawing. The use of a timer ensures that AIS information is updated and redrawn at regular intervals.