protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
//For each face
foreach (KeyValuePair<int, Affdex.Face> pair in Faces)
{
Affdex.Face face = pair.Value;
var featurePoints = face.FeaturePoints;
//Calculate bounding box corners coordinates.
System.Windows.Point tl = new System.Windows.Point(featurePoints.Min(r => r.X) * XScale,
featurePoints.Min(r => r.Y) * YScale);
System.Windows.Point br = new System.Windows.Point(featurePoints.Max(r => r.X) * XScale,
featurePoints.Max(r => r.Y) * YScale);
System.Windows.Point bl = new System.Windows.Point(tl.X, br.Y);
//Draw Points
if (DrawPoints)
{
foreach (var point in featurePoints)
{
dc.DrawEllipse(pointBrush, null, new System.Windows.Point(point.X * XScale, point.Y * YScale), fpRadius, fpRadius);
}
//Draw BoundingBox
dc.DrawRectangle(null, boundingPen, new System.Windows.Rect(tl, br));
}
//Draw Metrics
if (DrawMetrics)
{
double padding = (bl.Y - tl.Y) / MetricNames.Count;
double startY = tl.Y - padding;
foreach (string metric in MetricNames)
{
double width = maxTxtWidth;
double height = maxTxtHeight;
float value = -1;
PropertyInfo info;
if ((info = face.Expressions.GetType().GetProperty(NameMappings(metric))) != null)
{
value = (float)info.GetValue(face.Expressions, null);
}
else if ((info = face.Emotions.GetType().GetProperty(NameMappings(metric))) != null)
{
value = (float)info.GetValue(face.Emotions, null);
}
SolidColorBrush metricBrush = value > 0 ? pozMetricBrush : negMetricBrush;
value = Math.Abs(value);
SolidColorBrush txtBrush = value > 1 ? emojiBrush : boundingBrush;
double x = tl.X - width - margin;
double y = startY += padding;
double valBarWidth = width * (value / 100);
if (value > 1) dc.DrawRectangle(null, boundingPen, new System.Windows.Rect(x, y, width, height));
dc.DrawRectangle(metricBrush, null, new System.Windows.Rect(x, y, valBarWidth, height));
FormattedText metricFTScaled = new FormattedText((String)upperConverter.Convert(metric, null, null, null),
System.Globalization.CultureInfo.CurrentCulture,
System.Windows.FlowDirection.LeftToRight,
metricTypeFace, metricFontSize * width / maxTxtWidth, txtBrush);
dc.DrawText(metricFTScaled, new System.Windows.Point(x, y));
}
}
//Draw Emoji
if (DrawEmojis)
{
if (face.Emojis.dominantEmoji != Affdex.Emoji.Unknown)
{
BitmapImage img = emojiImages[face.Emojis.dominantEmoji];
double imgRatio = ((br.Y - tl.Y) * 0.3) / img.Width;
System.Windows.Point tr = new System.Windows.Point(br.X + margin, tl.Y);
dc.DrawImage(img, new System.Windows.Rect(tr.X, tr.Y, img.Width * imgRatio, img.Height * imgRatio));
}
}
//Draw Appearance metrics
if (DrawAppearance)
{
BitmapImage img = appImgs[ConcatInt((int)face.Appearance.Gender, (int)face.Appearance.Glasses)];
double imgRatio = ((br.Y - tl.Y) * 0.3) / img.Width;
double imgH = img.Height * imgRatio;
dc.DrawImage(img, new System.Windows.Rect(br.X + margin, br.Y - imgH, img.Width * imgRatio, imgH));
}
}
//base.OnRender(dc);
}