0

I’ve extended SqlServerDataProvider to use the azure blob storage account for storing the media library assests. I’ve taken reference from below GitHub project: https://github.com/aweber1/Sitecore.Media.AzureBlobStorage

Everything looks fine but I am getting below error:

Exception: System.ArgumentException Message: Parameter is not valid. Source: System.Drawing at System.Drawing.Bitmap..ctor(Stream stream) at Sitecore.Resources.Media.ImageEffectsResize.ResizeImageStream(Stream inputStream, TransformationOptions options, ImageFormat outputFormat) at Sitecore.Resources.Media.ImageThumbnailGenerator.GetStream(MediaData mediaData, TransformationOptions options) at Sitecore.Resources.Media.MediaData.GetThumbnailStream(TransformationOptions options) at Sitecore.Resources.Media.ThumbnailProcessor.Process(GetMediaStreamPipelineArgs args) at (Object , Object ) at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain) at Sitecore.Resources.Media.Media.GetStreamFromPipeline(MediaOptions options, Boolean& canBeCached)

Because of above error the CE is very slow as Sitecore loads the actual image in the image field.

I’ve verified the GetMediaStreamPipelineArgs parameters while getting the stream from database (default one) and getting the stream from the azure blob storage account. I’ve found below differences in the MediaData.GetStream().Stream property:

Getting the media steam from the database:

CanRead: true
CanSeek: false
CanTimeout: false
CanWrite: false
Length: 1433038
Position: 0

Getting the media stream from the azure blob storage account:

CanRead: true
CanSeek: true
CanTimeout: false
CanWrite: true
Length: 1433038
Position: 0

It looks like the CanSeek=true is causing the issue while initializing the System.Drawing.Bitmap object.

Sitecore Version: Sitecore Experience Platform 9.0 Update-1

Can anyone please help me?

1 Answer 1

0

We’ve tried to understand the underline code with the help of dotPeek reflector and we’ve found below line of code causing the issue when we pass the System.IO.MemoryStream in the ResizeImageStream() method.

FileUtil.CopyStream(inputStream, (Stream) memoryStream, 8192);

Note: It's working fine when we pass the System.IO.Stream

To fix that we’ve created separate class (copied from the Sitecore.Resources.Media.ImageEffectsResize) and updated ResizeImageStream() method. Here is the updated method:

public virtual Stream ResizeImageFromStream(Stream inputStream, TransformationOptions options, ImageFormat outputFormat)
{
    Assert.ArgumentNotNull((object)inputStream, "inputStream");
    Assert.ArgumentNotNull((object)options, "options");
    Assert.ArgumentNotNull((object)outputFormat, "outputFormat");
    ResizeOptions resizeOptions = this.GetResizeOptions(options);
    if (resizeOptions.IsEmpty)
        return inputStream;
    if (inputStream.Length > Settings.Media.MaxSizeInMemory)
    {
        Tracer.Error((object)"Could not resize image stream as it was larger than the maximum size allowed for memory processing.");
        return (Stream)null;
    }
    if (Settings.Media.UseLegacyResizing)
        return this.ResizeLegacy(inputStream, options, outputFormat);
    Resizer resizer = new Resizer();
    using (Bitmap originalBitmap = new Bitmap((Stream)inputStream))
    {
        Size frameSize = resizer.GetFrameSize(originalBitmap, resizeOptions);
        if (originalBitmap.Size.Equals((object)frameSize))
        {
            inputStream.Seek(0L, SeekOrigin.Begin);
            return (Stream)inputStream;
        }
        using (Bitmap resizedBitmap = resizer.Resize(originalBitmap, resizeOptions, outputFormat))
        {
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.Seek(0L, SeekOrigin.Begin);
            ImageCodecInfo encoderInfo = this.FindEncoderInfo(outputFormat);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)options.Quality);
            resizedBitmap.Save((Stream)memoryStream, encoderInfo, encoderParams);
            memoryStream.SetLength(memoryStream.Position);
            memoryStream.Seek(0L, SeekOrigin.Begin);
            return (Stream)memoryStream;
        }
    }
}

Also we need to apply below patch:

<pipelines>
  <getMediaStream>
    <processor type="Demo.Foundation.MediaLibrary.Infrastructure.Pipelines.GetMediaStream.ThumbnailProcessor, Demo.Foundation.MediaLibrary"
               patch:instead="*[@type='Sitecore.Resources.Media.ThumbnailProcessor, Sitecore.Kernel']" />
  </getMediaStream>
</pipelines>

Thank you,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.