//先画一个圆
graphics g = e.graphics;
g.fillrectangle(brushes.white, this.clientrectangle);
g.drawellipse(pens.black, -100, -100, 200, 200);
//使y轴正向朝上,必须做相对于x轴镜像
//变换矩阵为[1,0,0,-1,0,0]
matrix mat = new matrix(1, 0, 0, -1, 0, 0);
g.transform = mat;
rectangle rect = this.clientrectangle;
int w = rect.width;
int h = rect.height;
g.translatetransform(w/2, -h/2);
//以原点为中心,做一个半径为100的圆
g.drawellipse(pens.red, -100, -100, 200, 200);
g.translatetransform(100, 100);
g.drawellipse(pens.green, -100, -100, 200, 200);
g.scaletransform(2, 2);
g.drawellipse(pens.blue, -100, -100, 200, 200);
局部坐标系——只对某些图形进行变换,而其它图形元素不变。
protected override void onpaint(painteventargs e)
{
graphics g = e.graphics;
//客户区设置为白色
g.fillrectangle(brushes.white, this.clientrectangle);
//y轴朝上
matrix mat = new matrix(1, 0, 0, -1, 0, 0);
g.transform = mat;
//移动坐标原点到窗体中心
rectangle rect = this.clientrectangle;
int w = rect.width;
int h = rect.height;
g.translatetransform(w/2, -h/2);
//在全局坐标下绘制椭圆
g.drawellipse(pens.red, -100, -100, 200, 200);
g.fillrectangle(brushes.black, -108, 0, 8, 8);
g.fillrectangle(brushes.black, 100, 0, 8, 8);
g.fillrectangle(brushes.black, 0, 100, 8, 8);
g.fillrectangle(brushes.black, 0, -108, 8, 8);
//创建一个椭圆然后在局部坐标系中进行变换
graphicspath gp = new graphicspath();
gp.addellipse(-100, -100, 200, 200);
matrix mat2 = new matrix();
//平移
mat2.translate(150, 150);
//旋转
mat2.rotate(30);
gp.transform(mat2);
g.drawpath(pens.blue, gp);
pointf[] p = gp.pathpoints;
g.fillrectangle(brushes.black, p[0].x-2, p[0].y+2, 4, 4);
g.fillrectangle(brushes.black, p[3].x-2, p[3].y+2, 4, 4);
g.fillrectangle(brushes.black, p[6].x-4, p[6].y-4, 4, 4);
g.fillrectangle(brushes.black, p[9].x-4, p[9].y-4, 4, 4);
gp.dispose();
//base.onpaint (e);
}
alpha混合
color.fromargb()的a就是alpha。alpha的取值范围从0到255。0表示完全透明,255完全不透明。
当前色=前景色×alpha/255+背景色×(255-alpha)/255
protected override void onpaint(painteventargs e)
{
graphics g = e.graphics;
//创建一个填充矩形
solidbrush brush = new solidbrush(color.blueviolet);
g.fillrectangle(brush, 180, 70, 200, 150);
//创建一个位图,其中两个位图之间有透明效果
bitmap bm1 = new bitmap(200, 100);
graphics bg1 = graphics.fromimage(bm1);
solidbrush redbrush =
new solidbrush(color.fromargb(210, 255, 0, 0));
solidbrush greenbrush =
new solidbrush(color.fromargb(210, 0, 255, 0));
bg1.fillrectangle(redbrush, 0, 0, 150, 70);
bg1.fillrectangle(greenbrush, 30, 30, 150, 70);
g.drawimage(bm1, 100, 100);
//创建一个位图,其中两个位图之间没有透明效果
bitmap bm2 = new bitmap(200, 100);
graphics bg2 = graphics.fromimage(bm2);
bg2.compositingmode = compositingmode.sourcecopy;
bg2.fillrectangle(redbrush, 0, 0, 150, 170);
bg2.fillrectangle(greenbrush, 30, 30, 150, 70);
g.compositingquality = compositingquality.gammacorrected;
g.drawimage(bm2, 300, 200);
//base.onpaint (e);
}
反走样
protected override void onpaint(painteventargs e)
{
graphics g = e.graphics;
//放大8倍
g.scaletransform(8, 8);
//没有反走样的图形和文字
draw(g);
//设置反走样
g.smoothingmode = smoothingmode.antialias;
//右移40
g.translatetransform(40, 0);
//再绘制就是反走样之后的了
draw(g);
//base.onpaint (e);
}
private void draw(graphics g)
{
//绘制图形和文字
g.drawline(pens.gray, 10, 10, 40, 20);
g.drawellipse(pens.gray, 20, 20, 30, 10);
string s = "反走样测试";
font font = new font("宋体", 5);
solidbrush brush = new solidbrush(color.gray);
g.drawstring(s, font, brush, 10, 40);
}
完了。暂时先总结那么多。以后发现必要的可以再补充。