(2010-08-13) C#.NET Graphics

摘要:(2010-08-13) C#.NET Graphics

命名空間:System.Drawing 
 

程式碼  (繪製文字)

 

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

namespace mod01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //要畫具
           // System.Drawing.Graphics g = this.CreateGraphics();
            System.Drawing.Graphics g = e.Graphics;
            Int32 y = 10;
            while (y < 25)
            {
                g.DrawString("認證中心", new System.Drawing.Font("標楷體", 48.0F),
                    new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(y*1,y*2,y*10)),
                    new System.Drawing.Point(10, y));
                y++;
            }
        }
    }
}

程式碼 (繪制 線 與 圓)

 

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

namespace mod01
{
    public partial class TestStringDrawing : Form
    {
        public TestStringDrawing()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           //MessageBox.Show(this.panel1.BackColor.ToArgb().ToString());

            //要Panel建立自己Graphics
            System.Drawing.Graphics g = this.panel1.CreateGraphics();
            //刷底色
            g.FillRectangle(new SolidBrush(Color.LightGray),0,0,this.panel1.Width,this.panel1.Height);
            g.DrawString(this.textBox1.Text,new Font("標楷體",30.0f),
                new SolidBrush(Color.Blue),10,10);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics g = this.panel2.CreateGraphics();
            //陣列點
            Point[] ps = new Point[]{new Point(10,20),
            new Point(10,30),
            new Point(20,40),
            new Point(30,50)
            };
            g.DrawLines(new Pen(Color.Red), ps);
            //畫圓
            g.FillPie(new SolidBrush(Color.Red), 0, 0, 100, 100, 30, 300);

        }
    }
}

桯式碼( 繪制圖後存檔[含ICon] )

 

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

namespace mod01
{
    public partial class TestImage : Form
    {
        public TestImage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //問畫具
            System.Drawing.Graphics g = this.panel1.CreateGraphics();
            //建構Image物件\
            //建立串流
            //目錄

            String imgFile = System.Windows.Forms.Application.StartupPath + @"\Home.jpg";
            System.IO.Stream fs1=new System.IO.FileStream(imgFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.Drawing.Image img = new System.Drawing.Bitmap(fs1);
            System.Drawing.Image newImage = new System.Drawing.Bitmap(img, new Size(this.panel1.Width, this.panel1.Height));
            //Image img = Image.FromFile(imgFile);
            g.DrawImage(newImage,new Point(0,0));
           
            //儲存
            newImage.Save(@"c:\home_new.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

          //Icon
            System.Drawing.Icon icon = new Icon(Application.StartupPath + @"\error.ico");
            this.CreateGraphics().DrawIcon(icon, 100, 10);
            this.Icon = icon;           
        }
    }
}