Begin Print

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace BeginPrint_c
{
///

/// Summary description for BeginPrint.
///

public class BeginPrint1 : System.Windows.Forms.Form
{
#region Class Local Storage
PrintDocument Pd;
Font Pf;
TextReader file;
int Pages = 0;
#endregion

private System.Windows.Forms.ComboBox cmbPrinters;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdStartPrint;
private System.Windows.Forms.ListBox lstPaper;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ListBox lstRes;

private System.ComponentModel.Container components = null;

public BeginPrint1()
{
InitializeComponent();

}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.cmbPrinters = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.cmdStartPrint = new System.Windows.Forms.Button();
this.lstPaper = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.lstRes = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// cmbPrinters
//
this.cmbPrinters.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbPrinters.Location = new System.Drawing.Point(16, 192);
this.cmbPrinters.Name = “cmbPrinters”;
this.cmbPrinters.Size = new System.Drawing.Size(256, 21);
this.cmbPrinters.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 176);
this.label1.Name = “label1”;
this.label1.Size = new System.Drawing.Size(256, 16);
this.label1.TabIndex = 1;
this.label1.Text = “Installed Printers”;
//
// cmdStartPrint
//
this.cmdStartPrint.Location = new System.Drawing.Point(88, 224);
this.cmdStartPrint.Name = “cmdStartPrint”;
this.cmdStartPrint.Size = new System.Drawing.Size(88, 32);
this.cmdStartPrint.TabIndex = 2;
this.cmdStartPrint.Text = “Start Print”;
this.cmdStartPrint.Click += new System.EventHandler(this.cmdStartPrint_Click);
//
// lstPaper
//
this.lstPaper.Location = new System.Drawing.Point(16, 24);
this.lstPaper.Name = “lstPaper”;
this.lstPaper.Size = new System.Drawing.Size(256, 56);
this.lstPaper.TabIndex = 3;
//
// label2
//
this.label2.Location = new System.Drawing.Point(18, 8);
this.label2.Name = “label2”;
this.label2.Size = new System.Drawing.Size(256, 16);
this.label2.TabIndex = 4;
this.label2.Text = “Paper Size”;
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 88);
this.label3.Name = “label3”;
this.label3.Size = new System.Drawing.Size(256, 16);
this.label3.TabIndex = 6;
this.label3.Text = “Printer Resolution”;
//
// lstRes
//
this.lstRes.Location = new System.Drawing.Point(16, 104);
this.lstRes.Name = “lstRes”;
this.lstRes.Size = new System.Drawing.Size(256, 56);
this.lstRes.TabIndex = 5;
//
// BeginPrint
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label3,
this.lstRes,
this.label2,
this.lstPaper,
this.cmdStartPrint,
this.label1,
this.cmbPrinters});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = “BeginPrint”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “BeginPrint”;
this.Load += new System.EventHandler(this.BeginPrint_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new BeginPrint1());
}

private void BeginPrint_Load(object sender, System.EventArgs e)
{
Init();
}

private void Init()
{
foreach(String p in PrinterSettings.InstalledPrinters)
cmbPrinters.Items.Add(p);

if ( cmbPrinters.Items.Count > 0 )
cmbPrinters.SelectedIndex = 0;

//Add a few paper sizes to the list box
lstPaper.Items.Add(PaperKind.A4.ToString());
lstPaper.Items.Add(PaperKind.Letter.ToString());
lstPaper.Items.Add(PaperKind.CSheet.ToString());

//Add all the printer resolutions to the list box
lstRes.Items.Add(PrinterResolutionKind.Custom.ToString());
lstRes.Items.Add(PrinterResolutionKind.Draft.ToString());
lstRes.Items.Add(PrinterResolutionKind.High.ToString());
lstRes.Items.Add(PrinterResolutionKind.Low.ToString());
lstRes.Items.Add(PrinterResolutionKind.Medium.ToString());
}

private void cmdStartPrint_Click(object sender, System.EventArgs e)
{
try
{
file = new StreamReader(“Test.txt”);
try
{
//Create the document and give it a somewhat unique name
Pd = new PrintDocument();
Pd.DocumentName = DateTime.Now.Millisecond.ToString();

//Install event handlers
Pd.BeginPrint += new PrintEventHandler(this.BeginPrint);
Pd.PrintPage += new PrintPageEventHandler(this.PagePrint);
Pd.EndPrint += new PrintEventHandler(this.EndPrint);

// Print the document.
Pd.Print();
}
finally
{
file.Close();
if (Pd != null)
Pd.Dispose();
if (Pf != null)
Pf.Dispose();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void BeginPrint1(object sender, PrintEventArgs ev)
{
PageSettings Psettings = Pd.DefaultPageSettings;

//Initialize the font
Pf = new Font(“Times New Roman”, 10);

Pd.PrinterSettings.PrinterName = cmbPrinters.SelectedItem.ToString();

foreach (PaperSize ps in Pd.PrinterSettings.PaperSizes)
{
if (ps.PaperName == lstPaper.SelectedItem.ToString())
{
Psettings.PaperSize = ps;
break;
}
}

foreach (PrinterResolution pr in Pd.PrinterSettings.PrinterResolutions)
{
if (pr.Kind.ToString() == lstRes.SelectedItem.ToString())
{
Psettings.PrinterResolution = pr;
break;
}
}

//Make 1/4 inch margins all around
Psettings.Margins = new Margins(25, 25, 25, 25);
Pd.DefaultPageSettings = Psettings;
//Reset the pages
Pages = 0;
}

private void EndPrint(object sender, PrintEventArgs ev)
{
Pf.Dispose();
}

// The PrintPage event is raised for each page to be printed.
private void PagePrint(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line = null;

//Keep track of pages as they are printed
if (++Pages == 2)
{
try
{
ev.PageSettings.Landscape = true;
}
catch (Exception ex)
{
ev.PageSettings.Landscape = false;
}
}
else
ev.PageSettings.Landscape = false;

// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / Pf.GetHeight(ev.Graphics);

// Iterate over the file, printing each line. Use a basic StringFormat
while (count++ < linesPerPage && ((line=file.ReadLine()) != null)) { yPos = topMargin + (count * Pf.GetHeight(ev.Graphics)); ev.Graphics.DrawString (line, Pf, Brushes.Black, leftMargin, yPos, new StringFormat()); } // If more lines exist, print another page. if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } } } [/csharp]

Print Dialogs

   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PrintDialogs_c
{
    public class PrintDialogs : System.Windows.Forms.Form
    {
    private Image MyImage;
    private PrintDocument pd;
    private PrintPreviewDialog Preview;

    private System.Windows.Forms.Button cmdShow;
    private System.Windows.Forms.Label lblPrint;
        private System.ComponentModel.Container components = null;

        public PrintDialogs()
        {
            InitializeComponent();

      MyImage = Bitmap.FromFile(@"d:colorbars.jpg");
      Preview = new PrintPreviewDialog();
      Preview.UseAntiAlias = true;
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.cmdShow = new System.Windows.Forms.Button();
      this.lblPrint = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // cmdShow
      // 
      this.cmdShow.Location = new System.Drawing.Point(352, 344);
      this.cmdShow.Name = "cmdShow";
      this.cmdShow.Size = new System.Drawing.Size(72, 24);
      this.cmdShow.TabIndex = 0;
      this.cmdShow.Text = "Show";
      this.cmdShow.Click += new System.EventHandler(this.cmdShow_Click);
      // 
      // lblPrint
      // 
      this.lblPrint.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.lblPrint.Location = new System.Drawing.Point(144, 32);
      this.lblPrint.Name = "lblPrint";
      this.lblPrint.Size = new System.Drawing.Size(280, 136);
      this.lblPrint.TabIndex = 1;
      // 
      // PrintDialogs
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(442, 373);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.lblPrint,
                                                                  this.cmdShow});
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "PrintDialogs";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "PrintDialogs";
      this.Load += new System.EventHandler(this.PrintDialogs_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new PrintDialogs());
        }

    private void PrintDialogs_Load(object sender, System.EventArgs e)
    {
      pd = new PrintDocument();
      pd.PrintPage += new PrintPageEventHandler(this.pd_Print);
      
      Preview.Document = pd;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      DrawIt(e.Graphics);
    }

    private void pd_Print(object sender, PrintPageEventArgs e)
    {
      lblPrint.Text += "pd_Print pd= " + sender.ToString() + "
" ;
      DrawIt(e.Graphics);
    }

    private void DrawIt(Graphics G)
    {
      G.SmoothingMode=SmoothingMode.AntiAlias;
      G.DrawImage(MyImage, 10, 10);

      LinearGradientBrush B = new LinearGradientBrush(
                              new Rectangle(0, 0, 50, 10),
                              Color.Red, Color.Blue, 
                              LinearGradientMode.ForwardDiagonal);
      G.FillEllipse(B, 10, 200, 200, 75);

      G.DrawString("Print Preview Test", 
                   new Font("Comic Sans MS",24), B, 50, 275);
    }

    private void cmdShow_Click(object sender, System.EventArgs e)
    {
      Preview.WindowState = FormWindowState.Maximized;
      pd.DocumentName = DateTime.Now.Ticks.ToString();
      Preview.ShowDialog();
    }
    }
}


           
          


Using PrintPreviewDialog

   
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Printing;

public class frmMain : System.Windows.Forms.Form {
    private System.Drawing.Printing.PrintDocument printDoc;
    private System.Windows.Forms.PrintPreviewDialog ppDialog;
    private System.Windows.Forms.Button btnPrint;
    public frmMain() {
        this.printDoc = new System.Drawing.Printing.PrintDocument();
        this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
        this.btnPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);

        this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
        this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
        this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
        this.ppDialog.Enabled = true;
        this.ppDialog.Location = new System.Drawing.Point(195, 22);
        this.ppDialog.MaximumSize = new System.Drawing.Size(0, 0);
        this.ppDialog.Opacity = 1;
        this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
        this.ppDialog.Visible = false;

        this.btnPrint.Location = new System.Drawing.Point(52, 28);
        this.btnPrint.Text = "Print";
        this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(176, 86);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.btnPrint});
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new frmMain());
    }

    private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
        PrinterSettings pSettings = new PrinterSettings();
        Font printFont = new Font("Arial", 12);

        int nTextPosY = e.MarginBounds.Top;
        int nTextPosX = e.MarginBounds.Left;
        int nHeight = (int)printFont.GetHeight(e.Graphics);

        foreach (string sPtr in PrinterSettings.InstalledPrinters) {
            pSettings.PrinterName = sPtr;
            if (pSettings.IsValid) {
                e.Graphics.DrawString(sPtr, printFont, Brushes.Black, nTextPosX, nTextPosY + 5);
                e.Graphics.DrawString("Can Duplex: " + pSettings.CanDuplex.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight));
                e.Graphics.DrawString("Is Default: " + pSettings.IsDefaultPrinter.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 2));
                e.Graphics.DrawString("Is Plotter: " + pSettings.IsPlotter.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 3));
                e.Graphics.DrawString("Landscape Angle: " + pSettings.LandscapeAngle.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 4));
                e.Graphics.DrawString("Maximum Copies: " + pSettings.MaximumCopies.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 5));
                e.Graphics.DrawString("Maximum Page: " + pSettings.MaximumPage.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 6));
                e.Graphics.DrawString("Minimum Page: " + pSettings.MinimumPage.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 7));
                e.Graphics.DrawString("Supports Color: " + pSettings.SupportsColor.ToString(),
                    printFont, Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight * 8));
                nTextPosY = nTextPosY + ((5 + nHeight * 8) + nHeight);
            }
        }
    }
    private void btnPrint_Click(object sender, System.EventArgs e) {
        ppDialog.Document = printDoc;
        ppDialog.ShowDialog();
    }
}

    


Steps through the common dialogs. Does nothing else useful


   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// CmnDlgs.cs -- steps through the common dialogs. Does nothing else useful.
//
//               Compile this program with the following command line:
//                   C:>csc CmnDlgs.cs
using System;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace clsCommonDialogs
{
    public class CommonDialogs
    {
        [STAThread]
        static public void Main ()
        {
            // Create and display a Choose Color dialog box.
            ColorDialog cd = new ColorDialog ();
            cd.ShowDialog ();
            cd.Dispose ();
            
            // Create and display a Choose Font dialog box.
            FontDialog fd = new FontDialog ();
            fd.ShowDialog ();
            fd.Dispose ();
                
            // Create and display an Open File dialog box.
            OpenFileDialog ofd = new OpenFileDialog ();
            ofd.ShowDialog ();
            ofd.Dispose ();
            
            // Create and display a Save File dialog box.
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.ShowDialog ();
            sfd.Dispose ();
            
            // Create and display a Page Setup dialog box.
            PrintDocument printDoc = new PrintDocument();
            PageSetupDialog psd = new PageSetupDialog ();
            psd.Document = printDoc;
            psd.ShowDialog ();
            psd.Dispose ();
            
            // Create and display an Print dialog box.
            PrintDialog pd = new PrintDialog ();
            pd.Document = printDoc;
            pd.ShowDialog ();
            pd.Dispose ();
            
            // Create and display an Print Preview File dialog box.
            // This dialog is not a part of the common dialog library.
            PrintPreviewDialog ppd = new PrintPreviewDialog ();
            ppd.ShowDialog ();
            ppd.Dispose ();
            printDoc.Dispose ();
        }
    }
}


           
          


Popup Menu action


   


  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class MainForm : System.Windows.Forms.Form
  {
    private int currFontSize = 20;

    private ContextMenu popUpMenu;

    private MenuItem currentCheckedItem;
    private MenuItem checkedHuge;
    private MenuItem checkedNormal;
    private MenuItem checkedTiny;

    public MainForm()
    {
      InitializeComponent();

      Text = "PopUp Menu";
      CenterToScreen();

      popUpMenu = new ContextMenu();

      popUpMenu.MenuItems.Add("Huge", new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Normal", new EventHandler(PopUp_Clicked));
      popUpMenu.MenuItems.Add("Tiny", new EventHandler(PopUp_Clicked));


      this.ContextMenu = popUpMenu;

      checkedHuge = this.ContextMenu.MenuItems[0];
      checkedNormal = this.ContextMenu.MenuItems[1];        
      checkedTiny = this.ContextMenu.MenuItems[2];
      currentCheckedItem = checkedNormal;
      currentCheckedItem.Checked = true;

      this.Resize += new System.EventHandler(this.MainForm_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);

    }

    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Form1";

    }

    static void Main() 
    {
      Application.Run(new MainForm());
    }
    private void PopUp_Clicked(object sender, EventArgs e) 
    {
      currentCheckedItem.Checked = false;

      MenuItem miClicked = (MenuItem)sender;
      string item = miClicked.Text;
      
      if(item == "Huge")
      {
        currFontSize = 30;
        currentCheckedItem = checkedHuge;
      }

      if(item == "Normal")
      {
        currFontSize = 20;
        currentCheckedItem = checkedNormal;
      }

      if(item == "Tiny")
      {
        currFontSize = 8;
        currentCheckedItem = checkedTiny;
      }
      currentCheckedItem.Checked = true;
      Invalidate();
        }

    private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.DrawString("Right click your mouse", 
        new Font("Times New Roman", (float)currFontSize), 
        new SolidBrush(Color.Black), 
        this.DisplayRectangle);
    }

    private void MainForm_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Dragging Images


   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class MainForm : Form {
    private PictureBox happyBox = new PictureBox();
    private int oldX, oldY;
    private bool isDragging;
    private Rectangle dropRect = new Rectangle(100, 100, 140, 170);

    public MainForm() {
        happyBox.SizeMode = PictureBoxSizeMode.StretchImage;
        happyBox.Location = new System.Drawing.Point(64, 32);
        happyBox.Size = new System.Drawing.Size(50, 50);
        happyBox.Cursor = Cursors.Hand;
        happyBox.Image = new Bitmap("happyDude.bmp");
        happyBox.MouseDown += new MouseEventHandler(happyBox_MouseDown);
        happyBox.MouseUp += new MouseEventHandler(happyBox_MouseUp);
        happyBox.MouseMove += new MouseEventHandler(happyBox_MouseMove);
        Controls.Add(happyBox);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 361);
        this.Text = "Dragging Images";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);

    }

    void happyBox_MouseMove(object sender, MouseEventArgs e) {
        if (isDragging) {
            happyBox.Top = happyBox.Top + (e.Y - oldY);
            happyBox.Left = happyBox.Left + (e.X - oldX);
        }
    }

    void happyBox_MouseUp(object sender, MouseEventArgs e) {
        isDragging = false;
        if (dropRect.Contains(happyBox.Bounds))
            MessageBox.Show("You win!", "What an amazing test of skill...");
    }

    void happyBox_MouseDown(object sender, MouseEventArgs e) {
        isDragging = true;
        oldX = e.X;
        oldY = e.Y;
    }

    private void MainForm_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.BlueViolet, dropRect);
        g.DrawString("Drag the happy guy in here...",new Font("Times New Roman", 25), Brushes.WhiteSmoke, dropRect);
    }
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
    }
}

    


PictureBox Scroll Text

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {
    Font textFont = new Font("Times New Roman", 24);

    public Form1() {
        this.panel1 = new System.Windows.Forms.Panel();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.panel1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        this.panel1.AutoScroll = true;
        this.panel1.AutoScrollMinSize = new System.Drawing.Size(600, 400);
        this.panel1.Controls.Add(this.pictureBox1);
        this.panel1.Location = new System.Drawing.Point(13, 13);
        this.panel1.Size = new System.Drawing.Size(267, 243);
        this.pictureBox1.BackColor = System.Drawing.SystemColors.Window;
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Size = new System.Drawing.Size(600, 400);
        this.pictureBox1.TabStop = false;
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 268);
        this.Controls.Add(this.panel1);
        this.Load += new System.EventHandler(this.Form1_Load);
        this.panel1.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
    }
    private void Form1_Load(object sender, EventArgs e) {
        Bitmap b = new Bitmap(600, 600);
        Graphics g = Graphics.FromImage(b);
        g.FillRectangle(Brushes.White, new Rectangle(0, 0, b.Width, b.Height));
        g.DrawString("Hello, World", textFont, Brushes.Black, 40, 40);
        g.DrawString("Hello, World", textFont, Brushes.Red, 40, 240);
        g.DrawString("Hello, World", textFont, Brushes.Blue, 350, 40);
        g.DrawString("Hello, World", textFont, Brushes.Green, 350, 240);
        pictureBox1.BackgroundImage = b;
        pictureBox1.Size = b.Size;
    }
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.PictureBox pictureBox1;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}