Skip to content

Instantly share code, notes, and snippets.

@dvhthomas
Created October 4, 2009 05:58
Show Gist options
  • Save dvhthomas/201182 to your computer and use it in GitHub Desktop.
Save dvhthomas/201182 to your computer and use it in GitHub Desktop.
Create image dynamically for testing
public static MemoryStream CreateTempImage(int width, int height)
{
using (var image = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
string text = String.Format("{0:s}", DateTime.Now);
Graphics graphics = Graphics.FromImage(image);
var brush = new SolidBrush(Color.Red);
graphics.FillRectangle(brush, 0, 0, image.Width, image.Height);
var font = new Font("Arial", 18, FontStyle.Bold);
var textBrush = new SolidBrush(Color.White);
var format = new StringFormat { Alignment = StringAlignment.Center };
graphics.DrawString(text, font, textBrush, width / 2, height / 2, format);
var stream = new MemoryStream();
image.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
return stream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment