Windows Phone 7 如何截取螢幕畫面

  • 4044
  • 0

Windows Phone 7 如何截取螢幕畫面

當我們在開發程式時候要寫使用手冊或是系統錯誤提出bug畫面,大家一定都會想到我們的老朋友PrtScn按鍵在透過小畫家或者是一些剪圖軟體把我們想要畫面剪出來貼到文件中,這樣的動作二次以下不會覺得很累,如果操作畫面很多的時候會覺得天呀是不是該有個方便的軟體來幫助我。所以今天我想要分享一個簡單程式碼用讓大家可以自已去開發取圖的功具。

首先開啟VS2010產生一個ASP.NET網站專案,使用裡面的Default.aspx頁面就可以了,打開Default.aspx.cs在Page_Load函式下加入下列程式碼

byte[] buffer = new byte[(int)Request.InputStream.Length];
            Request.InputStream.Read(buffer, 0, buffer.Length);
            if (buffer.Length > 0)
            {

                string FileName =  DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
                File.WriteAllBytes(FileName, buffer);
                Request.InputStream.Close();
                Response.Clear();
                Response.Write("上傳成功");
                Response.End();
            }

上面的網頁程式碼是給使用者透過WebRequest用POST方式把圖片上傳至Server存放,檔名採用日期時間yyyyMMdHHmmss格式為JPG,存檔結束後會回傳上傳成功字串,接者就把網頁發行到IIS上記得要打開權限要不然存檔會發生錯誤。

接下來網頁發行成功後新增Windows Phone 應用程式專案,在MainPage.xaml 上面隨便放一些UI 拉一個Button來回傳圖片,在Button上按二下進入MainPage.xaml.cs貼入下面程式碼

byte[] buffer;
      private void button1_Click(object sender, RoutedEventArgs e)
      {
         var mem = new MemoryStream();
          WriteableBitmap bmp = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight);
          bmp.Render(this, null);
          bmp.Invalidate();
          bmp.SaveJpeg(mem, bmp.PixelWidth,bmp.PixelHeight , 0, 100);
          HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://192.168.16.140/website/Default.aspx"));
          req.Method = "POST";
          buffer = mem.ToArray();
          mem.Close();
          req.BeginGetRequestStream(new AsyncCallback(RequestCallback), req);
      }
      private void RequestCallback(IAsyncResult result)
      {
          HttpWebRequest req = (HttpWebRequest)result.AsyncState;
         Stream stream = req.EndGetRequestStream(result);
          stream.Write(buffer, 0, buffer.Length);
          stream.Close();
          req.BeginGetResponse(new AsyncCallback(ResponseCallback), req);
      }
      private void ResponseCallback(IAsyncResult result)
      {
          HttpWebRequest req = (HttpWebRequest)result.AsyncState;
          HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(result);
          Stream stream = resp.GetResponseStream();
          StreamReader Read = new StreamReader(stream);
          string responseString = Read.ReadToEnd();
          Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show(responseString); }));
          stream.Close();
          Read.Close();
          resp.Close();

      }

接下來啟動Windows Phone 應用程式按下Button 後在看看伺服器是不是有回傳圖片

image image

image