發表文章

目前顯示的是 2014的文章

如何讓Restful Service回應圖片

在一般利用WCF還做restful的服務,回應的部分可以指定要輸出為XML格式或是JSON格式,那麼如果說想要直接回應一張圖片的時候應該要怎麼進行呢? 手上是用WPF的應用程式,先Self Host一個restful service上去,之後來處理回應圖片的部分,下面是參考資訊來源 Self host http://stackoverflow.com/questions/20805317/how-to-set-up-wcf-self-hosted-rest-service Response image http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/fetching-image-using-wcf-rest-service/ http://stackoverflow.com/questions/12781173/cant-return-image-from-wcf-rest-service 主要的方式便是將圖片轉換成Stream,之後http這邊設定回應的類型是image,這樣就可以達成想要的樣子了,下面來看看程式碼的部分吧 Interface     [ ServiceContract ] interface   IMyService {     [ OperationContract ]     [ WebInvoke (UriTemplate  =   "/GetImage" , Method  =   "GET" ,         RequestFormat  =   WebMessageFormat . Xml, ResponseFormat  =   WebMessageFormat . Xml,         BodyStyle  =   WebMessageBodyStyle . Bare)]      Stream  GetImage(); } 很簡單的只有一個GetImage的Method 主程式這邊在Loaded事件中,去執行Self Host的相關動作 private   void  StartHost() {      Uri  httpUrl  =   new   Uri ( string . Fo

如何列出系統中的印表機,以及指定印表機來做列印

這篇筆記一下小技巧,如果在軟體中,想要列印東西到指定的印表機上,可以利用 PrinterSettings.PrinterName 的屬性來做,很簡單的就可以指定要使用的印表機,那麼如果要知道系統中所有已經安裝的印表機名稱,則可以用 PrinterSettings.InstalledPrinters 來做,下面是簡單的測試程式碼 using   System ; using   System . Drawing ; using   System . Drawing . Printing ; using   System . Windows . Forms ; namespace   WindowsFormsApplication1 {      public   partial   class   Form1  :  Form     {          PrintDocument   printer ;          public   Form1 ()         {              InitializeComponent ();              printer   =   new   PrintDocument ();              printer . PrintPage   +=   printer_PrintPage ;              foreach  ( var   item   in   System . Drawing . Printing . PrinterSettings . InstalledPrinters )             {                  cboPrint . Items . Add ( item );             }         }          void   printer_PrintPage ( object   sender ,  PrintPageEventArgs   e )         {              e . Graphics . DrawString ( "12345" ,  new   Font ( "Arial" ,  14 ),  Brush

監控服務(Service)的目前狀態

一般撰寫完Service程式之後,Service都是在背景持續的作業,前景部分是不會看到任何的介面的,那麼就會有一些延伸的需求產生。例如說如何知道目前Service的狀態,或是怎麼從Service中取出一些目前運作的訊息等等;這邊就先來看看要怎麼去取得Service的狀態。 要取得目前服務的狀態其實在實作上非常的簡單,只要利用ServiceController的功能就可以達到了,例如下面這個範例可以列出目前系統中的所有服務以及該服務是否支援『停止』的動作 using  System; using  System . ServiceProcess; using  System . Windows . Forms; namespace  svcMonitor {      public   partial   class   Form1  :  Form     {          ServiceController [] svcList;          public  Form1()         {             InitializeComponent();         }          private   void  btnRefresh_Click( object  sender,  EventArgs  e)         {             svcList  =   ServiceController . GetServices();              foreach  ( var  item  in  svcList)             {                 txtMessage . Text  +=                    item . ServiceName  +   " : "   +  item . Status . ToString()  +   Environment . NewLine;                 txtMessage . Text  +=                     "    "   +  item . ServiceName  +   " : &qu

Visual Studio 2013 update 1

Visual Studio 2013 Update 1 正式版本已經推出了,有使用的朋友們可以參考下面連結進行更新 Reference http://blogs.msdn.com/b/bharry/archive/2014/01/20/vs-2013-1-update-1-is-available.aspx Download link http://www.microsoft.com/zh-tw/download/details.aspx?id=41650 Happy coding !