架構圖
使用前必須先Using
System.Threading
System.Threading.Tasks
這兩個NameSpace
簡單範例:
....
Parallel.Invoke(
() => MethodA(),
() => MethodB(),
() => MethodC() );
....
private void MethodA()
{
MessageBox.Show("This is Method A");
}
private void MethodB()
{
MessageBox.Show("This is Method B");
}
private void MethodC()
{
MessageBox.Show("This is Method C");
}
相關文章:
http://msdn.microsoft.com/zh-tw/dd996613.aspx
http://msdn.microsoft.com/en-us/concurrency/default.aspx
http://msdn.microsoft.com/en-us/library/dd460693(VS.100).aspx
有需求或技術問題可以隨時跟我連絡 (MSN上線時)
2009年7月30日 星期四
認證登錄系統圖片產生 .Net 控制項
繼續上一篇文章,將Class改寫為.Net控制項
新增一個Event,如果USER點選到圓圈(Circle),會產生Identify_OK Event.
新增一個屬性SharpNumber,可以設定圖形的複雜度.
新增一個Method GetNext(),可以重新繪製新的認證圖片.
程式碼:
public 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;
}
}
範例引用程式碼:
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");
}
新增一個Event,如果USER點選到圓圈(Circle),會產生Identify_OK Event.
新增一個屬性SharpNumber,可以設定圖形的複雜度.
新增一個Method GetNext(),可以重新繪製新的認證圖片.
程式碼:
public 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;
}
}
範例引用程式碼:
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)...
常見的方式有底下兩種...
今天利用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)...
訂閱:
文章 (Atom)