-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 35384a7
Showing
27 changed files
with
1,667 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Drawing; | ||
using System.Drawing.Drawing2D; | ||
using System.Drawing.Imaging; | ||
using System.Drawing.Text; | ||
|
||
//----------------------------------- | ||
// Developer : Arash Azizi | ||
// Telegram : t.me/ArashAz_pv | ||
// Email : [email protected] | ||
// Github : github.com/MrArashAzizi | ||
// Gitlab : gitlab.com/SirArash | ||
//----------------------------------- | ||
|
||
namespace Captcha | ||
{ | ||
class EZCaptcha | ||
{ | ||
public string Text | ||
{ | ||
get { return this.text; } | ||
} | ||
public Bitmap Image | ||
{ | ||
get { return this.image; } | ||
} | ||
public int Width | ||
{ | ||
get { return this.width; } | ||
} | ||
public int Height | ||
{ | ||
get { return this.height; } | ||
} | ||
|
||
private string text; | ||
private int width; | ||
private int height; | ||
private string familyName; | ||
private Bitmap image; | ||
|
||
private Random random = new Random(); | ||
|
||
public EZCaptcha(string InputText, int width, int height) | ||
{ | ||
this.text = InputText; | ||
this.SetDimensions(width, height); | ||
this.GenerateImage(); | ||
} | ||
|
||
public EZCaptcha(string InputText, int width, int height, string FontFamilyName) | ||
{ | ||
this.text = InputText; | ||
this.SetDimensions(width, height); | ||
this.SetFamilyName(FontFamilyName); | ||
this.GenerateImage(); | ||
} | ||
|
||
~EZCaptcha() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
|
||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
this.Dispose(true); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
this.image.Dispose(); | ||
} | ||
|
||
private void SetDimensions(int width, int height) | ||
{ | ||
if (width <= 0) | ||
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero."); | ||
if (height <= 0) | ||
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero."); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
private void SetFamilyName(string FontFamilyName) | ||
{ | ||
try | ||
{ | ||
Font font = new Font(this.familyName, 12F); | ||
this.familyName = FontFamilyName; | ||
font.Dispose(); | ||
} | ||
catch | ||
{ | ||
this.familyName = System.Drawing.FontFamily.GenericSerif.Name; | ||
} | ||
} | ||
|
||
private void GenerateImage() | ||
{ | ||
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb); | ||
|
||
Graphics g = Graphics.FromImage(bitmap); | ||
g.SmoothingMode = SmoothingMode.AntiAlias; | ||
Rectangle rect = new Rectangle(0, 0, this.width, this.height); | ||
|
||
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White); | ||
g.FillRectangle(hatchBrush, rect); | ||
|
||
SizeF size; | ||
float fontSize = rect.Height + 1; | ||
Font font; | ||
|
||
do | ||
{ | ||
fontSize--; | ||
font = new Font(this.familyName, fontSize, FontStyle.Bold); | ||
size = g.MeasureString(this.text, font); | ||
} while (size.Width > rect.Width); | ||
|
||
StringFormat format = new StringFormat(); | ||
format.Alignment = StringAlignment.Center; | ||
format.LineAlignment = StringAlignment.Center; | ||
|
||
GraphicsPath path = new GraphicsPath(); | ||
path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format); | ||
float v = 4F; | ||
PointF[] points = | ||
{ | ||
new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v), | ||
new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v), | ||
new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v), | ||
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v) | ||
}; | ||
Matrix matrix = new Matrix(); | ||
matrix.Translate(0F, 0F); | ||
|
||
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); | ||
|
||
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.Gray, Color.DarkGray); | ||
g.FillPath(hatchBrush, path); | ||
|
||
int m = Math.Max(rect.Width, rect.Height); | ||
for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) | ||
{ | ||
int x = this.random.Next(rect.Width); | ||
int y = this.random.Next(rect.Height); | ||
int w = this.random.Next(m / 50); | ||
int h = this.random.Next(m / 50); | ||
g.FillEllipse(hatchBrush, x, y, w, h); | ||
} | ||
|
||
font.Dispose(); | ||
hatchBrush.Dispose(); | ||
g.Dispose(); | ||
|
||
this.image = bitmap; | ||
} | ||
} | ||
} |
Oops, something went wrong.