Question

            //convert photo to baos
            var memoryStream = new System.IO.MemoryStream();
            e.ChosenPhoto.CopyTo(memoryStream);
            //string baos = memoryStream.ToString();
            byte[] result = memoryStream.ToArray();
            String base64 = System.Convert.ToBase64String(result);
            String post_data = "&image=" + base64;
            ...
            wc.UploadStringAsync(imgur_api,"POST",post_data);  

I am using this code to upload an image to the Imgur API v3 using WebClient. The image being selected is either one of the 7 photos provided by the Windows Phone 7.1 emulator, or the simulated camera images. When I try to load the images, they are a largely-grey corrupted mess. Am I generating the base64 properly and/or do I need to render a Bitmap of the picture first before creating the byte[] and base64?

Thanks in advance!

Was it helpful?

Solution

Use something like Uri.EscapeDataString to escape the data so that special URL characters are not interpreted.

OTHER TIPS

I use this

 private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
    {
        if (e.TaskResult != TaskResult.OK) return;
        var bimg = new BitmapImage();
        bimg.SetSource(e.ChosenPhoto);
        var sbytedata = ReadToEnd(e.ChosenPhoto);
    }

 public static byte[] ReadToEnd(System.IO.Stream stream)
    {
        long originalPosition = stream.Position;
        stream.Position = 0;

        try
        {
            byte[] readBuffer = new byte[4096];

            int totalBytesRead = 0;
            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;

                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = stream.ReadByte();
                    if (nextByte != -1)
                    {
                        byte[] temp = new byte[readBuffer.Length * 2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            stream.Position = originalPosition;
        }
    }

And upload byte[] to server. Hope it's help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top