将byte []图像上传到Picasa

时间:2013-08-03 16:32:54

标签: c# windows-phone picasa

我用

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想要二进制数据?

1 个答案:

答案 0 :(得分: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;
            }
        }

希望有所帮助