开发者

Accessing Stream at the same time

I am attempting to reuse the same Stream multiple times. One for resizing the image, and the other for uploading the image. Whilst it does work for resizing the image, it seems to be locking out the other method for uploading the file. I have tried to copy the Stream using Stream.CopyTo(MemoryStream), then using that for uploading, but it still doesn't make a different.

I am opening a Stream using the PhotoChooserTask. I then pass the Stream to a ImageThumbnail method which creates a thumbnail of the image and then saves it to IsolatedStorage as shown below:

    public static void SaveThumbnail(Stream imageStream, string fileName, double imageMaxHeight, double imageMaxWidth)
    {
        var bitmapImage = new BitmapImage();
        bitmapImage.Set开发者_运维技巧Source(imageStream);
        var resizedImage = new WriteableBitmap(bitmapImage);

        using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            double scaleX = 1;
            using (var fileStream = isolatedStorage.CreateFile(fileName))
            {
                //do stuff for resizing here...
                resizedImage.SaveJpeg(fileStream, newWidth1, newHeight1, 0, 100);
            }
        }
    }

At the same time, I am reusing the same Stream from the PhotoChooserTask for uploading the image. EItherway, it seems to be locking eachother out, and no error is being thrown.

Any tips?


You need to copy the stream into a byte array, because streams change during use and can't be cloned.

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32768];
    while (true)
    {
        int read = input.Read (buffer, 0, buffer.Length);
        if (read <= 0)
            return;
        output.Write (buffer, 0, read);
    }
}


Copying to a MemoryStream should do the trick. To reuse the memory stream, you need to reset the position back to the beginning, by setting the Position property back to 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜