'Drawing a line on a bitmap
I made a face dector in matlab, and I am translating it into c# code, I everything is moslty done. In the main I use
System.Drawing.Bitmap b = new
System.Drawing.Bitmap("C:*Location of file on computer*");
to initilly attain the image, and in the final steps I have this code
public static void ratio(System.Drawing.Bitmap b, Dictionary<int, List<int>> map)
{
double height=0;
double width=0;
foreach (KeyValuePair<int, List<int>> place in map)
{
height = place.Value[2] - place.Value[3];
width = place.Value[0] - place.Value[1];
if( ((height/width) >= 1) && ((height/width) <= 2 ) )
draw(b, place, map);
}
}
public static void draw(System.Drawing.Bitmap bmp, KeyValuePair<int, List<int>> place, Dictionary<int, List<int>> map)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create coordinates of points that define line.
int x1 = place.Value[1]; //topleft to topright
int y1 = place.Value[3];
int x2 = place.Value[0];
int y2 = place.Value[3];
int x3 = place.Value[0]; //topright to bottomright
int y3 = place.Value[3];
int x4 = place.Value[0];
int y4 = place.Value[2];
int x5 = place.Value[0]; //bottomright to bottomleft
int y5 = place.Value[2];
int x6 = place.Value[1];
int y6 = place.Value[2];
int x7 = place.Value[1]; //bottomleft to topleft
int y7 = place.Value[2];
int x8 = place.Value[1];
int y8 = place.Value[3];
// Draw line to screen.
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x1, y1, x2, y2);
}
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x3, y3, x4, y4);
}
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x5, y5, x6, y6);
}
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x7, y7, x8, y8);
}
}
to draw the a box around the face. Ratio uses the boundaries from labels obtained form connected component labeling to find the right ratio for a human face (my numbers are just made up) map is dictionary which contains the label number, along with the xmax, xmin, ymax, and ymin as values. Everything compiles with no errors but, what I am trying to do now is to display the said image with the drawn box around the face and I am unsure of how to do that
Solution 1:[1]
Assuming that this is a Windows.Forms application you can just drop a PictureBox control onto the form from the Toolbox, set its Dock property to Fill, and set its Image property in code:
PictureBox1.Image = b;
Solution 2:[2]
In the form designer, put a PictureBox
control on the form and position it and size it however you like. You could add one programmatically, if you like (or if it's necessary).
Then, add an event handler for the form's Load
event, and in that method, apply this code:
System.Drawing.Bitmap b = new System.Drawing.Bitmap("C:*Location of file on computer*");
pictureBox1.Image = b;
Then, your drawing method can become:
public static void draw(KeyValuePair<int, List<int>> place, Dictionary<int, List<int>> map)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create coordinates of points that define line.
int x1 = place.Value[1]; //topleft to topright
int y1 = place.Value[3];
int x2 = place.Value[0];
int y2 = place.Value[3];
int x3 = place.Value[0]; //topright to bottomright
int y3 = place.Value[3];
int x4 = place.Value[0];
int y4 = place.Value[2];
int x5 = place.Value[0]; //bottomright to bottomleft
int y5 = place.Value[2];
int x6 = place.Value[1];
int y6 = place.Value[2];
int x7 = place.Value[1]; //bottomleft to topleft
int y7 = place.Value[2];
int x8 = place.Value[1];
int y8 = place.Value[3];
// Draw line to screen.
using (Graphics g = Graphics.FromHwnd(pictureBox1.Handle))
{
g.DrawLine(blackPen, x1, y1, x2, y2);
g.DrawLine(blackPen, x3, y3, x4, y4);
g.DrawLine(blackPen, x5, y5, x6, y6);
g.DrawLine(blackPen, x7, y7, x8, y8);
}
pictureBox1.Invalidate();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gareththegeek |
Solution 2 | DonBoitnott |