前言
WPF如何使用DrawingVisual绘制验证码?
XAML代码如下:
<Grid>
<Image Source="{Binding ImageSource,RelativeSource={RelativeSource AncestorType={x:Type local:CheckCode}}}"
MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
</Grid>
后台XAML.CS
private static string CreateCode(int strLength)
{
var strCode = "abcdefhkmnprstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; ;
var _charArray = strCode.ToCharArray();
var randomCode = "";
int temp = -1;
Random rand = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < strLength; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(strCode.Length - 1);
if (!string.IsNullOrWhiteSpace(randomCode))
{
while (randomCode.ToLower().Contains(_charArray[t].ToString().ToLower()))
{
t = rand.Next(strCode.Length - 1);
}
}
if (temp == t)
{
return CreateCode(strLength);
}
temp = t;
randomCode += _charArray[t];
}
return randomCode;
}
private ImageSource CreateCheckCodeImage(string checkCode, int width, int height)
{
if (string.IsNullOrWhiteSpace(checkCode))
return null;
if (width <= 0 || height <= 0)
return null;
DrawingVisual drawingVisual = new DrawingVisual();
Random random = new Random(Guid.NewGuid().GetHashCode());
using (DrawingContext dc = drawingVisual.RenderOpen())
{
dc.DrawRectangle(Brushes.White, new Pen(Brushes.Silver, 1D), new Rect(new Size(70, 23)));
FormattedText formattedText = new FormattedText(checkCode,
System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(new FontFamily("Arial"), FontStyles.Oblique, FontWeights.Bold, FontStretches.Normal),
20.001D, new LinearGradientBrush(Colors.Green, Colors.DarkRed, 1.2D))
{
MaxLineCount = 1,
TextAlignment = TextAlignment.Justify,
Trimming = TextTrimming.CharacterEllipsis
};
dc.DrawText(formattedText, new Point(3D, 0.1D));
for (int i = 0; i < 10; i++)
{
int x1 = random.Next(width - 1);
int y1 = random.Next(height - 1);
int x2 = random.Next(width - 1);
int y2 = random.Next(height - 1);
dc.DrawGeometry(Brushes.Silver, new Pen(Brushes.Silver, 0.5D), new LineGeometry(new Point(x1, y1), new Point(x2, y2)));
}
for (int i = 0; i < 100; i++)
{
int x = random.Next(width - 1);
int y = random.Next(height - 1);
SolidColorBrush c = new SolidColorBrush(Color.FromRgb((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255)));
dc.DrawGeometry(c, new Pen(c, 1D), new LineGeometry(new Point(x - 0.5, y - 0.5), new Point(x + 0.5, y + 0.5)));
}
dc.Close();
}
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(70, 23, 96, 96, PixelFormats.Pbgra32);
renderBitmap.Render(drawingVisual);
return BitmapFrame.Create(renderBitmap);
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ImageSource = CreateCheckCodeImage(CreateCode(4), (int)this.Width, (int)this.Height);
}
效果:
WPF开发者QQ群: 340500857
blogs: https://www.cnblogs.com/yanjinhua
Github:https://github.com/yanjinhuagood
作者:驚鏵
出处:https://www.cnblogs.com/yanjinhua
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请著名作者 出处 https://github.com/yanjinhuagood
源码地址
Github:https://github.com/yanjinhuagood/WPFCheckCode
Gitee:https://gitee.com/yanjinhua/WPFCheckCode.git
技术群:添加小编微信并备注进群
小编微信:mm1552923
公众号:dotNet编程大全