Hallo i have one wcf server and one client and i want to send/receive image from/to client as string (convert to base64). I send from server to client already and works. I want to send from client to server and get error: Invalid length for a Base-64 char array. Probably the error is in xml file.
Here is my code:
Server side: (WCFService.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Json;
namespace Services
{
public class WCFService : Services_Form, IWCFService
{
public string ImageToBase64(Image image)
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
//RECEIVE IMAGE
public string getimage()
{
return ImageToBase64(pictureBox1.Image);
}
//SEND IMAGE
public string postimage(string im)
{
pictureBox2.Image = Base64ToImage(im);
return "Ok";
}
}
}
(IWCFService.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace Services
{
[ServiceContract]
public interface IWCFService
{
[OperationContract(Name = "image")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "getimage")]
string getimage();
[OperationContract(Name = "image2")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "postimage?Image={im}")]
string postimage(string im);
}
}
The Server Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
namespace Services
{
public partial class Services_Form : Form
{
ServiceHost host;
string WCFPort;
//SERVER FORM
public Services_Form()
{
InitializeComponent();
}
private void StartWCFServer()
{
if (host == null)
{
Uri baseAddress = new Uri("http://localhost:"+WCFPort+"/");
host = new ServiceHost(typeof(WCFService), baseAddress);
host.AddServiceEndpoint(typeof(IWCFService), new WSHttpBinding(), "Services");
try
{
host.Open();
}
catch
{
MessageBox.Show("ERROR");
}
}
else
{
MessageBox.Show("Change PORT");
}
}
private void btn_StopServer_Click(object sender, EventArgs e)
{
if (host != null)
{
host.Close();
}
else MessageBox.Show("Server already closed!");
}
//START SERVER BUTTON
private void button1_Click_1(object sender, EventArgs e)
{
WCFPort = "8000";
StartWCFServer();
}
}
}
Server's App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="Services.WCFService" behaviorConfiguration="ServiceBehaviour">
<endpoint bindingConfiguration="webHttpBindingDev" address="" binding="webHttpBinding" contract="Services.IWCFService" behaviorConfiguration="web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingDev">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
Now the client side:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
namespace Διαχείριση
{
public partial class Administator_Form : Form
{
string IpAddress = "localhost";
string Port = "8000";
//CLIENT FORM
public Administator_Form()
{
InitializeComponent();
try
{
WebRequest request = WebRequest.Create(UrlTag(""));
WebResponse response = request.GetResponse();
}
catch
{
MessageBox.Show("Error connect to server");
}
}
public string ImageToBase64(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
private T GetDataFromServer<T>(string url)
{
WebRequest request = WebRequest.Create(string.Format(url));
WebResponse response = request.GetResponse();
Stream stream = request.GetResponse().GetResponseStream();
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
T result = (T)json.ReadObject(stream);
return result;
}
private string UrlTag(string tag)
{
return "http://" + IpAddress + ":" + Port + "/" + tag;
}
//SEND IMAGE
private void button2_Click(object sender, EventArgs e)
{
string myParameters = "";
myParameters = "?Image=" + ImageToBase64(pictureBox1.Image) + "";
string responseMessage = GetDataFromServer<String>(UrlTag("postimage" + myParameters));
}
//RECEIVE IMAGE
private void button3_Click(object sender, EventArgs e)
{
string img = GetDataFromServer<string>(UrlTag("getimage"));
pictureBox2.Image = Base64ToImage(img);
}
}
}
No comments:
Post a Comment