Errors Saving an Image from byte[] (from SQL Server)

Hi all,

I do hope someone can supply me with a nice quick and consise answer to my issue…

In a nutshell, I’m getting the error :

An exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll but was not handled in user code
Additional information: startIndex cannot be larger than length of string.

within this function on the SetValue line :

    private bool CacheImage(byte[] bytes, string filename){
        //get the Media Service and create the node
        IMediaService _mediaService = ApplicationContext.Current.Services.MediaService;
        IMedia _mediaItem = _mediaService.CreateMedia(filename, 1159, "Image");
        //load the bytes as a stream and apply to the node
        MemoryStream mStream = new MemoryStream(bytes);
        mStream.Position = 0;
        _mediaItem.SetValue("umbracoFile", filename, mStream);
        //save the media node
        _mediaService.Save(_mediaItem);
        return true;
    }

I’ve seen a bunch of posts about similar problems but nothing seems to supply me with a 100% solution. Can anyone give me a hint?

One question that springs to mind is ‘what string’? we’re dealing with a stream here right??

Thanks in advance

Danny “Blatant”


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/57479-errors-saving-an-image-from-byte-from-sql-server