0

我用

PhotoChooserTask photoChooserTask = new PhotoChooserTask();

让用户选择一张图片。之后,我尝试将图像数据上传到 Picasa:

byte[] buffer = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Position = 0;
MemoryStream stream = new MemoryStream();
e.ChosenPhoto.Read(buffer, 0, buffer.Length);

// Write to the request stream
postStream.Write(buffer, 0, buffer.Length);
postStream.Close();

上面的方法我遇到的问题是图片上传成功,但是图片数据无效。图片上传后看不到。也许是因为我使用 bytedata 而 Picasa 想要二进制数据?

4

1 回答 1

0

尝试这个:

var buffer= 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;
            }
        }

希望有帮助

于 2013-08-05T14:25:30.437 回答