using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;//GDI+渲染力更强的类的命名空间
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Graphics g;//画布
Bitmap bmp;//图片
public Form2()
{
InitializeComponent();
//实例化一个图片
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bmp);//以一个图片来创建一个画布
g.Clear(Color.White);//设置画布的背景颜色
pictureBox1.Image = bmp;//画出来的东西显示出来
}
public Color Color = Color.Red;//画笔的颜色
public int lineWidth = 1;//线条的宽度
private void button2_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog()==DialogResult.OK)
{
button2.BackColor = cd.Color;
Color = cd.Color;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lineWidth = int.Parse(comboBox1.Text);
}
int type = 0;//1为矩形 2为饼
private void button1_Click(object sender, EventArgs e)
{
type = 1;
////画一个矩形
//Pen p = new Pen(Color, lineWidth);
//Rectangle re=new Rectangle(0,0,200,200);
//g.DrawRectangle(p,re);
////pictureBox1.Refresh();//刷新整个控件,使整个控件无效,导致整个控件重绘
//pictureBox1.Invalidate(); //使指定区域无效,导致重绘
//g.Dispose();
}
Point pStart;
Point pEnd;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
pStart = new Point(e.X, e.Y);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
pEnd = new Point(e.X,e.Y);//获取释放的点的坐标
Pen p = new Pen(Color,lineWidth);//实例化画具
int x=Math.Min(pStart.X,pEnd.X);//比较两个点的x坐标,返回较小的
int y=Math.Min(pStart.Y,pEnd.Y);//比较两个点的y坐标,返回较小的
int width=Math.Abs(pEnd.X - pStart.X);//获取两个点x坐标的差的绝对值
int height=Math.Abs(pEnd.Y - pStart.Y);//获取两个点y坐标的差的绝对值
if (type==1)
{
g.DrawRectangle(p,x,y,width,height);
}
if (type==2)
{
g.DrawPie(p, x, y, width, height, 30, 270);
}
if (type==3)
{
Rectangle re=new Rectangle(x,y,width,height);
Brush b = new LinearGradientBrush(re,Color,Color.Yellow,60);
g.FillEllipse(b, re);
}
if (type==4)
{
Bitmap bt = new Bitmap(imagePath);
TextureBrush tb = new TextureBrush(bt);
Rectangle re=new Rectangle(x,y,width,height);
g.FillEllipse(tb, re);
}
this.pictureBox1.Refresh();
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
pictureBox1.Refresh();
//在这个picture上画一个方框
Graphics g2 = pictureBox1.CreateGraphics();
pEnd = new Point(e.X, e.Y);//获取释放的点的坐标
Pen p = new Pen(Color, lineWidth);//实例化画具
int x = Math.Min(pStart.X, pEnd.X);//比较两个点的x坐标,返回较小的
int y = Math.Min(pStart.Y, pEnd.Y);//比较两个点的y坐标,返回较小的
int width = Math.Abs(pEnd.X - pStart.X);//获取两个点x坐标的差的绝对值
int height = Math.Abs(pEnd.Y - pStart.Y);//获取两个点y坐标的差的绝对值
g2.DrawRectangle(p, x, y, width, height);
}
}
//画饼
private void button3_Click(object sender, EventArgs e)
{
type = 2;
//Pen p = new Pen(Color.Black, 1);
//g.DrawPie(p,
}
//把图片画在控件上
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图片(*.jpg;*.bmp;*.png;*.gif)|*.jpg;*.bmp;*.png;*.gif";
if (ofd.ShowDialog()==DialogResult.OK)
{
string fileName = ofd.FileName;
//把图片拿出来
Image im=new Bitmap(fileName);
//g.DrawImage(im, 0,0,400,500);
int width = im.Width / 3;
int height = im.Height / 3;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Rectangle rec1 = new Rectangle(j * (width+5), i* (height+5), width, height);
Rectangle rec2 = new Rectangle(j*width,i*height,width,height);
g.DrawImage(im, rec1, rec2, GraphicsUnit.Pixel);
}
}
pictureBox1.Invalidate();
}
}
private void button5_Click(object sender, EventArgs e)
{
type = 3;//渐变椭圆
}
string imagePath="";
//图片椭圆
private void button6_Click(object sender, EventArgs e){
OpenFileDialog op = new OpenFileDialog();
op.Filter = "图片(*.jpg;*.bmp;*.png;*.gif)|*.jpg;*.bmp;*.png;*.gif";
if (op.ShowDialog()==DialogResult.OK)
{
imagePath = op.FileName;
type = 4;//图片椭圆
}
}
//把画出来的图像保存在本地
private void button7_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
sf.Filter="图片|*.jpg|";
if (sf.ShowDialog()==DialogResult.OK)
{
string savePath = sf.FileName;
//保存bmp到磁盘
bmp.Save(savePath,ImageFormat.Jpeg);
MessageBox.Show("保存成功!");
}
}
}
}