pnp/PnP

Core.ProfilePictureUploader - Cannot access a closed stream when image size matches target size.

they007 opened this issue · 0 comments

Category

[X] Bug
[ ] Enhancement

Environment

[X] Office 365 / SharePoint Online
[ ] SharePoint 2016
[ ] SharePoint 2013

Expected or Desired Behavior

Images of all sizes should be re-sized and uploaded.

Observed Behavior

User Error: Failed to upload thumbnailpicture to SPO for Cannot access a closed Stream. error occurs when source and desired file sizes match. In my case, the file was 72x72. The small file uploaded fine, but then the medium file would fail.

Steps to Reproduce

Create a source image that is 72x72 and try to upload via this tool. My image was originally coming from Exchange but this also reproduced when pulling from a local folder.

I was able to "fix" this issue by changing some of the code. I had the ResizeImageSmall return a copy of the stream vs the original one. There is likely a better way to handle this, but it did enable me to use the tool.

Original code:
if (originalImage.Width == NewWidth) //if sourceimage is same as destination, no point resizing, as it loses quality
{
OriginalImage.Seek(0, SeekOrigin.Begin);
originalImage.Dispose();
return OriginalImage; //return same image that was passed in
}

My Changes:
if (originalImage.Width == NewWidth) //if sourceimage is same as destination, no point resizing, as it loses quality
{
MemoryStream memStream = new MemoryStream();
OriginalImage.Seek(0, SeekOrigin.Begin);
OriginalImage.CopyTo(memStream);
originalImage.Dispose();
memStream.Seek(0, SeekOrigin.Begin);
return memStream; //return unchanged copy of the same image stream that was passed in
}