Friday 15 April 2016

c# graphics lecture

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging; //allows file graphics
using System.IO;
using System.Drawing.Drawing2D;

namespace drawit
{
    public partial class Form1 : Form
    {
        bool eye = true;
        Graphics g;  //draws on form
        Pen p = new Pen(Color.Black);
        SolidBrush b = new SolidBrush(Color.Black);
        Font f = new Font(new FontFamily("Arial"), 12, FontStyle.Bold);

        public Form1()
        {   InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {//play a media file
            System.Diagnostics.Process.Start("wmplayer.exe", 
                                            @"c:\code\clock.avi");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            axAgent1.Characters.Load("wizard", 
                     @"c:\windows\msagent\chars\merlin.acs");
            axAgent1.Characters["wizard"].Show(null);
            axAgent1.Characters["wizard"].MoveTo(200, 300, 100);
            axAgent1.Characters["wizard"].Speak("hi there");
            axAgent1.Characters["wizard"].Think("who are you");
            axAgent1.Characters["wizard"].Play("congratulate");
            axAgent1.Characters["wizard"].Play("announce");
            axAgent1.Characters["wizard"].Play("confused");
            axAgent1.Characters["wizard"].Play("explain");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            g.Clear(Color.Aqua); //change background color
            g.DrawString("Hi there", f, b, 50, 100);
            g.DrawLine(p, 50, 100, 200, 200);
            g.FillRectangle(b, 200, 100, 50, 50);
            g.DrawEllipse(p, 100, 150, 30, 30);
            
            g.DrawPie(p, 250, 150, 40, 40, 0, 45);
            g.DrawArc(p, 100, 100, 40, 40, 90, 90);
            g.DrawImage(Image.FromFile(@"c:\code\dog.jpg"), 50, 150, 50, 50);
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Pen pback = new Pen(Color.Aqua);
            if (eye)
            {//redraw eye using background color
                //then draw line using black
                g.DrawEllipse(pback, 100, 150, 30, 30);//eye
                g.DrawLine(p, 100, 165, 130, 165);//line inside eye
                eye = false;
            }
            else
            {//redraw line using background color
                //then draw eye using black
                g.DrawLine(pback, 100, 165, 130, 165);
                //eye
                g.DrawEllipse(p, 100, 150, 30, 30);
                eye = true;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            g = base.CreateGraphics();//draw on form
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string pics = "147.2580369=+-*/";
            int x = 0;
            int row = 50;
            int col = 50;
            Font f2 = new Font(new FontFamily("Arial"), 18, FontStyle.Bold);
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    g.DrawRectangle(p, row, col, 40, 40);
                    g.DrawString(pics.Substring(x, 1), f2, b, 
                                         row + 10, col + 10);
                    col = col + 60;
                    x = x + 1;
                }
                row = row + 60;
                col = 50;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Graphics g2 = panel1.CreateGraphics();//draw on panel
            Pen p = new Pen(Color.Black);
            g2.DrawLine(p, 20, 20, 60, 60);
        }

        private void button6_Click(object sender, EventArgs e)
        {   Bitmap bm = new Bitmap(300, 300);//size of file
            Graphics g = Graphics.FromImage(bm); //hook up to a file
            g.Clear(Color.Aqua); //essential for files
            g.DrawString("Hi there", f, b, 50, 100);
            g.DrawLine(p, 50, 100, 200, 200);
            g.FillRectangle(b, 200, 100, 50, 50);
            g.DrawEllipse(p, 100, 150, 30, 30);
            g.DrawPie(p, 250, 150, 40, 40, 0, 45);
            g.DrawArc(p, 100, 100, 40, 40, 90, 90);
            g.DrawImage(Image.FromFile(@"c:\code\dog.jpg"), 50, 150, 50, 50);
            bm.Save(@"c:\code\sample.jpg", ImageFormat.Jpeg);
            //bm.Save(response.outputstream); //saves to a web page
            MessageBox.Show("file created");
        }
        private void button7_Click(object sender, EventArgs e)
        {//put copyright on a file
            Bitmap b = (Bitmap)Bitmap.FromFile(@"c:\code\dog.jpg");
            Graphics g = Graphics.FromImage(b);
            int size = b.Height;
            int y = size - 20; //subtract double font size
            Font f = new Font(new FontFamily("Arial"), 10, FontStyle.Bold);
            SolidBrush br = new SolidBrush(Color.Black); 
            g.DrawString("Copyright 2016 Randall", f, br, 10, y);
            b.Save(@"c:\code\dog2.jpg", ImageFormat.Jpeg);
            MessageBox.Show("file saved");
        }

        private void button8_Click(object sender, EventArgs e)
        {//do graphics on a printer
            DialogResult  p = printDialog1.ShowDialog();
            if (p == DialogResult.Cancel) return;
            printDocument1.PrinterSettings.PrinterName 
                = printDialog1.PrinterSettings.PrinterName;
            //printDocument1.Print(); //prints to printer
            //printPreviewControl1.Document = this.printDocument1;
            printPreviewDialog1.Document = this.printDocument1;
            printPreviewDialog1.ShowDialog();
        }
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {   Font f = new Font(new FontFamily("Arial"), 10, FontStyle.Regular);
            SolidBrush b = new SolidBrush(Color.Black);
            e.Graphics.DrawString("Randall", f, b, 0, 0);
            int column = 0;  int row = 12;
            e.Graphics.DrawString("X", f, b, column, row);
            e.Graphics.DrawString("X*X", f, b, column + 20, row);
            for (int x = 1; x <= 5; x++)
            {   row = row + 12;
                e.Graphics.DrawString(x.ToString(), f, b, column, row);
e.Graphics.DrawString((x * x).ToString(), f, b,column + 20, row);
            }
            e.HasMorePages = false;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hi");
        }

        private void button10_Click(object sender, EventArgs e)
        {//change shape of a button
            GraphicsPath g = new GraphicsPath();
            g.AddEllipse(new Rectangle(0, 0, button9.Size.Width - 5,
                                          button9.Size.Height - 5));
            button9.Region = new Region(g); //attach drawing to button
            button9.FlatStyle = FlatStyle.Flat;
            button9.FlatAppearance.BorderColor = Color.DarkGray;
            button9.BackColor = Color.DarkGray;
        }
    }
}

No comments:

Post a Comment