473,508 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to upload an image in C# Smart device Application

14 New Member
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
3 26933
PsychoCoder
465 Recognized Expert Moderator Contributor
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
Udara chathuranga
14 New Member
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
465 Recognized Expert Moderator Contributor
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

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

Similar topics

6
2887
by: Brian Holbrook | last post by:
I am new to VB .Net. Is there anything close to what a flex grid was in VB 6.0? By this I mean being able to manipulate and add text data that is not in a database. I would be taking data from...
5
1530
by: Roger Strong | last post by:
Hi! I need to create a VB.NET application for a Pocket PC. I've installed VB.Net, the .NET Compact Framework and the Microsoft Pocket PC 2003 SDK. Still, there is no "Smart Device Application"...
0
1106
by: Jason Gyetko | last post by:
I have a VB.NET Windows Application which uses Shell() to run a program. What is the equivalent for a Smart Device Application? Thanks.
2
4595
by: Jeff | last post by:
Hello The Smart Device Application template doesn't show up in the New Project dialog of Visual Studio 2003 .NET. I installed my software in this order: 1. Microsoft ActiveSync 3.7.1 2 ...
4
1504
by: EchoRev | last post by:
Hi all, this is my first post and if I'm honest its not a particularly new one but here goes I've read on several different sites on the web about calling unmanaged c++ code from within a c#...
1
1028
by: mino | last post by:
how can i access to a Ms Access database using VB.Net2003 in a Smart device Application?
2
1759
by: muzilli | last post by:
Hi, I'm developing an application for Smart Device Application and I'm creating Threads. In determinated time the thread must be stoped. I was searching for the help about the Threads and I...
0
1209
by: Jan | last post by:
Hi, I am working on a smart device application. The application should run properly on two different devices with two different resolutions (240*320 and 240*240). I know in the 'normal'...
0
2817
by: Chen Naikun | last post by:
hi, i created a webservice in vs 2008 and i can add and update the servie reference from a windows form application, but after i tried to add the web reference from a smart device application, i...
2
2463
by: dipalipatel | last post by:
Hi, I have one Smart Device Application for .NET Framework 1.0 and now i upgraded to Framework 2.0. Now when i run the application it's giving me an error on my device that: Can't find...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.