有需求或技術問題可以隨時跟我連絡 (MSN上線時)

2009年8月12日 星期三

認證登錄系統圖片產生 .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);
....

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


2 8 10 16進位制轉換

UI Layout如下圖所示:

重要程式碼:
namespace 進制轉換
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox_From.SelectedIndex = 0;
comboBox_To.SelectedIndex = 2;
textBox_Source.Text = "0";
}

private void button1_Click(object sender, EventArgs e)
{
textBox_Result.Text = Convert2UrBase(textBox_Source.Text, int.Parse(comboBox_From.SelectedItem.ToString()), int.Parse(comboBox_To.SelectedItem.ToString())); }
public string Convert2UrBase(String Input_Value, int FromBase, int ToBase)
{
long RtnValue;
try
{
RtnValue = Convert.ToInt64(Input_Value, FromBase);
}
catch (Exception other)
{
RtnValue = 0;
}
return Convert.ToString(RtnValue, ToBase);
}

}
}

中華民國身分證驗證程式碼

驗證規則,如下圖所示



C#程式碼:
public bool CheckIdentificationId(string Input_ID)
{
bool IsTrue = false;
if (Input_ID.Length == 10)
{
Input_ID = Input_ID.ToUpper();
if (Input_ID[0] >= 0x41 && Input_ID[0] <= 0x5A)
{
int[] Location_No = new int[] { 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21, 22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33 };
int[] Temp = new int[11];
Temp[1] = Location_No[(Input_ID[0]) - 65] % 10;
int Sum = Temp[0] = Location_No[(Input_ID[0]) - 65] / 10;
for (int i = 1; i <= 9; i++)
{
Temp[i + 1] = Input_ID[i] - 48;
Sum += Temp[i] * (10 - i);
}
if (((Sum % 10) + Temp[10]) % 10 == 0)
{
IsTrue = true;
}
}
}
return IsTrue;
}

應用範例:
bool Result = CheckIdentificationId(textBox_ID.Text);

修改字串編碼方式(Encoding)

有時候會出現顯示會出現亂碼,通常是編碼方式出了問題.
我們可以理用Encoding來解決這個問題...
範例程式:
string Source = "轉換編碼方式範例字串";
byte[] Target_BIG5 = Encoding.Default.GetBytes(Source ); //將字串轉為byte[], 中文版Default就是指BIG5編碼
byte[] Target_UTF8 = Encoding.Convert(Encoding.Default, Encoding.UTF8, Target_BIG5 );//轉碼Encoding.Convert(Source Encoder,Target Encoder , Source Variable)
MessageBox.Show(Encoding.UTF8.GetString(Target_UTF8 ));//顯示轉為UTF8後的字串

C# 3.0提供Dictionary新的宣告法

這是3.0以前的宣告方式
Dictionary myDictionary = new Dictionary();
myDictionary.Add("Candy", "住址wwwwww");
myDictionary.Add("Jony", "住址xxxxx");
myDictionary.Add("Mary", "住址yyyyy");
myDictionary.Add("Nelson", "住址zzzzz");


3.0起,強化了Collection Initializer,所以你可以改成這樣的宣告
Dictionary dctNewWay = new Dictionary() {
{"Candy", "住址wwwwww"},
{"Jony", "住址xxxxx"},
{"Mary", "住址yyyyy"},
{"Nelson", "住址zzzzz"}
};

這樣的方式
就像是宣告固定元素的陣列
string[] NameList = { "Candy", "Jony", "Mary", "Nelson" };