Silverlight Image(s) upload doesn't have a pixelWidth or pixelHeigth

176 views Asked by At

I'm having issues opening multiple image files from the users desktop and then converting those images to a scaled down size which then gets displayed on the UI (after all the converting is done). I can't find what the issue is exactly but what I've observed is that there seems to be a 5 second limit between hitting the "Open" button on the "OpenFileDialog" box control and how much time I have to read those File(s). I've used 6 files ranging in size of 9-11MB, and in another case I've used 50 1-2MB files and in all cases the process will read up until 5 seconds have expired. It never fails on the same image either so the image isn't causing the issue which would further make me believe its not a file count issue. If I test this process with only a few small sized files it happens under 1 second and there is not failure and I see all images on the UI. That is why I'm guessing its a timing issue. I know silverlight has a security exception between when the user interacts with a control (button) and how much time can elapse before displaying the "OpenFileDialog" box but this time limit seems to be different but I can't find any documentation.

Here is the code I'm using. It seems to be a pretty common recipe used everywhere but posting for completeness. The error happens on the line

var bitmap = new WriteableBitmap(bitmapImage);

The reason it fails is because the bitmapImage pixelWidth/Height == 0. Here is the full code.

private const int MaxPixelSize = 500;

    public byte[] Convert(FileInfo fileInfo, FileTypes fileType, DateTime startTime)
    {
        byte[] result = null;

        using (var stream = fileInfo.OpenRead())
        {
            DateTime EndTime = DateTime.Now;
            if (fileType == FileTypes.JPG || fileType == FileTypes.BMP || fileType == FileTypes.PNG)
            {
                var bitmapImage = new BitmapImage();
                bitmapImage.SetSource(stream);

                double scaleX = 1;
                double scaleY = 1;
                if (bitmapImage.PixelWidth > MaxPixelSize)
                {
                    scaleX = MaxPixelSize / (double)bitmapImage.PixelWidth;
                }

                if (bitmapImage.PixelHeight > MaxPixelSize)
                {
                    scaleY = MaxPixelSize / (double)bitmapImage.PixelHeight;
                }
                var scale = Math.Min(scaleX, scaleY);

                var bitmap = new WriteableBitmap(bitmapImage);
                var resizedBitmap = bitmap.Resize((int)((double)bitmapImage.PixelWidth * scale), (int)((double)bitmapImage.PixelHeight * scale), WriteableBitmapExtensions.Interpolation.Bilinear);

                using (var scaleStream = new MemoryStream())
                {
                    var encoder = new JpegEncoder();
                    var image = resizedBitmap.ToImage();
                    encoder.Encode(image, scaleStream);

                    result = scaleStream.GetBuffer();
                }
            }
            else
            {
                result = new byte[stream.Length];
                stream.Read(result, 0, (int)stream.Length);
            }
        }

        return result;
    }

Any help or suggestions are welcomed.

Thanks,

Dean

1

There are 1 answers

0
hello On

if bitmapImage.ImageOpened event is executed, you can get valid pixelWidth and height. when bitmapImage.SetSource(stream) is excuted, this event will be invoked.