2015-04-03 2 views
0

Я хочу загрузить изображение в мою службу wcf из окна телефона 8.1 приложения. Я использую HttpRequest/HTTPResponse Сообщения КОДЕКСАwindows phone 8.1 REST Служба WCF Загрузить изображение

Заказчик:

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

Как пишет MSDN на Bitmap Constructor (Stream) 's страницы (https://msdn.microsoft.com/en-us/library/z7ha67kw%28v=vs.110%29.aspx), вы получаете 'System.ArgumentException', если:

stream does not contain image data or is null. -or- stream contains a PNG image file with a single dimension greater than 65,535 pixels.

вы должны проверить эти условия!

+0

Спасибо за ответ, я решаю эту проблему –

+0

Я рад, что смог помочь! – WPMed

Смежные вопросы