歡迎跟我連絡

本頁最下方有Web MSN可以直接跟我交談喔!
免安裝程式...哈哈 歡迎聊天

2009年7月30日 星期四

認證登錄系統圖片產生物件

目前網路上有很多防止程式自動登錄註冊...攻擊網頁的方式...
常見的方式有底下兩種...



今天利用C#寫了一個認證圖片產生物件IdentifyPicture,效果如下圖
只要USER點選到圖中唯一圓形的區塊就算認證成功(箭頭所指)...


程式碼:
public class IdentifyPicture
{
private Random myRandon;
private int mySharpNo;
private Size myPictureSize;
private Bitmap myBitmap;
public Bitmap IdPicture //產生的圖片
{
get { return myBitmap; }
}
private Region myRegion;
public Region IdRegion //驗證圓形於圖上範圍
{
get { return myRegion; }
}
public IdentifyPicture(Size PictureSize , int SharpNumber)
{
myRandon = new Random();
mySharpNo = SharpNumber;
myPictureSize = PictureSize;
myBitmap = new Bitmap(PictureSize.Width, PictureSize.Height);
myRegion = new Region();
DrawANewPicture();
}
private void DrawANewPicture()
{
GraphicsPath myGP = new GraphicsPath();
Graphics myG = Graphics.FromImage(myBitmap);
myG.Clear(Color.White);

//DrawEllipse
int mySize = GetSize(myPictureSize.Height);
Point myPoint = GetLocation(myPictureSize, mySize);
Pen myPen = new Pen(GetColor(), 3);
myG.DrawEllipse(myPen, new Rectangle(myPoint, new Size(mySize, mySize)));
myGP.AddEllipse(new Rectangle(myPoint, new Size(mySize, mySize)));
myRegion = new Region(myGP);

//DrawRectangle
for (int i = 0; i < mySharpNo; i++)
{
mySize = GetSize(myPictureSize.Height);
myPoint = GetLocation(myPictureSize, mySize);
myPen = new Pen(GetColor(), 3);
myG.DrawRectangle(myPen, new Rectangle(myPoint, new Size(mySize, mySize)));
}
for (int i = 0; i < mySharpNo; i++)
{
mySize = GetSize(myPictureSize.Height);
myPoint = GetLocation(myPictureSize, mySize);
myPen = new Pen(GetColor(), 3);
myG.DrawPie(myPen, new Rectangle(myPoint, new Size(mySize, mySize)), GetAngle(), GetAngle());
}
}
private Point GetLocation(Size PictureSize, int SharpWidth)
{
int X = myRandon.Next(0, PictureSize.Width - SharpWidth - 3); //3 Pen Width
int Y = myRandon.Next(0, PictureSize.Height - SharpWidth - 3);//3 Pen Width
return new Point(X, Y);
}
private int GetSize(int PictureWidth)
{
return (int)(PictureWidth / myRandon.Next(2, 6));
}
private Color GetColor()
{
switch (myRandon.Next(0, 10))
{
case 0:
return Color.Yellow;
case 1:
return Color.Violet;
case 2:
return Color.Green;
case 3:
return Color.DeepPink;
case 4:
return Color.DarkSlateBlue;
case 5:
return Color.DarkRed;
case 6:
return Color.CornflowerBlue;
case 7:
return Color.DarkSalmon;
case 8:
return Color.Gainsboro;
case 9:
return Color.Gold;
case 10:
return Color.Indigo;
default:
return Color.Black;
}
}
private float GetAngle()
{
return (float)(myRandon.Next(10, 350));
}
}

應用範例如下:

程式碼:
public partial class Form1 : Form
{
Region CheckRegion;
Point MouseLocation;
IdentifyPicture myIDPicture;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckRegion = new Region();
MouseLocation = new Point(0, 0);
myIDPicture = new IdentifyPicture(pictureBox_CP.Size, 10);
pictureBox_CP.Image = myIDPicture.IdPicture;
CheckRegion = myIDPicture.IdRegion;
}

private void button_NewPicture_Click(object sender, EventArgs e)
{
myIDPicture = new IdentifyPicture(pictureBox_CP.Size, 10);
pictureBox_CP.Image = myIDPicture.IdPicture;
CheckRegion = myIDPicture.IdRegion;
label_Result.Text = "?";
}
private void pictureBox_CP_Click(object sender, EventArgs e)
{
if (CheckRegion.IsVisible(MouseLocation))
label_Result.Text = "OK";
else
label_Result.Text = "?";
}
private void pictureBox_CP_MouseMove(object sender, MouseEventArgs e)
{
MouseLocation = e.Location;
}
private void pictureBox_CP_MouseLeave(object sender, EventArgs e)
{
MouseLocation = new Point(0, 0);
}

}

提供大家參考(重點用粗體字表示)...
若有錯請不吝指教...
有機會在改成.Net 控制項(Control)...

0 個回應:

MSN狀態(我在線上時,可以跟我交談喔)