Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to send image through wcf from my windows application,

See my sample code below:

Windows code:

C#
private void BtnRegister_Click(object sender, EventArgs e)
       {
           FileStream fs;
           byte[] imgByteArrFriends;
           RetunImageBytes(PathFriends, out fs, out imgByteArrFriends);

           byte[] imgByteArrFamily;
           RetunImageBytes(PathFamily, out fs, out imgByteArrFamily);

           byte[] imgByteArrLovers;
           RetunImageBytes(PathLovers, out fs, out imgByteArrLovers);

           byte[] imgByteArrOthers;
           RetunImageBytes(PathOthers, out fs, out imgByteArrOthers);

           string StatusMessage=ObjProfilePicture.SaveProfilePictures("Eswar", imgByteArrFriends, imgByteArrLovers, imgByteArrFamily, imgByteArrOthers);
       }

       private void RetunImageBytes(string path,out FileStream fs, out byte[] imgByteArr)
       {
           imgByteArr = null;
           fs = null;
           if (path != null)
           {
               fs = new FileStream(path, FileMode.Open, FileAccess.Read);
               imgByteArr = new byte[fs.Length];
               fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
               fs.Close();
           }
       }





WCF Code:

C#
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
   public string SaveProfilePictures(string UserID, byte[] Friends, byte[] Lovers, byte[] Family, byte[] Others)
   {
       string Message = null;
       try
       {
           objBlobDbConfig.ExecuteCommand(UserID, Friends, Lovers, Family, Others);
           Message="Success";
       }
       catch (Exception ex)
       {
           Message= "Image Upload Failed.. Please try after sometime";
       }



If any body know the solution please help me

What I have tried:

I am trying to send image through wcf from my windows application,

See my sample code below:

Windows code:

C#
private void BtnRegister_Click(object sender, EventArgs e)
       {
           FileStream fs;
           byte[] imgByteArrFriends;
           RetunImageBytes(PathFriends, out fs, out imgByteArrFriends);

           byte[] imgByteArrFamily;
           RetunImageBytes(PathFamily, out fs, out imgByteArrFamily);

           byte[] imgByteArrLovers;
           RetunImageBytes(PathLovers, out fs, out imgByteArrLovers);

           byte[] imgByteArrOthers;
           RetunImageBytes(PathOthers, out fs, out imgByteArrOthers);

           string StatusMessage=ObjProfilePicture.SaveProfilePictures("Eswar", imgByteArrFriends, imgByteArrLovers, imgByteArrFamily, imgByteArrOthers);
       }

       private void RetunImageBytes(string path,out FileStream fs, out byte[] imgByteArr)
       {
           imgByteArr = null;
           fs = null;
           if (path != null)
           {
               fs = new FileStream(path, FileMode.Open, FileAccess.Read);
               imgByteArr = new byte[fs.Length];
               fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
               fs.Close();
           }
       }





WCF Code:

C#
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
   public string SaveProfilePictures(string UserID, byte[] Friends, byte[] Lovers, byte[] Family, byte[] Others)
   {
       string Message = null;
       try
       {
           objBlobDbConfig.ExecuteCommand(UserID, Friends, Lovers, Family, Others);
           Message="Success";
       }
       catch (Exception ex)
       {
           Message= "Image Upload Failed.. Please try after sometime";
       }



If any body know the solution please help me
Posted
Updated 7-Apr-16 8:08am

1 solution

C#
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));

Why are you trying to convert an integer into an integer? You will end up with an invalid number and thus not read all the data.

See FileStream.Length Property (System.IO)[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900