我想从windows phone 8.1应用程序将图像上传到我的wcf服务。我使用HttpRequest / HttpResponse消息
客户代码:
private async void RecognizeProduct()
{
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
byte[] arr = originalBitmap.ToByteArray();
var barcodeImageForm = new ByteArrayContent(arr, 0, arr.Count());
barcodeImageForm.Headers.ContentType = new MediaTypeHeaderValue("image/bmp");
form.Add(barcodeImageForm, "image", "barcodeImage.bmp");
HttpResponseMessage response = await httpClient.PostAsync("http://localhost:51746/Service.svc/recognize", form);
response.EnsureSuccessStatusCode();
httpClient.Dispose();
string result = response.Content.ReadAsStringAsync().Result;
}
服务界面:
[OperationContract]
[WebInvoke(UriTemplate = "/recognize", Method="POST")]
string RecognizeBarcode(Stream barcodeImageStream);
服务方式:
public string RecognizeBarcode(Stream barcodeImageStream)
{
byte[] buffer = new byte[32768];
MemoryStream ms = new MemoryStream();
barcodeImageStream.CopyTo(ms);
Bitmap barcodePhoto = new Bitmap(barcodeImageStream);
string barcodeResult = BarcodeEncoder.BarcodeEncode(barcodePhoto);
barcodeImageStream.Close();
ms.Close();
return barcodeResult;
}
的Web.config
<bindings>
<webHttpBinding>
<binding name="" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed" openTimeout="00:25:00" closeTimeout="00:25:00"
sendTimeout="00:25:00" receiveTimeout="00:25:00">
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="WhereCheaperService.Service">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="WhereCheaperService.IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="max" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<!--BEGIN ADD ENDPOINT BEHAVIOR-->
<endpointBehaviors>
<behavior name ="web">
<webHttp />
</behavior>
</endpointBehaviors>
<!--END of ADD ENDPOINT BEHAVIOR-->
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
客户端成功创建服务但请求不起作用。我收到错误:
An exception of type 'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user code
我如何解决这个问题?感谢。
答案 0 :(得分:0)
当MSDN在Bitmap Constructor(Stream)的页面(https://msdn.microsoft.com/en-us/library/z7ha67kw%28v=vs.110%29.aspx)上写入时,您会得到一个&#39; System.ArgumentException&#39;如果:
stream不包含图像数据或为null。 -要么- stream包含一个PNG图像文件,其单个维度大于65,535像素。
你应该检查这些条件!