I have a Picture box inside a Groupbox in my form with the Picture of a radar set as the background picture. My intention is to dynamically load tiny Jpeg images within the radar area (overlaid) at runtime but I am unsure as to the best way to achieve this. All crazy ideas welcomed (but I would prefer sane easy to do ones). Thank you all.
It depends a lot on what your "radar" needs to look like, but almost certainly you'll need to implement the Paint event handler, and draw the contents of the radar display yourself. A picture box will only get you so far ("not very").
GDI+ is very easy to use to draw circles, lines, text, and images, and will give you complete control over how your display looks.
The simplest way is to load your tiny JPEGs into tiny PictureBoxes, and add them to the main PictureBox's Controls collection (i.e. place them on the PictureBox) at runtime.
Since this will probably produce flicker, the slightly more complex way is to keep the main picture and the tiny pictures in class-level Bitmap objects, and in the main PictureBox's Paint event, you copy the main picture followed by each tiny picture onto a second class-level Bitmap (named _doubleBuffer or something like that) using the DrawImage method, and then copy _doubleBuffer onto your PictureBox (also using DrawImage). Whenever you need to update your display and redraw everything, you just call the PictureBox's Invalidate method.
There are loads of examples here on SO that show how to use these methods. Good luck, it sounds fun (if you're rewriting the classic arcade game Submarine, let me know - I loved that game).
As for actual example:
// Among others
using System.Collections.Generic;
using System.Drawing;
using System.IO;
class TinyPic {
public readonly Image Picture;
public readonly Rectangle Bounds;
public TinyPic(Image picture, int x, int y) {
Picture = picture;
Bounds = new Rectangle(x, y, picture.Width, picture.Height);
}
}
class MyForm : Form {
Dictionary<String, TinyPic> tinyPics = new Dictionary<String, TinyPic>();
public MyForm(){
InitializeComponent(); // assuming Panel myRadarBox
// with your background is there somewhere;
myRadarBox.Paint += new PaintEventHandler(OnPaintRadar);
}
void OnPaintRadar(Object sender, PaintEventArgs e){
foreach(var item in tinyPics){
TinyPic tp = item.Value;
e.Graphics.DrawImageUnscaled(tp.Picture, tp.Bounds.Location);
}
}
void AddPic(String path, int x, int y){
if ( File.Exists(path) ){
var tp = new TinyPic(Image.FromFile(path), x, y);
tinyPics[path] = tp;
myRadarBox.Invalidate(tp.Bounds);
}
}
void RemovePic(String path){
TinyPic tp;
if ( tinyPics.TryGetValue(path, out tp) ){
tinyPics.Remove(path);
tp.Picture.Dispose();
myRadarBox.Invalidate(tp.Bounds);
}
}
}
This of course is very basic, assumes image source is path and doesn't take care of many intricate things, but that's the quick and dirty jist of it which you can certainly build on.
Click here to run a sample application that demonstrates the basics of how to do radar (or one way, at least). Note: this application does not do double-buffering or transparency of the tiny image.
Source code for the project is here.
anyone have any idea how to program the line and sweeping it using C#? Please email me at [email protected]. I used GDI to draw some circles and lines to create my own radar but i still cant seem to create the sweep line. Anyone has a simple way of creating it because i am new to C# thanks.