歡迎跟我連絡

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

2009年7月31日 星期五

認證登錄系統圖片產生 .Net 控制項 (續)

下面圖片中的Circle或是Pie的圖形邊緣都有鋸齒出現不怎麼美觀.


找到IdentifyPictureControl.cs中這個DrawANewPicture() Function
加入下面粗體字那行,修正後就可以美觀一點了...
//Private Function
private void DrawANewPicture()
{
myPictureSize = this.Size;
GraphicsPath myGP = new GraphicsPath();
myBitmap = new Bitmap(this.Width, this.Height);
Graphics myG = Graphics.FromImage(myBitmap);
//GDI+ 修飾用屬性
myG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
myG.Clear(Color.White);
....

修正後,看起來舒服多了...

母親給出的答案

有個孩子對一個問題一直想不通:為什麼他的同桌的同學想考第一名就考上第一,而自己想考第一卻才考了全班第二十一名?

回家後他問道:"媽媽我是不是比別人笨?我覺得我和他一樣聽老師的話,一樣認真地做作業,可是,為什麼我總是比落後他?"媽媽聽了兒子的話,感覺到兒子開始有自 尊心了,而這種自尊心正在被學校的排名傷害著。她望著兒子,沒有回答,因為她不知道該怎樣回答。

又一次考試後,孩子考了第十七名,而他的同學還是第一名。回家後,兒子又問了同樣的問題。她真想說,人的智力確實有三六九等,考第一的人,腦子就是比一般的人靈活。然而這樣的回答,難道是孩子真想知道的答案嗎?她慶幸自己沒說出口。

應該怎樣回答兒子的問題呢?有幾次,她真想重覆那幾句被上萬個父母重覆了上萬次的話--你太貪玩了,你在學習上還不夠勤奮,和別人比起來還不夠努力……以此來搪塞兒子。然而,像她兒子這樣腦袋不夠聰明,在班上成績不甚突出的孩子,平時活得還不夠辛苦嗎?所以她沒有那麼做,她想為兒子的問題找到一個完美的答案。

兒子小學畢業了,雖然他比過去更加努力,但依然沒有趕上他的同學,不過與過去相比,他的成績一直在進步。為了對兒子的進步表示讚賞,她帶他去看了一次大海。就是在這次旅行中,這位母親回答了兒子的問題。
現在這位兒子再也不擔心自己的名次了,也再沒有人追問他小學時成績排第幾名,因為他已經以全校第一名的成績考上了大學。放寒假回來時,母校請他給同學及家長們做一段演說。其中他講小時候的一段經歷:「我和母親坐在沙灘上,她指著前面對我說,你看那些在海邊爭食的鳥兒,當海浪打來的時候,小麻雀總能迅速地起飛,牠們拍打兩三下翅膀就升入天空;而海鷗總顯得非常笨拙,牠們從沙灘飛入天空總要很長時間,然而,真正能飛越大海橫過大洋的還是海鷗。」這場演說使很多母親流下了眼淚,其中包括他自己的母親。
這位母親從不說一些令孩子洩氣的話,在找不到適當的答案前寧可沉默,以自身之受去支持孩子的一步步成長,孩子在這樣寬容的環境下,最後交出優秀的成績。

教育,是對生命個體的尊重和喚醒,是對人的內在潛能的開發和拓展,讓孩子健康地生長,需要一種平和的心境,一種智慧的胸襟,一種獨特的魅力,這一切必須以寬容為基礎!


雨季說:
一段刻苦銘心的話,可以改變聽的懂的人的一生...
但是絕大部分,都是對牛彈琴 聽者無心

2009年7月30日 星期四

認證登錄系統圖片產生 .Net 控制項

繼續上一篇文章,將Class改寫為.Net控制項

新增一個Event,如果USER點選到圓圈(Circle),會產生Identify_OK Event.
新增一個屬性SharpNumber,可以設定圖形的複雜度.
新增一個Method GetNext(),可以重新繪製新的認證圖片.

程式碼:
1.IdentifyPictureControl.cspublic partial class IdentifyPictureControl : UserControl
{
//Private Variable
private Random myRandom = new Random();
private int mySharpNo;
private Size myPictureSize;
private Bitmap myBitmap;
private Region myRegion;
private Point myMouseLocation;
public int SharpNumber
{
set { mySharpNo = value; }
get { return mySharpNo; }
}
public IdentifyPictureControl()
{
InitializeComponent();
mySharpNo = 20;
myRegion = new Region();
myMouseLocation = new Point(-1, -1);
}

//Event
public event EventHandler Identify_OK;
protected virtual void OnIdentify_OK(EventArgs e)
{
if (Identify_OK != null)
{
Identify_OK(this, e);
}
}

//Private Component Event
private void IdentifyPictureControl_Load(object sender, EventArgs e)
{
DrawANewPicture();
this.BackgroundImage = myBitmap;
}
private void IdentifyPictureControl_Click(object sender, EventArgs e)
{
if (myRegion.IsVisible(myMouseLocation))
{
OnIdentify_OK(EventArgs.Empty);
}

}
private void IdentifyPictureControl_MouseMove(object sender, MouseEventArgs e)
{
myMouseLocation = e.Location;
}
private void IdentifyPictureControl_MouseLeave(object sender, EventArgs e)
{
myMouseLocation = new Point(-1, -1);
}

//Private Function
private void DrawANewPicture()
{
myPictureSize = this.Size;
GraphicsPath myGP = new GraphicsPath();
myBitmap = new Bitmap(this.Width, this.Height);
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/2; 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/2; 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 = myRandom.Next(0, PictureSize.Width - SharpWidth - 3); //3 Pen Width
int Y = myRandom.Next(0, PictureSize.Height - SharpWidth - 3); //3 Pen Width
return new Point(X, Y);
}
private int GetSize(int PictureWidth)
{
return (int)(PictureWidth / myRandom.Next(2, 6));
}
private Color GetColor()
{
switch (myRandom.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)(myRandom.Next(10, 350));
}

//Public method
public void GetNext()
{
DrawANewPicture();
this.BackgroundImage = myBitmap;
}
}

2.IdentifyPictureControl.Designer.cs
partial class IdentifyPictureControl
{
///


/// 設計工具所需的變數。
///

private System.ComponentModel.IContainer components = null;

///
/// 清除任何使用中的資源。
///

/// 如果應該公開 Managed 資源則為 true,否則為 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 元件設計工具產生的程式碼

///
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
///

private void InitializeComponent()
{
this.SuspendLayout();
//
// IdentifyPictureControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Name = "IdentifyPictureControl";
this.Size = new System.Drawing.Size(148, 148);
this.Load += new System.EventHandler(this.IdentifyPictureControl_Load);
this.MouseLeave += new System.EventHandler(this.IdentifyPictureControl_MouseLeave);
this.Click += new System.EventHandler(this.IdentifyPictureControl_Click);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.IdentifyPictureControl_MouseMove);
this.ResumeLayout(false);

}

#endregion

}

範例引用程式碼:
private void button1_Click(object sender, EventArgs e)
{
identifyPictureControl1.SharpNumber = 30;
identifyPictureControl1.GetNext();
}


private void identifyPictureControl1_Identify_OK(object sender, EventArgs e)
{
MessageBox.Show("OK");
}

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

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



今天利用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)...

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