Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created July 21, 2017 02:42
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 adrianseeley/388716c06a39a876d8c662bfe5103451 to your computer and use it in GitHub Desktop.
Save adrianseeley/388716c06a39a876d8c662bfe5103451 to your computer and use it in GitHub Desktop.
calendar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace CalCal
{
class Program
{
static string[] DaysShort = new string[] { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
static string[] Days = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
static string[] Months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber" };
static string FontName = "Gotham Book";
static void Main(string[] args)
{
/*FontFamily[] ff = System.Drawing.FontFamily.Families;
for (int i = 0; i < ff.Length; i++)
{
Console.WriteLine(ff[i]);
}
Console.ReadLine();*/
DrawCalendar(
PixelsPerInch: 100,
InchesWide: 24,
InchesTall: 36,
MarginInches: 1.5f,
TitleInchesTall: 4f,
DaybarInchesTall: 2,
StartDate: new DateTime(2017, 7, 1)
);
}
static void DrawCalendar (
int PixelsPerInch,
float InchesWide,
float InchesTall,
float MarginInches,
float TitleInchesTall,
float DaybarInchesTall,
DateTime StartDate)
{
float pixelsWide = PixelsPerInch * InchesWide;
float pixelsTall = PixelsPerInch * InchesTall;
float pixelsMargin = PixelsPerInch * MarginInches;
float pixelsTitleTall = PixelsPerInch * TitleInchesTall;
float pixelsDaybarTall = PixelsPerInch * DaybarInchesTall;
Rectangle frameRectangle = new Rectangle(
(int)pixelsMargin,
(int)pixelsMargin,
(int)(pixelsWide - (2 * pixelsMargin)),
(int)(pixelsTall - (2 * pixelsMargin))
);
Rectangle titleRectangle = new Rectangle(
frameRectangle.X,
frameRectangle.Y,
frameRectangle.Width,
(int)pixelsTitleTall
);
Rectangle daybarRectangle = new Rectangle(
frameRectangle.X,
titleRectangle.Bottom,
frameRectangle.Width,
(int)pixelsDaybarTall
);
Rectangle gridRectangle = new Rectangle(
frameRectangle.X,
daybarRectangle.Bottom,
frameRectangle.Width,
frameRectangle.Height - titleRectangle.Height - daybarRectangle.Height
);
for (int i = 0; i < 13; i++)
{
Bitmap calendar = new Bitmap((int)pixelsWide, (int)pixelsTall);
using (Graphics g = Graphics.FromImage(calendar))
{
g.FillRectangle(Brushes.White, 0, 0, calendar.Width, calendar.Height);
//DrawRect(g, frameRectangle, Pens.Black);
DrawTitle(g, titleRectangle, Pens.Black, Brushes.Black, new Font(FontName, 115), StartDate);
DrawDaybar(g, daybarRectangle, Pens.Black, Brushes.Black, new Font(FontName, 34));
DrawGrid(g, gridRectangle, Pens.Black, Brushes.Black, new Font(FontName, 40), StartDate);
}
calendar.Save("Calendar_" + i + "_" + Months[StartDate.Month - 1] + "_" + StartDate.Year + ".png");
StartDate = StartDate.AddMonths(1);
}
}
static void DrawRect (Graphics g, Rectangle Rect, Pen Color)
{
g.DrawRectangle(Color, Rect);
}
static void DrawString (Graphics g, Rectangle Rect, Font Font, Brush Color, String s)
{
SizeF stringSize = g.MeasureString(s, Font);
PointF position = new PointF(
(Rect.Left + (Rect.Width / 2)) - (stringSize.Width / 2),
(Rect.Top + (Rect.Height / 2)) - (stringSize.Height / 2)
);
g.DrawString(s, Font, Color, position);
}
static void DrawStringCenterX(Graphics g, Rectangle Rect, Font Font, Brush Color, String s)
{
SizeF stringSize = g.MeasureString(s, Font);
PointF position = new PointF(
(Rect.Left + (Rect.Width / 2)) - (stringSize.Width / 2),
Rect.Top + 45
);
g.DrawString(s, Font, Color, position);
}
static void DrawStringAtPoint (Graphics g, int X, int Y, Font Font, Brush Color, string s)
{
g.DrawString(s, Font, Color, X, Y);
}
static void DrawTitle (Graphics g, Rectangle Rect, Pen FrameColor, Brush FontColor, Font Font, DateTime StartDate)
{
//DrawRect(g, Rect, FrameColor);
DrawString(g, Rect, Font, FontColor, (Months[StartDate.Month - 1] + " " + StartDate.Year).ToUpper());
}
static void DrawDaybar(Graphics g, Rectangle Rect, Pen FrameColor, Brush FontColor, Font Font)
{
float tilePixelsWide = Rect.Width / 7;
//DrawRect(g, Rect, FrameColor);
Rectangle tileRect = new Rectangle(Rect.X, Rect.Y, (int)tilePixelsWide, Rect.Height);
for (int x = 0; x < 7; x++)
{
DrawString(g, tileRect, Font, FontColor, DaysShort[x].ToUpper());
//DrawRect(g, tileRect, FrameColor);
tileRect.X += tileRect.Width;
}
}
static void DrawGrid(Graphics g, Rectangle Rect, Pen FrameColor, Brush FontColor, Font Font, DateTime StartDate)
{
float tilePixelsWide = Rect.Width / 7;
float tilePixelsTall = Rect.Height / 6;
Rectangle tileRect = new Rectangle(Rect.X, Rect.Y, (int)tilePixelsWide, (int)tilePixelsTall);
DateTime walkingDate = new DateTime(StartDate.Ticks);
int startIndex = Array.IndexOf(Days, walkingDate.DayOfWeek.ToString());
bool started = false;
bool done = false;
for (int y = 0; y < 6; y++)
{
for (int x = 0; x < 7; x++)
{
if (!started && startIndex == x)
{
started = true;
}
//DrawRect(g, tileRect, FrameColor);
if (started && !done)
{
DrawStringCenterX(g, tileRect, Font, FontColor, walkingDate.Day.ToString());
walkingDate = walkingDate.AddDays(1);
if (walkingDate.Day == 1)
{
done = true;
}
}
tileRect.X += tileRect.Width;
}
tileRect.Y += tileRect.Height;
tileRect.X = Rect.X;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment