.net图片的文字效果
2008-08-17 14:02 twxstar 阅读(603) 评论(0) 收藏 举报近段时间在做一个项目的时候,做了一个图片中的文字的立体效果处理
不过图片尺寸大的话,在循环比较的时候,速度比较慢
以下是代码
1
public partial class Form1 : Form2

{3
public Form1()4

{5
InitializeComponent();6
}7
private void button1_Click(object sender, EventArgs e)8

{9
Image MyImage = new Bitmap(@"c:\sourcepic.jpg", true);//这里修改你存放的图片的路径10
int PosX = 10;11
int PosY = MyImage.Height / 2 ;12
int MyFont = 150;13
bool bMyFound = false;14
int MyWidth = MyImage.Width;15
int MyHeight = MyImage.Height;16
int MySize = MyWidth * MyHeight;17
Bitmap MyPattern = new Bitmap(MyWidth, MyHeight);18
Bitmap MyBitmap = new Bitmap(MyImage);19
Graphics g = Graphics.FromImage(MyPattern);20
Graphics MyGraphics = Graphics.FromImage(MyImage);21
g.Clear(Color.White);22
while (!bMyFound)23

{24
Font NewFont = new Font("宋体", MyFont, FontStyle.Bold);25
SizeF MyString = new SizeF(MyWidth, MyHeight);26
MyString = g.MeasureString("示例文字", NewFont);27
if (MyString.Width < MyPattern.Width)28

{29
if (MyString.Height < MyPattern.Height)30

{31
bMyFound = true;32
g.DrawString("示例文字", NewFont, new SolidBrush(Color.Black), PosX, PosY);33

34
}35
}36
else37

{38
MyFont = MyFont - 1;39
}40
}41

42
for (int x = 1; x < MyPattern.Width; x++)43

{44
for (int y = 1; y < MyPattern.Height; y++)45

{46
int a, re, gr, bl, x1, y1, z1;47
if (MyPattern.GetPixel(x, y).ToArgb() == Color.Black.ToArgb())48

{49
a = MyBitmap.GetPixel(x, y).A;50
re = MyBitmap.GetPixel(x, y).R;51
gr = MyBitmap.GetPixel(x, y).G;52
bl = MyBitmap.GetPixel(x, y).B;53
x1 = re;54
y1 = gr;55
z1 = bl;56
if (bl + 25 < 255)57
bl = bl + 25;58
if (gr + 25 < 255)59
gr = gr + 25;60
if (re + 25 < 255)61
re = re + 25;62
if (x1 - 25 > 0)63
x1 = x1 - 25;64
if (y1 - 25 > 0)65
y1 = y1 - 25;66
if (z1 - 25 > 0)67
z1 = z1 - 25;68
MyGraphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), x, y + 1, 3, 3);69
MyGraphics.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(a, x1, y1, z1))), x, y, 1, 1);70
}71
}72
}73
MyImage.Save(@"c:\fff.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//保存图片的路径74
this.BackgroundImage = new Bitmap(@"c:\fff.jpg", true);;75
76
}
浙公网安备 33010602011771号