2012-07-26 46 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