-6

I am new to c#. I am developing an iOS project. i want to upload images to server. So i want to convert image into string. can anyone help me to do this.

I am agree any type of method to upload image to sever.

Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
Indian
  • 23
  • 11

2 Answers2

3

How about that?

FileStream stream = new FileStream(imageFilePath, FileMode.Open);
BinaryReader binreader = new BinaryReader(stream);
byte[] buffer = new byte[(int) stream.Length];
buffer = binreader.ReadBytes((int) stream.Length);
string serialized = Convert.ToBase64String(buffer)

If you have the System.Drawing.Image object instead of a file path, you can do:

System.Drawing.Image image; //initialize it someway
MemoryStream ms = new MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); //if it is jpeg
byte[] buffer = ms.ToArray();
string serialized = Convert.ToBase64String(buffer);

And then you pass the 'serialiazed' value to the server.

Put if it will work depends on how the server will handle that.

heringer
  • 2,698
  • 1
  • 20
  • 33
  • i got somethng can't able to read by human. okay. how to send that to php server? – Indian Feb 13 '15 at 11:16
  • hello heringer, say me, which one i have to pass to server? and you have converted imagefilepath instead image? no need to convert image?please explain. i can't understand. – Indian Feb 13 '15 at 11:25
  • i got two error when i am trying with your second method. Error is i cant use System.Drawing.ImageView.Image.i mean, i have image on imageview. how o set it to system.drawing? second error is Save does not ahve any definiton. how to solve this? guide me heringer. – Indian Feb 14 '15 at 04:25
2

The same just a little bit less code

//Clientside    
byte[] imgBytes = File.ReadAllBytes("FilePath");
string imgStr = System.Convert.ToBase64String(imgBytes);

//Serverside 
byte[] serverSideImgBytes = System.Convert.FromBase64String(imgStr);
File.WriteAllBytes("PathAndFileName", serverSideImgBytes);
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
Alexey
  • 21
  • 3
  • server is php.. how can apply this codes to php? – Indian Feb 13 '15 at 11:20
  • i think you can use php function named base64_decode – heringer Feb 13 '15 at 12:03
  • guys, you all teach me how to convert filepath. i don't need to convert filepath. i need to convert image only. not its path. please understand. – Indian Feb 14 '15 at 04:38
  • Replace FilePath on the path to your image like ../Images/Picture.jpg. And when you will send image bytes to server send filename for saving. – Alexey Feb 18 '15 at 15:59