.NET GDI

System.Drawing
System.Drawing.2D
System.Drawing.Imaging
System.Drawing.Text

Drawing

Color.FromArgb
Color.FromName

Size(w,h)
Point(x,y)
Rectangle()

1
2
3
4
5
6
7
8
9
10
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Red, 2);
Brush brush = new SolidBrush(Color.Blue);
Font font = new Font("宋体", 25);
Rectangle rect = new Rectangle(20, 120, 100, 160);
g.DrawLine(pen, 20, 100, 100, 100);
g.DrawRectangle(pen, rect);
g.DrawString("GDI+图形编程", font, brush, 20, 20);
brush.Dispose(); font.Dispose(); pen.Dispose();
g.Dispose();

Graphics

DrawARC:绘制圆弧
DrawBezier:绘制贝塞尔曲线
DrawClosedCurve:绘制封闭曲线
DrawCurve:绘制曲线
DrawEllipse:绘制椭圆 **
DrawIcon:绘制图标
DrawImage:绘制图像 **
DrawLine:绘制直线
DrawString:绘制文本
DrawRectangle:绘制矩形

FillXXX:绘制实心

Pen

Pen pen = new Pen(Color.Blue,5);

PenAlignment
DashStyle

Brush

抽象类,不能被实例化

双缓冲技术

DoubleBuffered

原理:

Bitmap bmp = new Bitmap(600,600);
Graphics g = Graphics.FromImage(bmp);
g.FillEllipse(brush, i10, j10, 10, 10);
g.DrawLine();
g.DrawString();
this.CreateGraphics().DrawImage(bmp, 0, 0);

图像处理

Image 抽象类,主要使用 Bitmap,处理BMP、Jpg、Gif、Png

  • bitmap.GetPixel(x,y); 获得像素点
  • 使用指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
public unsafe class UnsafeBitmap
{

Bitmap bitmap;
int stride;
BitmapData bitmapData = null;
Byte* pBase = null;

public UnsafeBitmap(Bitmap bitmap)
{
this.bitmap = new Bitmap(bitmap);
}

public UnsafeBitmap(int width, int height)
{
this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
}

public void Dispose()
{
bitmap.Dispose();
}

public Bitmap Bitmap
{
get
{
return (bitmap);
}
}

public struct PixelData
{
public byte blue;
public byte green;
public byte red;
}

private Point PixelSize
{
get
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF bounds = bitmap.GetBounds(ref unit); return new Point((int)bounds.Width, (int)bounds.Height);
}
}

public void LockBitmap()
{

GraphicsUnit unit = GraphicsUnit.Pixel;

RectangleF boundsF = bitmap.GetBounds(ref unit);
Rectangle bounds = new Rectangle((int)boundsF.X,
(int)boundsF.Y,
(int)boundsF.Width,
(int)boundsF.Height);



bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
pBase = (Byte*)bitmapData.Scan0.ToPointer();

stride = bitmapData.Stride;
//stride = (int)boundsF.Width * sizeof(PixelData);
//if (stride % 4 != 0)
//{
// stride = 4 * (stride / 4 + 1);
//}

}

public PixelData GetPixel(int x, int y)
{
PixelData returnValue = *PixelAt(x, y);
return returnValue;
}

public void SetPixel(int x, int y, PixelData colour)
{
PixelData* pixel = PixelAt(x, y);
*pixel = colour;
}

public void UnlockBitmap()
{
bitmap.UnlockBits(bitmapData);
bitmapData = null;
pBase = null;
}

public PixelData* PixelAt(int x, int y)
{
return (PixelData*)(pBase + y * stride + x * sizeof(PixelData));
}

}

public class Test {

static void Main()
{

Bitmap bitmap = new Bitmap(@"1.jpg");
UnsafeBitmap bitmap2 = new UnsafeBitmap( bitmap );

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for( int i=0; i<bitmap.Width; i++) for( int j=0; j<bitmap.Height; j++ )
{
Color c = bitmap.GetPixel( i,j );
}
sw.Stop();
Console.WriteLine( sw.ElapsedTicks );

sw.Reset();
sw.Start();
bitmap2.LockBitmap();
for( int i=0; i<bitmap.Width; i++) for( int j=0; j<bitmap.Height; j++ )
{
UnsafeBitmap.PixelData c = bitmap2.GetPixel( i,j );
}
bitmap2.UnlockBitmap();
sw.Stop();
Console.WriteLine( sw.ElapsedTicks );
}
}