Posts
[C#]使用ExifLibrary簡易快速的擷取圖片的Exif資訊
namespace WindowsFormsApplication34 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnLoad_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { var photoFile = openFileDialog1.FileName; ExifFile exifFile = ExifFile.Read(photoFile); pbxThumbnail.Image = exifFile.Thumbnail.ToBitmap(); tbxMessage.Clear(); foreach (ExifProperty item in exifFile.Properties.Values) { tbxMessage.Text += string.Format("{0}:{1} “, item.Name, item.Value); } } } } }
read morePosts
[C#]使用Faker.Net輔助建立假的數據資料
namespace ConsoleApplication11 { class Program { static void Main(string[] args) { for (int i = 0; i < 3; ++i) { ShowFakeUserInfo(); Console.WriteLine(new String(’=’, 50)); } }
static void ShowFakeUserInfo() { Console.WriteLine(String.Format("User: {0}", Faker.Name.FullName())); Console.WriteLine(String.Format("Phone: {0}", Faker.Phone.Number())); Console.WriteLine(String.Format("Email1: {0}", Faker.Internet.Email())); Console.WriteLine(String.Format("Email2: {0}", Faker.Internet.FreeEmail())); Console.WriteLine(String.Format("Company: {0}", Faker.Company.Name())); Console.WriteLine(String.Format("Country: {0}", Faker.Address.Country())); } } }
read morePosts
[C#]使用FindFirstFile、FindNextFile API實做EnumerateFiles
[DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] private static extern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData); [DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] private static extern bool FindNextFile(IntPtr hndFindFile, ref WIN32_FIND_DATA lpFindFileData); [DllImport( "kernel32.dll" , SetLastError = true )] private static extern bool FindClose(IntPtr hndFindFile); [Serializable, StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto), BestFitMapping(false)] internal struct WIN32_FIND_DATA { public FileAttributes dwFileAttributes; public uint ftCreationTime_dwLowDateTime; public uint ftCreationTime_dwHighDateTime; public uint ftLastAccessTime_dwLowDateTime; public uint ftLastAccessTime_dwHighDateTime; public uint ftLastWriteTime_dwLowDateTime; public uint ftLastWriteTime_dwHighDateTime; public uint nFileSizeHigh; public uint nFileSizeLow; public int dwReserved0; public int dwReserved1; [MarshalAs(UnmanagedType.
read morePosts
[C#]使用InternetGetConnectedState API偵測目前電腦網路的連線狀態
namespace ConsoleApplication21 { class Program { [DllImport(“wininet”)] public static extern bool InternetGetConnectedState( ref uint lpdwFlags, uint dwReserved );
static void Main(string[] args) { uint flags = 0x0; var isNetworkAvailable = InternetGetConnectedState(ref flags, 0); Console.WriteLine(string.Format("Network available: {0} ({1})", isNetworkAvailable.ToString(), flags.ToString())); } } }
read morePosts
[C#]使用Microsoft Translator Soap API實作翻譯功能
if (String.IsNullOrEmpty(text)) return DEFAULT_DETECTED_LANG; const string DETECT_API_URI_PATTERN = "http://api.microsofttranslator.com/V2/Http.svc/Detect?appId={0}&text={1}"; const string MATCH_PATTERN = "<[^<>]*>([^<>]*)<[^<>]*>"; WebRequest req = WebRequest.Create(String.Format(DETECT_API_URI_PATTERN, APP_ID, text)); WebResponse resp = req.GetResponse(); string local = DEFAULT_DETECTED_LANG; using (StreamReader reader = new StreamReader(resp.GetResponseStream())) { local = reader.ReadToEnd(); local = Regex.Match(local, MATCH_PATTERN).Groups[1].Value; } return local; }</pre> if (!langs.Contains(lang)) lang = "en"; return client.Speak(APP_ID, tbxSource.Text, lang, "audio/wav", string.Empty); }</pre> var langNames = (from lang in langs.AsParallel() select new { Name = Client.
read morePosts
[C#]使用Mutex實現單一程式執行個體的注意事項
namespace WindowsFormsApplication10 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);
Boolean bCreatedNew; //Create a new mutex using specific mutex name Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew); if (bCreatedNew) Application.Run(new Form1()); } } }
//Create a new mutex using specific mutex name Mutex m = new Mutex(false, "myUniqueName", out bCreatedNew); GC.Collect(); if (bCreatedNew) Application.
read more