-
-
Notifications
You must be signed in to change notification settings - Fork 888
Description
Setting image1 = image2; (not the same variable names used in the code below), using the code below resulted in image1 seeming to be an image object, with expected size and other image attributes, but calling image1.Save(memoryStream, imageEncoder); produced bewildering error messages such as "Object reference not set to an instance of an object." about an object not being set, among other errors. Since the object was obviously set to something, this was difficult to troubleshoot.
The following code calls the key methods related to this issue
Image image = await ReadObjectDataAsync(bucketName, key, regionEndpoint);
result = await resizeAndSaveImage(keyPrefix, image, 1000, "b");
The following method is where the problem originates:
public async Task<Image> ReadObjectDataAsync(string bucketName, string keyName, RegionEndpoint bucketRegion)
{
Image image = null;
using (MemoryStream stream = new MemoryStream())
{
try
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = keyName
};
using (IAmazonS3 client = new AmazonS3Client(bucketRegion))
using (GetObjectResponse getObjResponse = await client.GetObjectAsync(request))
using (Stream responseStream = getObjResponse.ResponseStream)
{
getObjResponse.ResponseStream.CopyTo(stream);
stream.Position = 0;
var formatType = Image.DetectFormat(stream);
using (Image<Rgba32> myimage = (Image<Rgba32>)Image.Load(stream))
{
if (myimage != null)
{
// ********************************************************************
// Replace below with: "image = myimage;" to get strange results!
// ********************************************************************
image = myimage.Clone(); // This works fine.
}
}
}
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
return image;
}
}
The following code is where it blows up, but is not associated with the origin of the issue.
private async Task<string> resizeAndSaveImage(string keyPrefix, Image imageOriginal, int maxWidthHeightDimension, string fileNameSuffix)
{
var result = "";
try
{
getNewDimensions(maxWidthHeightDimension, imageOriginal.Size(), out Size newSize, out bool dimensionsChanged);
Image imageNew;
// Image will only be resized if it is larger than the maxWidthHeight allowed size
if (dimensionsChanged)
{
// *********************************************************************
// Blew up here before .Clone() mentioned elsewhere was used
// but not certain that .Clone() fixed it, but suspect that it did
// see note at the bottom of this page that is related to this!
// *********************************************************************
imageNew = imageOriginal.Clone(ctx => ctx.Resize(newSize));
}
else
{
imageNew = imageOriginal;
}
IImageEncoder imageEncoder = new JpegEncoder()
{
Quality = 100,
Subsample = JpegSubsample.Ratio444
};
var memoryStream = new MemoryStream();
// *********************************************************************
// Blows up on next line if .Clone() mentioned elsewhere is not used
// *********************************************************************
imageNew.Save(memoryStream, imageEncoder); // specify encoder then encode and write the data to memory stream
}
catch (Exception err)
{
}
return result;
}
Note:
(Earlier it was blowing up on this: imageNew = imageOriginal.Clone(ctx => ctx.Resize(newSize));) and produced the following error.
An error occurred when processing the image using ResizeProcessor1. See the inner exception for more detail.
ArgumentException InnerException message: System.ArgumentException: Must not be empty.
Parameter name: frames
at SixLabors.ImageSharp.Image1.ValidateFramesAndGetSize(IEnumerable1 frames)
at SixLabors.ImageSharp.Image1..ctor(Configuration configuration, ImageMetadata metadata, IEnumerable1 frames)
at SixLabors.ImageSharp.Processing.Processors.Transforms.ResizeProcessor1.CreateDestination(Image1 source, Rectangle sourceRectangle)
at SixLabors.ImageSharp.Processing.Processors.CloningImageProcessor1.CloneAndApply(Image`1 source, Rectangle sourceRectangle)
Since that time I did some changing of MemoryStream to Stream and Image to Image<Rgba32> I don't know what effect those had on anything. I associate the evaporation of this earlier problem with the issue raised in the title of this Issue.