2012-07-26 36 views
1

我目前正在嘗試製作一個簡單的照片上傳功能,它將上傳我拍攝的截圖。我發現這個網站dumpyourphoto.com,但我真的不明白如何在C#中做到這一點。任何人都可以引導我通過這個?通過C#使用Dumpyourphoto圖片託管

基本上所有我需要的是將截圖照片上傳到服務器,並希望它會返回給我一個該照片的網址。從那裏開始,我將把這個URL上傳到我已經設置的OpenShift數據庫中,並將其上傳爲文本並將鏈接存儲在數據庫中。

對。感謝西蒙的問題。我意識到我沒有提供太多細節。

所以基本上我使用了kinect的截圖,這是我正在使用的功能。

private void btn_ss_Click(object sender, RoutedEventArgs e) 
    { 
     // create a png bitmap encoder which knows how to save a .png file 
     BitmapEncoder encoder = new PngBitmapEncoder(); 

     // create frame from the writable bitmap and add to encoder 
     encoder.Frames.Add(BitmapFrame.Create(this.colorBitmap)); 

     string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", CultureInfo.CurrentUICulture.DateTimeFormat); 

     string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 

     string path = System.IO.Path.Combine(myPhotos, "KinectSnapshot-" + time + ".png"); 

     // write the new file to disk 
     try 
     { 
      using (FileStream fs = new FileStream(path, FileMode.Create)) 
      { 
       encoder.Save(fs); 
      } 

      this.ss_dis.Text = string.Format("{0} {1}", "Screenshot has been taken.", path); 
     } 
     catch (IOException) 
     { 
      this.ss_dis.Text = string.Format("{0} {1}", "Failed to take Screenshot.", path); 
     } 
    } 

,我奮力的是,我從來沒有真正前處理網絡活動,如HttpWebRequest的功能和網站顯示XML和JSON。我對如何做到這一點略有想法,但我不太確定。 這是指向開發者api的鏈接。 http://www.dumpyourphoto.com/information/api

更新:我試圖自己解決問題,但我被困在最後一部分。我不知道如何將bytearray和key附加到HttpWebRequest。

private byte[] imgToByteArray(string _FileName) 
    { 
     byte[] _buffer = null; 

     try 
     { 

      System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
      System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); 

      long _TotalByte = new System.IO.FileInfo(_FileName).Length; 
      _buffer = _BinaryReader.ReadBytes((Int32)_TotalByte); 

      _FileStream.Close(); 
      _FileStream.Dispose(); 
      _BinaryReader.Close(); 
     } 
     catch(Exception _Exception) 
     { 
      Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); 
     } 

     return _buffer; 
    } 

這是Image to ByteArray函數。

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     string imgPath = "C:\\KinectSnapshot-04-46-14.png"; 
     string key = "1d533e9033f9d5b9b509055d8a00932aaf1ace7f"; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.dumpyourphoto.com/api/upload_photo/xml"); 
     string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "KinectSnapshot-" + "03-38-28" + ".png"); 

     byte[] img = imgToByteArray(path); 
     request.Method = "POST"; 
     request.Credentials = CredentialCache.DefaultCredentials; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = img.Length; 
     using(Stream dataStream = request.GetRequestStream()) 
      dataStream.Write(img, 0, img.Length); 

     using (WebResponse response = request.GetResponse()) 
     using(Stream responseStream = response.GetResponseStream()) 
     using (StreamReader reader = new StreamReader(responseStream)) 
     { 
      string responseResults = reader.ReadToEnd(); 
      Console.WriteLine(responseResults); 
     } 
} 

更新:這是我目前的位置。我還有兩個問題。我不知道在哪裏附上密鑰文件和上傳圖片的標題。任何人都可以啓發我嗎?

我真的很感謝任何幫助,我可以得到!

+1

你試過了什麼?什麼?他們網站上的API向您展示了需要完成的工作。你究竟在爲什麼而掙扎? – 2012-07-26 03:02:13

回答

0

您將使用HttpWebRequest使用其API中列出的方法發出POST請求。有什麼具體的事情你正在努力?

+0

是的,很抱歉,我在這方面還很新。看看API文檔,我猜我需要一張照片的字節串,一個鍵和一個標題。到目前爲止,這是我所做的。我將圖像轉換爲字節數組,我嘗試使用HttpWebRequest上傳此文件。我將該方法設置爲已發佈。問題是我不是很清楚如何將關鍵字和bytearray附加到HttpWebRequest中。 – Bocky 2012-07-26 05:10:17

+1

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx解釋得很好 – 2012-07-26 05:35:14

+0

這確實幫助了很多!但是我只剩下兩個小問題了。< – Bocky 2012-07-26 06:13:17