如何在PictureBox上透明的显示文字2(利用Graphics技术)
利用c#的GDI+技术,PictureBox.CreateGraphics()绘图,利用g.DrawString写文字。
利用this.Invalidate()刷新Form窗体,或者利用PictureBox.Invalidate()刷新PictureBox.。
范例代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![]()
9
namespace CCDTest
10
{
11
public partial class Form1 : Form
12
{
13
string filename;
14
//Label lblResult;
15
public Form1()
16
{
17
InitializeComponent();
18
filename = Application.StartupPath + "\\cc1.bmp";
19
}
20![]()
21
private void Form1_Load(object sender, EventArgs e)
22
{
23
pbCCD.Load(filename);
24
}
25![]()
26
private void pbCCD_MouseDown(object sender, MouseEventArgs e)
27
{
28
PointF pf = e.Location;
29![]()
30
using (Graphics g = pbCCD.CreateGraphics())
31
{
32
Console.WriteLine("Beg MyDraw
.");
33
Font f = new Font("Arial", 12);
34
g.DrawString("Hello!", f, Brushes.Violet, pf);
35
Console.WriteLine("End MyDraw
..");
36
}
37
}
38![]()
39
private void pbCCD_MouseUp(object sender, MouseEventArgs e)
40
{
41
pbCCD.Invalidate();
42
}
43
}
44
}
45![]()
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
namespace CCDTest10
{11
public partial class Form1 : Form12
{13
string filename;14
//Label lblResult;15
public Form1()16
{17
InitializeComponent();18
filename = Application.StartupPath + "\\cc1.bmp";19
}20

21
private void Form1_Load(object sender, EventArgs e)22
{23
pbCCD.Load(filename);24
}25

26
private void pbCCD_MouseDown(object sender, MouseEventArgs e)27
{28
PointF pf = e.Location;29

30
using (Graphics g = pbCCD.CreateGraphics())31
{32
Console.WriteLine("Beg MyDraw
.");33
Font f = new Font("Arial", 12);34
g.DrawString("Hello!", f, Brushes.Violet, pf);35
Console.WriteLine("End MyDraw
..");36
}37
}38

39
private void pbCCD_MouseUp(object sender, MouseEventArgs e)40
{41
pbCCD.Invalidate();42
}43
}44
}45


浙公网安备 33010602011771号