3

in a Windows store app project i have this method

private async void getUSerImage()
    {
        try
        {

            using (var httpClient = new HttpClient { BaseAddress = Constants.baseAddress })
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", App.Current.Resources["token"] as string);

                using (var response = await httpClient.GetAsync("user/image"))
                {
                    string responseData = await response.Content.ReadAsStringAsync();
                    byte[] bytes = Encoding.Unicode.GetBytes(responseData);

                    StorageFile sampleFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("userImage.jpg", CreationCollisionOption.ReplaceExisting);
                    await FileIO.WriteBytesAsync(sampleFile, bytes);
                    var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

                }
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
        }
    }

im trying to convert the binary data received to bytes and save it as a file this is what the responseData contains something like this: enter image description here

the image gets created but its corrupt , i can't open it i assume this

byte[] bytes = Encoding.Unicode.GetBytes(responseData);

does't work

the webservice documentation says that "The body contains the binary image data" is there any better way to convert the binary data im receiving to bytes and save it to a file?

EDIT:

i ended up doing this and it worked

Stream imageStream = await response.Content.ReadAsStreamAsync();
                    byte[] bytes = new byte[imageStream.Length];

                    imageStream.Read(bytes, 0, (int)imageStream.Length);


                    StorageFile sampleFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("userImage.jpg", CreationCollisionOption.ReplaceExisting);
                    await FileIO.WriteBytesAsync(sampleFile, bytes);
                    var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
4
  • Where is the question? Commented Apr 23, 2015 at 10:22
  • oh, ill edit my post :) sorry Commented Apr 23, 2015 at 10:24
  • refer it may help you stackoverflow.com/questions/18827081/… stackoverflow.com/questions/5400173/… Commented Apr 23, 2015 at 10:33
  • I know this is an old question, but in case anyone else stumbles upon it, I believe the first three lines of code in Ric's solution can be replaced with byte[] bytes = response.Content.ReadAsByteArrayAsync(); Commented Aug 6, 2020 at 15:53

2 Answers 2

2

You want to read the data into a stream first rather than reading it as a string:

Stream imageStream= await response.Content.GetStreamAsync(theURI);
var image = System.Drawing.Image.FromStream(imageStream);
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

context.Response.ContentType = "image/jpeg";

// Get the stream 

var image = System.Drawing.Image.FromStream(stream);
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);

if you want to show the image instead of saving could use

$('img')[0].src = 'Home/getUSerImage' // what ever image url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.