Skip to content

Instantly share code, notes, and snippets.

@robws
Created September 1, 2021 14:17
Show Gist options
  • Select an option

  • Save robws/c822cc0cf8f078dbbe1e8502308a10cf to your computer and use it in GitHub Desktop.

Select an option

Save robws/c822cc0cf8f078dbbe1e8502308a10cf to your computer and use it in GitHub Desktop.
Balsamiq 64x64 PNG File Type Icon
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
/// <summary>
/// The {{Create-Image-BalsamiqIconWithText64x}} class.
/// Creates a 64x64 "file type" graphic with an embedded caption inside, in a transparent format suitable for Balsamiq
/// </summary>
/// <remarks>
/// <para></para>
/// </remarks>
void Main()
{
string filename_out=@"c:\docs\junkdrawer\";
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(64, 64);
Graphics g = Graphics.FromImage(bitmap);
g.Clear(Color.Transparent);
g.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
GdiHelper.CreateRoundedRectanglePath(g, new Rectangle(0, 0, 64,64), 10, new Pen(Color.Black,5));
string text1 = "ps1";
filename_out+=text1+".png";
drawCaption(g,"Roboto",20,FontStyle.Bold,text1,Brushes.Black,64,64);
bitmap.Save(filename_out, ImageFormat.Png);
g.Dispose();
System.Diagnostics.Process.Start(filename_out);
return;
}
/// <summary>
/// Draw a caption centered in an image
/// </summary>
/// <example>
/// Example text
/// <code>
///
/// </code>
/// </example>
void drawCaption(Graphics g,string FontFamily,float fontSize, System.Drawing.FontStyle style, string c, Brush bru, int width,int height)
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
GraphicsPath p = new GraphicsPath();
Font font1 = new Font(FontFamily, fontSize, style, GraphicsUnit.Point);
int fontStyle = (int)font1.Style;
float emSize = g.DpiY * font1.Size / 72;
p.AddString(c, font1.FontFamily, fontStyle, emSize, new Point(0, 0), StringFormat.GenericTypographic);
Rectangle bounds = new Rectangle(0, 0, width, height);
var area = Rectangle.Round(p.GetBounds());
var offset = new Point(bounds.Left + (bounds.Width / 2 - area.Width / 2) - area.Left, bounds.Top + (bounds.Height / 2 - area.Height / 2) - area.Top);
var translate = new Matrix();
translate.Translate(offset.X, offset.Y);
p.Transform(translate);
g.FillPath(bru, p);
}
@robws
Copy link
Copy Markdown
Author

robws commented Sep 1, 2021

Making public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment