用VB.NET绘制GDI图形例程
Protected Overrides Sub onpaint(ByVal e As System.Windows.Forms.PaintEventArgs)
【教程】用VB.NET绘制GDI图形
注释://///////////绘制任意直线
Dim g As Graphics = e.Graphics
Dim mypen As Pen = New Pen(Color.Red, 2)
g.DrawLine(mypen, 100, 100, 10, 10)
注释://///////////绘制矩形(任意直线构成的封闭图形)
Dim point1 As PointF = New PointF(100F, 100F)
Dim point2 As PointF = New PointF(200F, 100F)
Dim point3 As PointF = New PointF(200F, 200F)
Dim point4 As PointF = New PointF(100F, 200F)
Dim curvepoints As PointF() = {point1, point2, point3, point4}
g.DrawPolygon(New Pen(Color.Blue, 2), curvepoints)
注释:////////////文本表示
Dim FFamily As FontFamily = New FontFamily("Arial")
Dim font As Font = New Font(FFamily, "20", FontStyle.Bold, FontStyle.Italic,
GraphicsUnit.Pixel)
Dim text As String = "I love you!"
Dim solidbrush As SolidBrush = New SolidBrush(Color.Red)
Dim pr As PointF = New PointF(100, 10)
e.Graphics.DrawString(text, font, solidbrush, pr)
注释:////////////平面绘制
Dim rec As RectangleF = New RectangleF(10, 10, 200, 100)
g.DrawPie(mypen, rec, 150, 150)
注释:///////////封闭图形,0.7应该是个圆
g.DrawClosedCurve(mypen, curvepoints, 0.7,
Drawing.Drawing2D.FillMode.Alternate)
注释:///////////大家自己试试看吧
g.DrawArc(mypen, 300, 300, 200, 200, 100, 100)
g.DrawCurve(mypen, curvepoints)
g.DrawBezier(mypen, 50, 50, 100, 50, 100, 100, 50, 100)
g.DrawBeziers(mypen, curvepoints)
注释://////////这可是一个圆
Dim rec1 As RectangleF = New RectangleF(10, 10, 100, 100)
g.DrawEllipse(mypen, rec1)
注释://////////这是一个椭圆
Dim rec2 As RectangleF = New RectangleF(10, 10, 200, 100)
g.DrawEllipse(mypen, rec2)
End Sub
如果需要提高图片质量,把
image.Save(sDstFilePath, ImageFormat.Jpeg);
改成
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
myEncoderParameters.Param[0] = myEncoderParameter;
image.Save(sDstFilePath, myImageCodecInfo, myEncoderParameters);
myEncoderParameter.Dispose();
myEncoderParameters.Dispose();
这是提高质量后的效果:
附一些关键代码:
public class MyGDI
{
public static void CreateWatermark(string sSrcFilePath, string sDstFilePath, string sText1, string sColor1, string sSize1, string sFont1, string sText2, string sColor2, string sSize2, string sFont2, string sBgColor, string sTransparence)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //文字抗锯齿
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f1 = new Font(sFont1, float.Parse(sSize1));
Font f2 = new Font(sFont2, float.Parse(sSize2));
Brush brushfortext1 = new SolidBrush(ColorTranslator.FromHtml(sColor1));
Brush brushfortext2 = new SolidBrush(ColorTranslator.FromHtml(sColor2));
Brush brushforbg = new SolidBrush(Color.FromArgb(Convert.ToInt16(255 * float.Parse(sTransparence)), ColorTranslator.FromHtml(sBgColor)));
【教程】用VB.NET绘制GDI图形 g.RotateTransform(-20);
Rectangle rect = new Rectangle(-image.Width/2-50, image.Height - 50, image.Width * 2, 40);
g.DrawRectangle(new Pen(brushforbg), rect);
g.FillRectangle(brushforbg, rect);
Rectangle rectfortext1 = new Rectangle(-image.Width/2 + image.Width / 5, image.Height - 45, image.Width * 2, 60);
for (int i = 0; i < 10; i++)
g.DrawString(sText1, f1, brushfortext1, rectfortext1);
Rectangle rectfortext2 = new Rectangle(-image.Width / 2 + image.Width / 5, image.Height -25, image.Width * 2, 60);
for (int i = 0; i < 10; i++)
g.DrawString(sText2, f2, brushfortext2, rectfortext2);
image.Save(sDstFilePath, ImageFormat.Jpeg);
image.Dispose();
}
public static void CreateRoundedCorner(string sSrcFilePath, string sDstFilePath, string sCornerLocation)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, sCornerLocation); //构建圆角外部路径
Brush b = new SolidBrush(Color.White);//圆角背景白色
g.DrawPath(new Pen(b), rectPath);
g.FillPath(b, rectPath);
g.Dispose();
image.Save(sDstFilePath, ImageFormat.Jpeg);
image.Dispose();
}
public static void CreatePlainText(string sSrcFilePath, string sDstFilePath,string sText, string sColor, string sSize, string sFont)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sSrcFilePath);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(image, 0, 0, image.Width, image.Height);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //文字抗锯齿
Font f = new Font(sFont,float.Parse(sSize));
Brush b = new SolidBrush(ColorTranslator.FromHtml(sColor));
Rectangle rect = new Rectangle(10, 5, image.Width, image.Height); //适当空开一段距离
for (int i = 0; i < 30; i++) //加强亮度
g.DrawString(sText, f, b, rect);
image.Save(sDstFilePath, ImageFormat.Jpeg);
image.Dispose();
}
private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, string sPosition)
{
GraphicsPath rectPath = new GraphicsPath();
switch (sPosition)
{
case "TopLeft":
{
rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
break;
}
case "TopRight":
{
rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
break;
}
case "BottomLeft":
{
rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
break;
}
case "BottomRight":
{
rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
break;
}
}
return rectPath;
}
}
我再来详细解释一下刚才实现双缓冲的具体步骤:
1、 在内存中建立一块“虚拟画布”:
Bitmap bmp = new Bitmap(600, 600);
2、 获取这块内存画布的Graphics引用:
Graphics g = Graphics.FromImage(bmp);
3、 在这块内存画布上绘图:
g.FillEllipse(brush, i * 10, j * 10, 10, 10);
4、将内存画布画到窗口中
this.CreateGraphics().DrawImage(bmp, 0, 0);
总结
怎么样?是不是很简单?但是正是这个简单的操作大大提高了绘图效率,所以如果你需要进行GDI+图形编程,双缓冲技术一定要掌握,特别是在进行大量图形绘制刷新的情况下要尽量采用。
Rect *magnifier; //方形区域
GraphicsPath path; //放大镜圆周
Region *region; //放大镜区域
magnifier=new Rect(point.x-100,point.y-100,200,200); //生成方形区域
path.AddEllipse(*magnifier); //根据方形区域生成圆周
region=new Region(&path); //构造放大镜区域
graphics->IntersectClip(region); //设置剪切区域
graphics->DrawImage(p_img,2*picRect.X-point.x,2*picRect.Y-point.y,2*picRect.Width,2*picRect.Height); //绘制放大镜区域图像
graphics->ReSetClip();
graphics->ExcludeClip(region);【教程】用VB.NET绘制GDI图形
graphics->DrawImage(p_img,picRect); //绘制原图