Skip to main content

Posts

Showing posts with the label C#

Compute Running (Execution) Time or Time Difference in C#

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CalculateTimeDifference { class Program { static void Main( string [] args) { string result; // start time of this program DateTime dStart = DateTime.Now; result = string .Format(" Start: Year:{0}, Month:{1}, Day:{2}, Hour:{3}, Minute:{4}, Second:{5}, Millisecond:{6}, In Ticks:{7} ", dStart.Year, dStart.Month, dStart.Day, dStart.Hour, dStart.Minute, dStart.Second,dStart.Millisecond, dStart.Ticks); Console.WriteLine(result); Console.ReadLine(); // wait until you press enter // end time of this program DateTime dEnd = DateTime.Now; result = string .Format(" End : Year:{0}, Month:{1}, Day:{2}, Hour:{3}, Minute:{4}, Second:{5}, Millisecond:{6}, In Ticks:{7} ", dStart.Year, dStart.Month, dEnd.Day, dEnd.Hour, dEnd.Minute, dEnd.Second, dEn...

Read text file using C#’s StreamReader

1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: using System.IO; 6: 7: namespace ConsoleApplication1 8: { 9: class Program 10: { 11: static void Main( string [] args) 12: { 13: // text file to read 14: string fileName = " YourFileName.extension "; 15: StreamReader sr = new StreamReader(fileName, Encoding.Default); 16: string tempS = " "; 17: while (sr.EndOfStream != true ) 18: { 19: // read a line a time 20: tempS = sr.ReadLine(); 21: // print each line on console 22: Console.Out.WriteLine(tempS); 23: } 24: } 25: } 26: }

C# Color.Equals() Color.ToArgb() 的小問題

Color tC; Bitmap tBitmap ; tBitmap = new Bitmap(100,100); BitmapDrawLine(tBitmap, new Point(49,49), new Point(51,51), Color.Gold, 3.0); tC =tBitmap.GetPixel(50, 50); // right if (tC.ToArgb() == Color.Gold.ToArgb()){ Console.Out.WriteLine("Find Gold"); // if( tC.Equals( Color.Gold) ){;} // This will not work //but currently don't know why  } public void BitmapDrawLine(Bitmap bM, Point sPoint, Point ePoint, Color lineColor,float lineThickness)         {             Graphics g = Graphics.FromImage(bM);             Pen myPen = new Pen(lineColor, lineThickness);             myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;             myPen.SetLineCap(System.Drawing.Drawing2D.Lin...

C#: call by reference using "ref"

public void ChangeValue( ref uint cB1 , ref uint c12, ref uint c23){     // change value through reference     cB1 = 10;     cB2 = 20;     cB3 = 30; }   public void MainFunction(){     uint cxxB1, cxxB2, cxxB3;     cxxB1 = 0;     cxxB2 = 0;     cxxB3 = 0;     // pass the reference to the method     ChangeValue( ref cxxB1 , ref cxxB2, ref cxxB3);    // print out the value after change     Console.Out.WriteLine("cxxB1 = " + cxxB1); // 10     Console.Out.WriteLine("cxxB2 = " + cxxB2); // 20     Console.Out.WriteLine("cxxB3 = " + cxxB3); // 30 }

在 C# 的 Bitmap 上畫線的時候,避免線條、連接處有缺角或破碎的方法

public void BitmapDrawLine(Bitmap bM, Point sPoint, Point ePoint, Color lineColor,float lineThickness) {     Graphics g = Graphics.FromImage(bM);     Pen myPen = new Pen(lineColor, lineThickness);         myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;     // 設定線條的端點及連接樣式     myPen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round,     System.Drawing.Drawing2D.LineCap.Round,     System.Drawing.Drawing2D.DashCap.Round);     g.DrawLine(myPen, sPoint, ePoint); }

C# Form 裡面使用 Timer

/* 之前使用 System.Timers; 但是在關掉視窗的時候好像怪怪的,不知到還需要都做什麼動作才可以。改用 System.Windows.Forms.Timer 就好多了,果然是為了 Form customized 的 Timer ... */ using System.Windows.Forms;   // 宣告 Timer System.Windows.Forms.Timer aTimer;   // new 給他一個實體 aTimer = new System.Windows.Forms.Timer();   // 在接收 event 的 List 上加入你的函式 aTimer.Tick += new EventHandler(TimerEventProcessor);   // 設定 Timer 觸發的間隔 aTimer.Interval = 5 * 1000;  // 5 秒  = 5000 毫秒   // 開始計時 aTimer.Start();  // 停掉 Timer 請用 Stop()   // 你的函式,把你在 Timer 每次發生Event 的時候想做的事寫在裡面 private void TimerEventProcessor(object myObject, EventArgs myEventArgs){     // do what you want when time out     // 想做的事 }