Here we draw an image and a text caption on a bitmap. We centre
horizontally text and image. The image is aligned to the top (the
default) and the caption to the bottom.
[C#]
Font font = new Font("Arial", 36, FontStyle.Regular);
int lineHeight = (int)Math.Round(font.GetHeight());
Size border = new Size(80, 80);
using (Image image = Bitmap.FromFile(Server.MapPath("rez/vladimir-tsokalo-N3moZ3Gn-gI-unsplash.jpg"))) {
Size size = new Size(image.Width, image.Height + lineHeight) + (2 * border);
using (Bitmap bm = new Bitmap(size)) {
RectangleF content = bm.Bounds;
content.Inflate(-border);
using (Graphics graphics = Graphics.FromImage(bm)) {
graphics.DrawImage(image, image.Bounds + (Point)border);
string txt = "My image caption";
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Far;
graphics.DrawString(txt, font, Brushes.Black, content, format);
}
bm.Save(Server.MapPath("IG8_StringFormat_Alignment.png"));
}
}

vladimir-tsokalo-N3moZ3Gn-gI-unsplash.jpg

IG8_StringFormat_Alignment.png
|