By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How to upload an image in C# Smart device Application

P: 14
I need to develop a smart device application to upload an image to my web FTP server using C#.net. But the required classes are not available in the System.Net reference as like in the normal windows applications. What should I do ?
Aug 19 '12 #1
Share this Question
Share on Google+
3 Replies


PsychoCoder
Expert Mod 100+
P: 465
You're going to need to use the WebClient class. here's a simple example:

Expand|Select|Wrap|Line Numbers
  1. public static void SendRequest(System.Text.StringBuilder sReq, byte[] sbyteData, Action<PicData, int> onEventResponse = null, Action onFinally = null)
  2. {
  3.     var client = new WebClient();
  4.  
  5.     client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(WcOpenWriteCompleted);
  6.     var uri = new Uri(sReq.ToString());
  7.     client.Headers[HttpRequestHeader.ContentLength] = sReq.Length.ToString(CultureInfo.CurrentCulture);
  8.     client.Headers[HttpRequestHeader.Accept] = "*/*";
  9.     client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
  10.  
  11.     client.OpenWriteAsync(uri, "POST", sbyteData);
  12. }
  13.  
  14. public static void WcOpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
  15. {
  16.     if (e.Error == null)
  17.     {
  18.         var objArr = e.UserState as object[];
  19.         var fileContent = e.UserState as byte[];
  20.  
  21.         Stream outputStream = e.Result;
  22.         if (fileContent != null) outputStream.Write(fileContent, 0, fileContent.Length);
  23.         outputStream.Flush();
  24.         outputStream.Close();
  25.  
  26.     }
  27. }
That should get you started.
Aug 22 '12 #2

P: 14
Thanks, But I still have the Below errors...

1.The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)

2.The type or namespace name 'OpenWriteCompletedEventHandler' could not be found (are you missing a using directive or an assembly reference?)

I have already included the System.net reference.
As I think WebClient class is not available for smart devices in C#.Net
Aug 25 '12 #3

PsychoCoder
Expert Mod 100+
P: 465
This is a simple example of uploading an image to imgur from a Windows phone:

Expand|Select|Wrap|Line Numbers
  1. public void UploadAsync(Stream stream)
  2. {
  3.     try
  4.     {
  5.         var webClient = new WebClient();
  6.         webClient.Headers["Content-type"] = "application/x-www-form-urlencoded";
  7.  
  8.         var data = string.Format("key={0}&_fake_status=200&type=base64&image={1}", PublicKey, PhotoStreamToBase64(stream));
  9.  
  10.         webClient.UploadStringAsync(new Uri("http://api.imgur.com/2/upload", UriKind.Absolute), "POST", data);
  11.  
  12.     }
  13.     catch (Exception ex)
  14.     {
  15.     }
  16. }
Expand|Select|Wrap|Line Numbers
  1. static string PhotoStreamToBase64(Stream stream)
  2. {
  3.     if (stream == null) throw new ArgumentNullException("stream");
  4.  
  5.     var mStream = new MemoryStream();
  6.     stream.CopyTo(mStream);
  7.     var result = mStream.ToArray();
  8.  
  9.     var base64Img = Convert.ToBase64String(result);
  10.     var sb = new StringBuilder();
  11.  
  12.     for (var i = 0; i < base64Img.Length; i += 32766)
  13.     {
  14.         sb.Append(Uri.EscapeDataString(base64Img.Substring(i, Math.Min(32766, base64Img.Length - i))));
  15.     }
  16.  
  17.     return sb.ToString();
  18. }
This code has been tested on a Windows Phone 7 and works. Some tweaking will need to be made to work with whatever server you're uploading to.

Happy Coding!
Aug 28 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.