Unit 10A
檔案讀取視窗
//開啟檔案讀取視窗,成功則將檔案指定給字串fn
string fn = ""; //""為空字串
if (openFileDialog1.ShowDialog() ==
DialogResult.OK)//顯示對話方塊
fn = openFileDialog1.FileName;
Unit 10B
Image影像檔案讀取
//宣告影像變數讀取檔案
Image im1= Image.FromFile(fn);
Unit 10C
指定繪圖畫布,繪出影像
//建立f2視窗的繪圖畫布(指定畫布在左上角0,0的位置)
Graphics g = f2.CreateGraphics();
g.DrawImage(im1, 0, 0);
Unit 10D
新設視窗
Form f2 = new Form();
//顯示視窗
f2.Show();
練習題:
//按button開啟檔案後顯示在新視窗,視窗大小為影像大小
//定義30個Point型態的座標資料,按button後在新視窗顯示這些點,這些點以小圓點表示,圓心為Point座標,直徑為4
int rdX, rdY;
Form f3 = new Form();
f3.Width = 800;
f3.Height = 480;
f3.Show();
Graphics g= f3.CreateGraphics();
for (int i = 0; i < 30; i++)
{
rdX = rd.Next(50, 750);
rdY = rd.Next(50, 430);
g.FillEllipse(Brushes.Red, rdX,
rdY, 4, 4);
String str = "(" +
rdX + "," + rdY + ")";
g.DrawString(str, new
System.Drawing.Font("Arial", 8), Brushes.Black, (float)rdX,
(float)rdY);
}
++++++++++++++++++++++++++++++++++++++++++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 影像讀取
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//開啟檔案讀取視窗,成功則將檔案指定給字串fn
string fn = ""; //""為空字串
if (openFileDialog1.ShowDialog() == DialogResult.OK)//顯示對話方塊
fn = openFileDialog1.FileName;
label1.Text = fn;
//宣告影像變數讀取檔案
Image im1= Image.FromFile(fn);
//新設視窗(做自己的繪圖畫布,建立視窗)
//練習:讓選取的影像大小等於顯示視窗的大小
Form f2 = new Form();
f2.Width = im1.Width;
f2.Height = im1.Height;
f2.BackColor = Color.FromArgb(126, 60, 255);
//顯示視窗
f2.Show();
//建立f2視窗的繪圖畫布(指定畫布在左上角0,0的位置)
Graphics g = f2.CreateGraphics();
g.DrawImage(im1, 0, 0);
}
Random rd = new Random();
Point[] p = new Point[30];
private void button2_Click(object sender, EventArgs e)
{
//定義30個Point型態的座標資料,按button後在新視窗顯示這些點,
//這些點以小圓點表示,圓心為Point座標,直徑為4
//g.FillEllipse(Brushes.Red, 50, 100, 4, 4);
int rdX, rdY;
Form f3 = new Form();
f3.Width = 800;
f3.Height = 480;
f3.Show();
Graphics g= f3.CreateGraphics();
for (int i = 0; i < 30; i++)
{
rdX = rd.Next(50, 750);
rdY = rd.Next(50, 430);
g.FillEllipse(Brushes.Red, rdX, rdY, 4, 4);
String str = "(" + rdX + "," + rdY + ")";
g.DrawString(str, new System.Drawing.Font("Arial", 8), Brushes.Black, (float)rdX, (float)rdY);
}
private void button3_Click(object sender, EventArgs e)
{
int rd1X, rd1Y;
Form f4 = new Form();
f4.Width = 800;
f4.Height = 480;
f4.Show();
Graphics g = f4.CreateGraphics();
for (int i = 0; i < 10; i++)
{
rd1X = rd.Next(0, 800);
rd1Y = rd.Next(50, 430);
g.FillRectangle(Brushes.BlueViolet, rd1X, rd1Y, 3, 480);
}
}
}
}