电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

GDI+编程的10个基本技巧


发布日期:2019/11/9
 

创建绘图表面

创建绘图表面有两种常用的方法下面设法得到PictureBox的绘图表面

private void Form_Load(object sender SystemEventArgs e)

{

//得到pictureBox的绘图表面

Graphics g = thispictureBoxCreateGraphics();

}

private void pictureBox_Paint(object sender SystemWindowsFormsPaintEventArgs e)

{

//得到pictureBox的绘图表面

Graphics g = eGraphics;

}

可以利用Graphics对象绘制出各种图形图案控件的Paint事件和OnPaint方法都可以绘图都是好时机在OnPaint方法里绘制图案一定从参数e里面得到Graphics属性下面是两个例子

protected override void OnPaint(PaintEventArgs e)

{

eGraphicsClear(ColorWhite);

float x y w h;

x = thisLeft+;

y = thisTop+;

w = thisWidth;

h = thisHeight;

Pen pen = new Pen(ColorRed );

eGraphicsDrawRectangle(pen x y w h);

baseOnPaint (e);

}

private void PictureBoxII_Resize(object sender EventArgs e)

{

thisInvalidate();

}

private void button_Click(object sender SystemEventArgs e)

{

thispictureBoxIICreateGraphics()FillEllipse(

BrushesBlue );

}

和文本有关的三个类

FontFamily——定义有着相似的基本设计但在形式上有某些差异的一组字样无法继承此类

Font——定义特定的文本格式包括字体字号和字形属性无法继承此类

StringFormat——封装文本布局信息(如对齐方式和行距)显示操作(如省略号插入和国家标准 (National) 数字位替换)和 OpenType 功能无法继承此类

下面的程序显示了一段文字

private void button_Click(object sender SystemEventArgs e)

{

Graphics g = thispictureBoxIICreateGraphics();

gFillRectangle(BrushesWhite thispictureBoxIIClientRectangle);

string s = aaaaaaaaaaaaaaaaaaaaaaaaaa;

FontFamily fm = new FontFamily(ËÎÌå);

Font f = new Font(fm FontStyleBold GraphicsUnitPoint);

RectangleF rectF = new RectangleF( );

StringFormat sf = new StringFormat();

SolidBrush sbrush = new SolidBrush(ColorFromArgb( ));

sfLineAlignment = StringAlignmentCenter;

sfFormatFlags = StringFormatFlagsDirectionVertical;

gDrawString(s f sbrush rectF sf);

}

GDI+的路径——GraphicsPath类

GraphicsPath类提供了一系列属性和方法利用它可以获取路径上的关键点可以添加直线段圆等几何元素可以获得包围矩形进行拾取测试这些功能都怎么用要仔细看一下

private void button_Click(object sender SystemEventArgs e)

{

//绘图表面

Graphics g = thispictureBoxIICreateGraphics();

//填充成白色

gFillRectangle(BrushesWhite thisClientRectangle);

//弄一个绘图路径¶

GraphicsPath gp = new GraphicsPath();

//添加一些集合图形

gpAddEllipse( );

gpAddPie( );

gpAddRectangle(new Rectangle( ));

//在绘图表面上绘制绘图路径

gDrawPath(PensBlue gp);

//平移

gTranslateTransform( );

//填充绘图路径¶

gFillPath(BrushesGreenYellow gp);

gpDispose();

}

区域——Region类

从已有的矩形和路径可以创建Region使用GraphicsFillRegion方法绘制Region该类指示由矩形和由路径构成的图形形状的内部无法继承此类

渐变色填充

需要使用两个刷子

线性梯度刷子(LinearGradientBrush)

路径梯度刷子(PathGuadientBrush)

private void button_Click(object sender SystemEventArgs e)

{

//绘图表面

Graphics g = thispictureBoxIICreateGraphics();

gFillRectangle(BrushesWhite thispictureBoxIIClientRectangle);

//定义一个线性梯度刷子

LinearGradientBrush lgbrush =

new LinearGradientBrush(

new Point( )

new Point( )

ColorFromArgb( )

ColorFromArgb( ));

Pen pen = new Pen(lgbrush);

//用线性笔刷梯度效果的笔绘制一条直线段并填充一个矩形

gDrawLine(pen );

gFillRectangle(lgbrush );

//定义路径并添加一个椭圆

GraphicsPath gp = new GraphicsPath();

gpAddEllipse( );

//用该路径定义路径梯度刷子

PathGradientBrush brush =

new PathGradientBrush(gp);

//颜色数组

Color[] colors = {

ColorFromArgb( )

ColorFromArgb( )

ColorFromArgb( )

ColorFromArgb( )};

//定义颜色渐变比率

float[] r = {f f f f};

ColorBlend blend = new ColorBlend();

blendColors = colors;

blendPositions = r;

brushInterpolationColors = blend;

//在椭圆外填充一个矩形

gFillRectangle(brush );

//用添加了椭圆的路径定义第二个路径梯度刷子

GraphicsPath gp = new GraphicsPath();

gpAddEllipse( );

PathGradientBrush brush = new PathGradientBrush(gp);

//设置中心点位置和颜色

brushCenterPoint = new PointF( );

brushCenterColor = ColorFromArgb( );

//设置边界颜色

Color[] color = {ColorFromArgb( )};

brushSurroundColors = color;

//用第二个梯度刷填充椭圆

gFillEllipse(brush );

}

GDI+的坐标系统

通用坐标系——用户自定义坐标系

页面坐标系——虚拟坐标系

设备坐标系——屏幕坐标系

当页面坐标系和设备坐标系的单位都是象素时它们相同

private void button_Click(object sender SystemEventArgs e)

{

Graphics g = thispictureBoxIICreateGraphics();

gClear(ColorWhite);

thisDraw(g);

}

private void Draw(Graphics g)

{

gDrawLine(PensBlack );

gDrawEllipse(PensBlack );

gDrawArc(PensBlack );

gDrawRectangle(PensGreen );

}

private void button_Click(object sender SystemEventArgs e)

{

//左移

Graphics g = thispictureBoxIICreateGraphics();

gClear(ColorWhite);

gTranslateTransform( );

thisDraw(g);

}

private void button_Click(object sender SystemEventArgs e)

{

//右移

Graphics g = thispictureBoxIICreateGraphics();

gClear(ColorWhite);

gTranslateTransform( );

thisDraw(g);

}

private void button_Click(object sender SystemEventArgs e)

{

//旋转

Graphics g = thispictureBoxIICreateGraphics();

gClear(ColorWhite);

gRotateTransform();

thisDraw(g);

}

private void button_Click(object sender SystemEventArgs e)

{

//放大

Graphics g = thispictureBoxIICreateGraphics();

gClear(ColorWhite);

gScaleTransform(f f);

thisDraw(g);

}

private void button_Click(object sender SystemEventArgs e)

{

//缩小

Graphics g = thispictureBoxIICreateGraphics();

gClear(ColorWhite);

gScaleTransform(f f);

thisDraw(g);

}

全局坐标——变换对于绘图表面上的每个图元都会产生影响通常用于设定通用坐标系

一下程序将原定移动到控件中心并且Y轴正向朝上

//先画一个圆

Graphics g = eGraphics;

gFillRectangle(BrushesWhite thisClientRectangle);

gDrawEllipse(PensBlack );

//使y轴正向朝上必须做相对于x轴镜像

//变换矩阵为[]

Matrix mat = new Matrix( );

gTransform = mat;

Rectangle rect = thisClientRectangle;

int w = rectWidth;

int h = rectHeight;

gTranslateTransform(w/ h/);

//以原点为中心做一个半径为的圆

gDrawEllipse(PensRed );

gTranslateTransform( );

gDrawEllipse(PensGreen );

gScaleTransform( );

gDrawEllipse(PensBlue );

局部坐标系——只对某些图形进行变换而其它图形元素不变

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = eGraphics;

//客户区设置为白色

gFillRectangle(BrushesWhite thisClientRectangle);

//y轴朝上

Matrix mat = new Matrix( );

gTransform = mat;

//移动坐标原点到窗体中心

Rectangle rect = thisClientRectangle;

int w = rectWidth;

int h = rectHeight;

gTranslateTransform(w/ h/);

//在全局坐标下绘制椭圆

gDrawEllipse(PensRed );

gFillRectangle(BrushesBlack );

gFillRectangle(BrushesBlack );

gFillRectangle(BrushesBlack );

gFillRectangle(BrushesBlack );

//创建一个椭圆然后在局部坐标系中进行变换

GraphicsPath gp = new GraphicsPath();

gpAddEllipse( );

Matrix mat = new Matrix();

//平移

matTranslate( );

//旋转

matRotate();

gpTransform(mat);

gDrawPath(PensBlue gp);

PointF[] p = gpPathPoints;

gFillRectangle(BrushesBlack p[]X p[]Y+ );

gFillRectangle(BrushesBlack p[]X p[]Y+ );

gFillRectangle(BrushesBlack p[]X p[]Y );

gFillRectangle(BrushesBlack p[]X p[]Y );

gpDispose();

//baseOnPaint (e);

}

Alpha混合

ColorFromArgb()的A就是AlphaAlpha的取值范围从表示完全透明完全不透明

当前色=前景色×alpha/+背景色×(-alpha)/

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = eGraphics;

//创建一个填充矩形

SolidBrush brush = new SolidBrush(ColorBlueViolet);

gFillRectangle(brush );

//创建一个位图其中两个位图之间有透明效果

Bitmap bm = new Bitmap( );

Graphics bg = GraphicsFromImage(bm);

SolidBrush redBrush =

new SolidBrush(ColorFromArgb( ));

SolidBrush greenBrush =

new SolidBrush(ColorFromArgb( ));

bgFillRectangle(redBrush );

bgFillRectangle(greenBrush );

gDrawImage(bm );

//创建一个位图其中两个位图之间没有透明效果

Bitmap bm = new Bitmap( );

Graphics bg = GraphicsFromImage(bm);

bgCompositingMode = CompositingModeSourceCopy;

bgFillRectangle(redBrush );

bgFillRectangle(greenBrush );

gCompositingQuality = CompositingQualityGammaCorrected;

gDrawImage(bm );

//baseOnPaint (e);

}

反走样

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = eGraphics;

//放大

gScaleTransform( );

//没有反走样的图形和文字

Draw(g);

//设置反走样

gSmoothingMode = SmoothingModeAntiAlias;

//右移

gTranslateTransform( );

//再绘制就是反走样之后的了

Draw(g);

//baseOnPaint (e);

}

private void Draw(Graphics g)

{

//绘制图形和文字

gDrawLine(PensGray );

gDrawEllipse(PensGray );

string s = 反走样测试;

Font font = new Font(宋体 );

SolidBrush brush = new SolidBrush(ColorGray);

gDrawString(s font brush );

}

完了暂时先总结那么多以后发现必要的可以再补充

上一篇:谈谈WCF Stream对象限制操作

下一篇:PageNavigator控件实现客户端分页