// Created by vince
// Copyright (c) 2014年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegate_test
{
//delegate type
delegate void myDelegate(string name);
class Program
{
static void Main(string[] args)
{
invokeDelegate(man, "vince");
invokeDelegate(lady, "Apple");
}
//using delegate to call method
static void invokeDelegate(myDelegate hello, string name)
{
hello(name);
}
//method
static void man(string name)
{
Console.WriteLine("Hi, Mr."+name);
}
static void lady(string name)
{
Console.WriteLine("Hey, Ms."+name);
}
}
}
Google Code Prettify
2014年11月7日 星期五
C# delegate(委派) 使用教學
2013年12月8日 星期日
C# 取得現在時間 display current time
最近在程式中要取得現在的時間帶毫秒,發現沒有用過,
於是決定做一個備忘錄:
時間
|
輸入
|
結果
|
年
|
y
|
13
|
年
|
yyy
|
2013
|
月
|
M
|
12
|
月(國字)
|
MMM
|
十二月
|
日
|
d
|
8
|
星期
|
ddd
|
周日
|
小時
|
h
|
11
|
小時(24小時制)
|
H
|
23
|
上午/下午
|
tt
|
下午
|
分
|
m
|
45
|
秒
|
s
|
35
|
毫秒
|
f
|
7
|
輸入位數為顯示最少位數,
如月分 M -> 12
年輸入yyyyy 會得到 02013
CODE:
// Created by vince
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;// for CultureInfo
namespace datetime_tutorial
{
class Program
{
static void Main(string[] args)
{
var time1 = DateTime.Now.ToString();
var time2 = DateTime.Now.ToString("y-M-d (ddd) t h:m:s.f", CultureInfo.InstalledUICulture);
var time3 = DateTime.Now.ToString("yyyy-MMM-dd (ddd) tt H:m:s:fff", CultureInfo.InstalledUICulture);
var time4 = DateTime.Now.ToString(DateTimeFormatInfo.CurrentInfo.FullDateTimePattern);
Console.WriteLine(time1);
Console.WriteLine("\n");
Console.WriteLine(time2);
Console.WriteLine("\n");
Console.WriteLine(time3);
Console.WriteLine("\n");
Console.WriteLine(time4);
Console.WriteLine("\n");
}
}
}
結果顯示:
2013年11月19日 星期二
C# visual studio 2012 EmuCV 安裝教學 與 Emgu.CV.CvInvoke 解決
EmguCV 是從 OpenCV裡包成專給 C#用的一個函式庫,
功能可以說是非常強大,可以稱得上是目前用C#學影像處理必學的 open source
可惜的是它的語法方式比較像是舊版OpenCV C-Style的風格改編,
如果一開始就是學C++-Style的人在上手方面可能會覺得比較陌生,
不過還是大力推薦它!!
編譯環境
windows7 64bit
emgucv 2.4.9 beta
visual studio 2112 express
1.先下載 EmguCV 函式庫
2.在我的電腦裡設定環境變數
在解壓縮資料夾中,找到:
..\EmguCV\bin
..\EmguCV\bin\x64 (for 64位元)
..\EmguCV\bin\x86 (for 32位元)
3.在專案上增加參考路徑(reference path)
在專案上按右鍵,選擇屬性(property) ,把bin資料夾內要使用的版本資料夾加進來
4.加入參考 dll
在專案上按右鍵,Add reference
5.加入UI Tool
在工具(tool) -> Choose Tool Items
加入Emgu.CV.UI.dll
加入後可以在 form 的工具列看到多了幾樣UI 選擇
6.開始編譯
到這邊就已經完成囉,這邊示範的程式是
按下open按鈕,就可以開啟對話窗選擇圖片,把它讀進 pictureBox 裡
Code:
// Created by vince
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV; //for emguCV
using Emgu.CV.Structure; //for Bgr
namespace c_sharp_emgucv_setup
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog filename = new OpenFileDialog();
if (filename.ShowDialog() == DialogResult.OK)
{
string name = filename.FileName;
Image img = new Image(name);
pictureBox1.Image = img.ToBitmap();
}
}
}
}
7.執行結果
看到圖就代表成功囉!!
8.特殊情況
有些人會在編譯成功後,執行時跳出
Emgu.CV.CvInvoke 的錯誤訊息,
可以試著把 ..bin\x64 (64 位元) 或 ..bin\x86 (32 位元) 資料夾中所有相關的
opencv_core249.dll
opencv_imgproc249.dll
......
等相關opencv_ 開頭的dll 直接丟進編譯後產生出來的debug 或release資料夾,
再執行一次應該就可以了!!(我在windows7 32bit 的電腦裡有實際用過,有成功)
2013年11月18日 星期一
C# SQLite 教學 SQLite tutorial
SQLite 是一個功能強大的資料庫 open source,而且安裝空間小,
是我目前最常用的資料庫 lib ,輕巧方便攜帶性高,
美中不足的地方是目前 C# 版只有支援到 .Net 3.5 和 .Net 2.0
編譯環境
windows7 64bit
.Net Framework 3.5
visual studio 2012 express
1.下載 ADO.NET 2.0 Provider for SQLite 並安裝它
2.引用 System.Data.SQLite.dll
32 bit : ../bin/System.Data.SQLite.dll
64 bit: ../bin/x64/System.Data.SQLite.dll
3.Code
// Created by vince
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;// for check file exist
using System.Data.SQLite;//for SQLite
namespace c_sharp_sqlite
{
public partial class Form1 : Form
{
private SQLiteConnection sqlite_connect;
private SQLiteCommand sqlite_cmd;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!File.Exists(Application.StartupPath + @"\myData.db"))
{
SQLiteConnection.CreateFile("myData.db");
}
string name = textBox1.Text;
string phone_number = textBox2.Text;
sqlite_connect = new SQLiteConnection("Data source=myData.db");
//建立資料庫連線
sqlite_connect.Open();// Open
sqlite_cmd = sqlite_connect.CreateCommand();//create command
sqlite_cmd.CommandText = @"CREATE TABLE IF NOT EXISTS phone (num INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, phone_number TEXT)";
//create table header
//INTEGER PRIMARY KEY AUTOINCREMENT=>auto increase index
sqlite_cmd.ExecuteNonQuery(); //using behind every write cmd
sqlite_cmd.CommandText = "INSERT INTO phone VALUES (null, '" + name + "','" + phone_number + "');";
sqlite_cmd.ExecuteNonQuery();//using behind every write cmd
sqlite_connect.Close();
}
private void button2_Click(object sender, EventArgs e)
{
sqlite_connect = new SQLiteConnection("Data source=myData.db");
//建立資料庫連線
sqlite_connect.Open();// Open
sqlite_cmd = sqlite_connect.CreateCommand();//create command
sqlite_cmd.CommandText = "SELECT * FROM phone"; //select table
SQLiteDataReader sqlite_datareader = sqlite_cmd.ExecuteReader();
while (sqlite_datareader.Read()) //read every data
{
String name_load = sqlite_datareader["name"].ToString();
String phone_number_load = sqlite_datareader["phone_number"].ToString();
MessageBox.Show(name_load + ":" + phone_number_load);
}
sqlite_connect.Close();
}
}
}
5.執行結果write:
read:
可以到release資料夾裡,會發現建立的myData.db跑出來了喔,
如果想要看它裡面的內容,可以參考這篇 : SQLite Database Browser 可以看資料庫的.db檔案
C# 用 ZXing 產生 QR Code QR Code generator using ZXing
最近在產生QR code時發現了一個不錯的open source,
就是 ZXing.Net
它可以支援 .net2.0, 3.5, 4.0, 4.5, 而且又是免費的,
程式碼也非常簡單喔!
網路上有很多分享都是 using com.zxing,好像在現在新的版本已經不適用了
編譯環境:
windows7 64bit
.Net Framework 4.5
visual studio 2012 express
ZXing.Net.0.12.0.0
1.下載 ZXing.Net
2.把DLL加到專案中
依造自己的 .Net Framework 來選擇 DLL
3.Code
// Created by vince
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing; // for BarcodeWriter
using ZXing.QrCode; // for QRCode Engine
namespace qrcode_tutorial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var writer = new BarcodeWriter //dll裡面可以看到屬性
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions //設定大小
{
Height = 300,
Width = 300,
}
};
pictureBox1.Image = writer.Write(textBox1.Text); //轉QRcode的文字
}
}
}
4.執行結果
2013年11月17日 星期日
C# 開啟 outlook 與網頁 open outlook and website
在做網頁的開啟或 outlook 的呼叫時,
System.Diagnostic 的命名空間中裡的 Process 類別,可以簡單的幫我們處理
編譯環境
windows7 64bit
.Net Framework4.5(測到.Net3.5都能用)
visual studio 2012 express
Code:
// Created by vince
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics; //使用process需增加
namespace c_sharp_outlook_tutorial_and_website
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string mailInfo = string.Format("mailto:{0}?Subject={1}&Body={2}", "vince@mail.com", "test", "hello!");
Process myProcess = new Process();
myProcess.StartInfo.FileName = mailInfo.ToString();
myProcess.Start(); //執行
myProcess.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("www.google.com.tw"); //一行開啟網站
}
}
}
2013年11月14日 星期四
C# 取得電腦上所有serial port
Mac Air本身沒有serial port可以使用,而剛好最近在玩Arduino開發版,
剛好可以用 C# 來跟我的 Arduino 通訊
不過這邊先簡單介紹怎麼取得電腦上所有的 serial port,
順便複習一下combobox 和 listbox
首先這邊先建立Windows Form Application
介面就簡單如圖所示:
// Created by vince
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; //要使用serial port需要引用它
namespace c_sharp_searchSerialPort
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string [] myPorts = SerialPort.GetPortNames(); //取得所有port的名字的方法
comboBox1.DataSource = myPorts; //直接取得所有port的名字
listBox1.DataSource = myPorts;
foreach (string port in myPorts) //使用迴圈方式取得所有port的名字
{
comboBox2.Items.Add(port); //一個一個增加
listBox2.Items.Add(port);
}
}
}
}
結果如下,可以發現直接指定所有port給combobox1(左邊),它會有預設的port跑出來,
而用迴圈添加的port給combobox2(右邊),它預設是空白,
直接指定所有port給listbobox1(左邊),它會預設選取 port 的名稱
2013年11月11日 星期一
C# webcam snapshot and save image to bitmap using aforge
在WINDOWS下做WEBCAM 影像的擷取,比較常見的就是用DirectShow或EmguCV,
最近突然發現有一個還不錯的open souce可以擷取webcam影像,而且大小不到50mb
它就是 aforge,這次就決定用它來示範怎麼做影像擷取與存圖!
編譯環境:
windows 7
visual studio 2012 express
aforge .net Framework-2.2.5
1.到 aforge 官網下載最新版.net Framework 並安裝
2.開啟新專案,選擇 Windows Form Application
PROJECT -> Add Reference -> Browse
到 AForge.NET 的安裝目錄下,AForge.NET\Framework\Release,
將 AForge.Video.DirectShow.dll 和 AForge.Video.dll 加進來
4.UI介面設計
5.Code
// Created by vince on 13/11/10.
// Copyright (c) 2013年 vince. All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video; //記得加入1
using AForge.Video.DirectShow; //記得加入2
namespace aforge_webcam_tutorial
{
public partial class Form1 : Form
{
public VideoCaptureDevice cam = null; //global
public FilterInfoCollection usbCams; //global
Bitmap image_from_cam; //global
public Form1()
{
InitializeComponent();
}
void got_frame(object sender, NewFrameEventArgs eventArgs)
{
image_from_cam = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = image_from_cam;
}
private void startButton_Click(object sender, EventArgs e)
{
usbCams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
cam = new VideoCaptureDevice(usbCams[0].MonikerString);
cam.NewFrame += new NewFrameEventHandler(got_frame);//註冊事件(got_frame)
cam.Start();
}
private void stopButton_Click(object sender, EventArgs e)
{
cam.Stop();
}
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog(this) == DialogResult.Cancel) //按下取消鍵時返回
{
return;
}
image_from_cam.Save(sfd.FileName);//存圖,副檔名記得給他.bmp
}
}
}
2013年10月2日 星期三
C# using gmail to send email 用gmail信箱寄信
編譯環境
visual C# 2010 express
.net framework 4.0(可以向下相容)
新建專案->選擇console application
code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail; //記得要加這個命名空間
namespace send_google_email_example
{
class Program
{
static void Main(string[] args)
{
System.Net.Mail.SmtpClient my_mail = new
System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
//建立 SmtpClient 物件 並設定 Gmail的smtp主機及Port,如果用gmail 這邊直接用預設,
不用修改
my_mail.Credentials = new System.Net.NetworkCredential("自己的gmail信箱",
"自己的密碼"); //輸入自己的gmail信箱和密碼
my_mail.EnableSsl = true;//Gmial 的 smtp 使用 SSL,直接用預設,不用修改
my_mail.Send("發送時自己顯示的名字<testtest@gmail.com>" , "收件者的名字<hellohello@ntu.edu.tw>" , "信箱標題" , "信件內容"); //發送Email
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail; //記得要加這個命名空間
namespace send_google_email_example
{
class Program
{
static void Main(string[] args)
{
System.Net.Mail.SmtpClient my_mail = new
System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
//建立 SmtpClient 物件 並設定 Gmail的smtp主機及Port,如果用gmail 這邊直接用預設,
不用修改
my_mail.Credentials = new System.Net.NetworkCredential("自己的gmail信箱",
"自己的密碼"); //輸入自己的gmail信箱和密碼
my_mail.EnableSsl = true;//Gmial 的 smtp 使用 SSL,直接用預設,不用修改
my_mail.Send("發送時自己顯示的名字<testtest@gmail.com>" , "收件者的名字<hellohello@ntu.edu.tw>" , "信箱標題" , "信件內容"); //發送Email
}
}
}
2013年9月26日 星期四
C# IPCAM viewer
1.編譯環境
visual C# 2010 express
.net framework 4.0
2.使用IPCAM廠牌
AVTECH AVN813
3.建立windows form project
建立projectau->選擇windows form
toolbox內拉出picturebox(預設名字為pictureBox1)
toolbox內拉出timer->property->
enable選為true
interval time 是每次截取的時間,可依fps自行修改
4.emample code
visual C# 2010 express
.net framework 4.0
2.使用IPCAM廠牌
AVTECH AVN813
3.建立windows form project
建立projectau->選擇windows form
toolbox內拉出picturebox(預設名字為pictureBox1)
toolbox內拉出timer->property->
enable選為true
interval time 是每次截取的時間,可依fps自行修改
4.emample code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; //一定要有 bitmap格式要用到
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace testIPcam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//ipcam URL
string sourceURL ="http://192.168.1.10:88/cgi-bin/guest/Video.cgi?media=JPEG";
// create HTTP request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL);
//add id,password
req.Credentials = new NetworkCredential("admin", "admin");
// get response
WebResponse resp = req.GetResponse();
// get response stream
Bitmap bmp = new Bitmap(resp.GetResponseStream());
//show image in picturebox
pictureBox1.Image = bmp;
}
}
}
訂閱:
文章 (Atom)






















