Working with System.Drawing in .net to create Captcha

I created a 6 digit alphanumeric text in C # and now I need to display it as an image. For this I use Bitmapand Graphicsin .Net.

As a result of this, I can get the image, but I need small changes in the font of the image, as we see in the Captcha Challenge zigzag, to make it a little difficult to read.

Here is my code ...

Bitmap bmp = new Bitmap(100, 30);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Navy);
string randomString = CaptchaImageResult.GetCaptchaString(6);
g.DrawString(randomString, new Font("Courier", 16), 
             new SolidBrush(Color.WhiteSmoke), 2, 2);
HttpResponse response = System.Web.HttpContext.Current.Response;
response.ContentType = "image/jpeg";
bmp.Save(response.OutputStream, ImageFormat.Jpeg);
bmp.Dispose();
new CaptchaChallenge { Captcha = bmp };

Update ..

I tried with this code, but the letters are not clear.

 Brush zigzagBrush = new System.Drawing.Drawing2D.HatchBrush(
                         System.Drawing.Drawing2D.HatchStyle.ZigZag, Color.White);
 g.DrawString(randomString, new Font("Courier", 16), zigzagBrush, 2, 2);
+4
source share
1 answer

Applying a drawn brush is better:

Bitmap bmp = new Bitmap(100, 30);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Navy);
g.DrawString(code, new Font("Courier", 16),
                     new SolidBrush(Color.WhiteSmoke), 2, 2);
g.FillRectangle(new HatchBrush(HatchStyle.BackwardDiagonal, Color.FromArgb(255,0,0,0),Color.Transparent), g.ClipBounds );
g.FillRectangle(new HatchBrush(HatchStyle.ForwardDiagonal, Color.FromArgb(255, 0, 0, 0), Color.Transparent), g.ClipBounds);
+3
source

Source: https://habr.com/ru/post/1613328/


All Articles