locked
Data Binding

    Question

  • I have GET request f in the ASP.NET Web API  value controller's which return image or image in byte[] format

    here is code

     
    public Image Get()        
    {            
      Image _image = Image.FromFile("file address"); 
      return _image;          
    }

    OR

    public byte[] Get()        
    {           
       Image _image = Image.FromFile("file address");
       byte[] _imageByte = ImageToByteArraybyMemoryStream(image);
       return _imageByte;        
    }       
    
    
     private byte[] ImageToByteArraybyMemoryStream(Image _image)   {            
       MemoryStream ms = new MemoryStream();   
       _image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
       return ms.ToArray();      
    }

    and in the client side which is windows store universal app i could not convert byte[] to Image

     
    private async void _Click(object sender, RoutedEventArgs e) 
     {           
      Uri datauri = new 
      Uri("http://localhost:63606/Api/ControllerName"); 
               var client = new HttpClient();      
      var datafile = await client.GetStringAsync(datauri);
     
     byte[] patern = JsonConvert.DeserializeObject<byte[]>(datafile);    
    
    ??????        
    }


    Tuesday, February 10, 2015 12:34 PM

Answers

  • Hi Azat,

    Per my understanding, your target is to download the image from server. In client side, we don’t need to care what the technology server is using, maybe a static file from IIS, or creating by code programmatically. Try the following link to see some methods. http://stackoverflow.com/questions/10836367/download-an-image-to-local-storage-in-metro-style-apps.

    About the server side, I am not very familiar with asp.net web API. I suggest you post this question on asp.net forum. http://forums.asp.net/. Just to save your time, I provide something I understand. You are using incorrect return type as response. I would recommend you try HttpResponseMessage to instead of byte array. Also set headers of response. See a code sample from http://stackoverflow.com/questions/13001588/output-image-using-web-api-httpresponsemessage.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Azat Tazayan Thursday, February 12, 2015 1:05 PM
    Wednesday, February 11, 2015 9:28 AM
    Moderator

All replies

  • Hi Azat,

    Per my understanding, your target is to download the image from server. In client side, we don’t need to care what the technology server is using, maybe a static file from IIS, or creating by code programmatically. Try the following link to see some methods. http://stackoverflow.com/questions/10836367/download-an-image-to-local-storage-in-metro-style-apps.

    About the server side, I am not very familiar with asp.net web API. I suggest you post this question on asp.net forum. http://forums.asp.net/. Just to save your time, I provide something I understand. You are using incorrect return type as response. I would recommend you try HttpResponseMessage to instead of byte array. Also set headers of response. See a code sample from http://stackoverflow.com/questions/13001588/output-image-using-web-api-httpresponsemessage.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Azat Tazayan Thursday, February 12, 2015 1:05 PM
    Wednesday, February 11, 2015 9:28 AM
    Moderator
  • Thanks Herro wong ! I have seen these articles and it was helped me
    • Edited by Azat Tazayan Thursday, February 12, 2015 1:08 PM
    Thursday, February 12, 2015 1:05 PM
  • Here is full answer

    http://stackoverflow.com/questions/28478183/windows-8-universal-app-asp-net-web-api

    Wednesday, February 18, 2015 9:53 AM