發表文章

目前顯示的是 4月, 2013的文章

System.Drawing.Bitmap要如何轉換成WPF中可用的ImageSource呢?

在一般情況下,如果我們有一些圖片需要顯示在WPF的應用程式中,通常我們會使用Image,以及指定Image.Source屬性,例如說下面這樣 img1.Source = new BitmapImage(new Uri(@"image file path", UriKind.RelativeOrAbsolute)); 利用這樣的方式,將圖片檔案顯示在Imagez上面;如果來源是byte array的話,會利用類似這樣的方式 System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.EndInit(); img1.Source = bitmapImage; 這樣就可以由byte陣列轉換成WPF中可以使用的圖片來源了,不過上面這段程式碼有個問題需要處理,在memoryStream的部分,上面並沒有看到Dispose的部分,這樣子不會產生一些記憶體耗用的狀況嗎?於是嘗試加上了MemortStream.Dispose的部分之後發現『疑?阿圖片怎麼顯示不出來了』,這個部分請參考一下 Conver

如何取得Serial port的friendly name

圖片
這邊筆記一下,一般情況來說,如果我們想要目前電腦中總共有那些Serialport可以使用的話,最常見的方式會是下面這樣             foreach (string name in SerialPort.GetPortNames())             {                 cboPortName.Items.Add(name);             } 不過如果說我們想要取得像是下圖紅框部分,比較"看的懂"(?) 的名稱的話,應該要怎麼做呢? 作法可以參考一下下面位置的相關程式碼 How to open a serial port by friendly name?