Skip to content

Instantly share code, notes, and snippets.

@rebornix
Created April 24, 2015 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebornix/5dfe79964630b171ed70 to your computer and use it in GitHub Desktop.
Save rebornix/5dfe79964630b171ed70 to your computer and use it in GitHub Desktop.
Draw badge(png)
public Bitmap Draw(Color subjectCol, string subjectStr, Color statusCol, string statusStr) {
var fullHeight = 0;
var fullWidth = 0;
var padding = 8;
var shadowOffset = 5;
var font = new Font("Segoe UI", 100, GraphicsUnit.Pixel);
var maxWidth = (subjectStr.Length + statusStr.Length) * 100 + 4 * padding;
Bitmap bitmap = new Bitmap(maxWidth, 200);
using (Graphics g = Graphics.FromImage(bitmap))
{
// Set modes
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Paint subject background
g.Clear(subjectCol);
// Draw subject string
drawStringWithShadow(g, subjectStr, font, padding, 0, shadowOffset);
// Paint status background
var size = g.MeasureString(subjectStr, font);
Rectangle rect = new Rectangle((int)size.Width + 2 * padding, 0, maxWidth - (int)size.Width - 2 * padding, 200);
g.FillRectangle(new SolidBrush(statusCol), rect);
// Draw status string
drawStringWithShadow(g, statusStr, font, size.Width + 3 * padding, 0, shadowOffset);
var statusSize = g.MeasureString(statusStr, font);
fullWidth = (int)(statusSize.Width + size.Width + 4 * padding);
fullHeight = (int)size.Height + 2;
}
// Crop
var cropArea = new Rectangle(0, 0, fullWidth, fullHeight);
Bitmap bmpCrop = bitmap.Clone(cropArea, bitmap.PixelFormat);
// Rounded Corner
var CornerRadius = 40;
Bitmap RoundedImage = new Bitmap(bmpCrop.Width, bmpCrop.Height);
using (Graphics g = Graphics.FromImage(RoundedImage))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
Brush brush = new TextureBrush(bmpCrop);
GraphicsPath gp = new GraphicsPath();
gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
g.FillPath(brush, gp);
}
return RoundedImage;
}
private void drawStringWithShadow(Graphics g, string text, Font font, float x, float y, float shadowOffset)
{
g.DrawString(text, font, new SolidBrush(ColorScheme.Shadow), x + shadowOffset, y + shadowOffset);
g.DrawString(text, font, Brushes.White, x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment