Unit 17A 類別定義與動畫
1.
設定視窗為(720,480),載入大地影像(至少1024*1024)
2.
將人物影像擺在視窗正中央
3.
以畫布平移方式,e.Graphics.TranslateTransform(x,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
WindowsFormsApplication1
{
public
partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
Image
im1 = Properties.Resources.f1;
int
x, y;
private
void Form1_Load(object
sender, EventArgs e)
{
this.Width
= 720; this.Height = 480;
this.DesktopLocation
= new Point(10,
10);
x=(720-im1.Width)/2;y=(480-im1.Height)/2;
}
private
void Form1_Paint(object
sender, PaintEventArgs e)
{
this.DoubleBuffered=true;
e.Graphics.TranslateTransform(x,
y);
e.Graphics.DrawImage(im1,x,y);
e.Graphics.ResetTransform(); //繪圖畫布還原
e.Graphics.FillEllipse(Brushes.Red, (720 - 64) / 2, (480 - 64) / 2, 64,
64);
}
private
void Form1_KeyDown(object
sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Left)
x=x-3;
else
if (e.KeyCode==Keys.Right)
x=x+3;
if
(e.KeyCode == Keys.Up)
y = y - 3;
else
if (e.KeyCode == Keys.Down)
y = y + 3;
this.Refresh();
}
}
}
Unit 17B
影像局部放大
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 Unit_17B
{
public
partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
int
x1, y1, x2, y2;
int
w = 96, h = 80;
int
x3=300, y3=150;
int
r = 3;
Image
im1 = Properties.Resources.wattch2;
private
void Form1_Load(object
sender, EventArgs e)
{
this.Width
= 960; this.Height = 640;
this.DesktopLocation
= new Point(10,
10);
}
private
void Form1_Paint(object
sender, PaintEventArgs e)
{
this.DoubleBuffered=true;
e.Graphics.DrawImage(im1, 0, 0);
//DrawRectangle(線色彩寬度,左上x,左上y,寬,高)
e.Graphics.DrawRectangle(new Pen(Color.Red, 3),
x1 - 84 / 2, y1 - 64
/ 2, w, h);
//re1:
影像上的某一矩形位置左上角(300,150),矩形寬高(80,50)
Rectangle
re1 =new Rectangle(x1-w/2,y1-h/2,w,h);
Rectangle
re2= new Rectangle(600,150,w*r,h*r);
e.Graphics.DrawImage(im1,re2,re1,GraphicsUnit.Pixel);
}
private
void Form1_MouseMove(object
sender, MouseEventArgs e)
{
Point
p = e.Location;
x1 = p.X; y1 = p.Y;
this.Refresh();
}
}
}