Friday 15 August 2014

xml - Error send image from client to WCF Server C# (Invalid length for a Base-64 char array) -



xml - Error send image from client to WCF Server C# (Invalid length for a Base-64 char array) -

hallo have 1 wcf server , 1 client , want send/receive image from/to client string (convert base64). send server client , works. want send client server , error: invalid length base-64 char array. error in xml file.

here 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); homecoming base64string; } } public image base64toimage(string base64string) { // convert base64 string byte[] byte[] imagebytes = convert.frombase64string(base64string); memorystream ms = new memorystream(imagebytes, 0, imagebytes.length); // convert byte[] image ms.write(imagebytes, 0, imagebytes.length); image image = image.fromstream(ms, true); homecoming image; } //receive image public string getimage() { homecoming imagetobase64(picturebox1.image); } //send image public string postimage(string im) { picturebox2.image = base64toimage(im); homecoming "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"); seek { host.open(); } grab { 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 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 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(); seek { webrequest request = webrequest.create(urltag("")); webresponse response = request.getresponse(); } grab { messagebox.show("error connect server"); } } public string imagetobase64(image image) { using (memorystream ms = new memorystream()) { // convert image byte[] image.save(ms, system.drawing.imaging.imageformat.jpeg); byte[] imagebytes = ms.toarray(); // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); homecoming base64string; } } public image base64toimage(string base64string) { // convert base64 string byte[] byte[] imagebytes = convert.frombase64string(base64string); memorystream ms = new memorystream(imagebytes, 0, imagebytes.length); // convert byte[] image ms.write(imagebytes, 0, imagebytes.length); image image = image.fromstream(ms, true); homecoming 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); homecoming result; } private string urltag(string tag) { homecoming "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); } } }

finaly found solution in question. code same in functions convert image base64 , base64 image must replace = , / , + characters in base64 string other characters because these characters url unavailable.

here right functions:

public string imagetobase64(image image) { using (memorystream m = new memorystream()) { image.save(m, image.rawformat); byte[] imagebytes = m.toarray(); string base64string = convert.tobase64string(imagebytes); string = base64string.replace('/', '_').replace('+', '-').replace('=', ','); base64string = a; homecoming base64string; } } public image base64toimage(string base64string) { // convert base64 string byte[] string = base64string.replace('_', '/').replace('-', '+').replace(',', '='); base64string = a; byte[] imagebytes = convert.frombase64string(base64string); memorystream ms = new memorystream(imagebytes, 0, imagebytes.length); // convert byte[] image ms.write(imagebytes, 0, imagebytes.length); image image = image.fromstream(ms, true); homecoming image; }

with way works perfectly. if have other improve ideas tell me larn more thinks.

c# xml wcf base64

No comments:

Post a Comment