Resize in WinRT/Windows 8

Oct 16, 2012 at 5:25 PM

Seems, that Resize() function is not work....

My code:

 

 

 public class PictureExtension 
    {
        private MemoryRandomAccessStream _memoryRandomAccessStream;
        private readonly Stream _dataStream;
        private readonly double _height;
        private readonly double _width;

        public PictureExtension(Stream dataStream, double height, double width)
        {
            _dataStream = dataStream;
            _memoryRandomAccessStream = (_dataStream.ToRandomAccessStream());
            _height = height;
            _width = width;
        }

 

 

 

 public byte[] ToArray(double maxSide)
        {
            if (_height <= maxSide && _width <= maxSide)
            {
                return _dataStream.ToArray();
            }
            else
            {
                var target = new WriteableBitmap((int) _width, (int) _height);

                var aspectRatio = (double)_width / _height;
                double newHeight;
                double newWidth;
                if (_width > _height)
                {
                    newWidth = maxSide;
                    newHeight = newWidth / aspectRatio;
                }
                else
                {
                    newHeight = maxSide;
                    newWidth = maxSide * aspectRatio;
                }

                int count = (int)_dataStream.Length;

                using (var bmpStream = target.PixelBuffer.AsStream())
                {
                    bmpStream.Seek(0, SeekOrigin.Begin);
                    bmpStream.Write(_dataStream.ToArray(), 0, _dataStream.ToArray().Length);
                }
               
                var resized = target.Resize((int)newWidth, (int)newHeight, WriteableBitmapExtensions.Interpolation.Bilinear);

                return target.ToByteArray();

            }
        }

}

 

I got byte[] array, but it is not image any more...

Coordinator
Oct 16, 2012 at 7:20 PM

Look closely at your code. You're returning target, not resized. And BTW, the ToByteArray is the raw pixel data and  can then only be used with FromByteArray.

Oct 17, 2012 at 8:30 AM

Oh, I forget to change the returning value after testing... but it nothing change.. If I return target, I see my image... But if I return resized, I see array of bytes but it is not image... It hasn't image's headers....

Coordinator
Oct 17, 2012 at 9:00 AM

As I wrote "the ToByteArray is the raw pixel data and  can then only be used with FromByteArray."

It's just the raw pixel data, if you want to use it as image JPEG or PNG, you have to encode it as such. See the WinRT BitmapEncoder class.