Mega Code Archive

 
Categories / C# / 2D Graphics
 

Create Graphics object From Image

using System; using System.Drawing; using System.Windows.Forms;     class HelloWorldBitmap: Form {      const  float fResolution = 300;      Bitmap bitmap = new Bitmap(1, 1);          public static void Main()      {           Application.Run(new HelloWorldBitmap());      }      public HelloWorldBitmap()      {           ResizeRedraw = true;                       bitmap.SetResolution(fResolution, fResolution);               Graphics grfx = Graphics.FromImage(bitmap);           Font     font = new Font("Times New Roman", 72);           Size     size = grfx.MeasureString(Text, font).ToSize();               bitmap = new Bitmap(bitmap, size);           bitmap.SetResolution(fResolution, fResolution);                           grfx = Graphics.FromImage(bitmap);           grfx.Clear(Color.White);           grfx.DrawString(Text, font, Brushes.Black, 0, 0);           grfx.Dispose();      }      protected override void OnPaint(PaintEventArgs pea)      {           grfx.DrawImage(bitmap, 0, 0);      } }