Bug 31029 - [Windows Phone 8.1]Generating an Image via MemoryStream does not appear
Summary: [Windows Phone 8.1]Generating an Image via MemoryStream does not appear
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 1.4.2
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Rui Marinho
URL:
Depends on:
Blocks:
 
Reported: 2015-06-11 13:53 UTC by Jon Douglas [MSFT]
Modified: 2015-06-17 08:18 UTC (History)
2 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Jon Douglas [MSFT] 2015-06-11 13:53:51 UTC
*Description

If one uses a MemoryStream to generate an image based off a BinaryWriter, it works just fine on other platforms, however on Windows Phone 8.1, it seems to have unexpected behavior such as just showing a blank screen.

EX:

Take for example we have a BmpMaker class to generate an ImageSource

public class BmpMaker
    {
        const int headerSize = 54; 
        readonly byte[] buffer; 
        public BmpMaker(int width, int height) 
        { 
            Width = width; 
            Height = height; 
            int numPixels = Width * Height; 
            int numPixelBytes = 4 * numPixels; 
            int fileSize = headerSize + numPixelBytes; 
            buffer = new byte[fileSize]; 
            // Write headers in MemoryStream and hence the buffer. 
            using (MemoryStream memoryStream = new MemoryStream(buffer)) 
            { 
                using (BinaryWriter writer = new BinaryWriter(memoryStream, Encoding.UTF8)) 
                { 
                    // Construct BMP header (14 bytes). 
                    writer.Write(new char[] { 'B', 'M' }); // Signature 
                    writer.Write(fileSize); // File size 
                    writer.Write((short)0); // Reserved 
                    writer.Write((short)0); // Reserved 
                    writer.Write(headerSize); // Offset to pixels 
                    // Construct BitmapInfoHeader (40 bytes). 
                    writer.Write(40); // Header size 
                    writer.Write(Width); // Pixel width 
                    writer.Write(Height); // Pixel height 
                    writer.Write((short)1); // Planes 
                    writer.Write((short)32); // Bits per pixel 
                    writer.Write(0); // Compression 
                    writer.Write(numPixelBytes); // Image size in bytes 
                    writer.Write(0); // X pixels per meter 
                    writer.Write(0); // Y pixels per meter 
                    writer.Write(0); // Number colors in color table 
                    writer.Write(0); // Important color count 
                } 
            } 
        }

        public int Width { get; private set; }
        public int Height { get; private set; }

        public void SetPixel(int row, int col, Color color) 
        { 
            SetPixel(row, col, (int)(255 * color.R), 
                (int)(255 * color.G), 
                (int)(255 * color.B), 
                (int)(255 * color.A)); 
        } 

        public void SetPixel(int row, int col, int r, int g, int b, int a = 255) 
        { 
            int index = (row * Width + col) * 4 + headerSize; 
            buffer[index + 0] = (byte)b; 
            buffer[index + 1] = (byte)g; 
            buffer[index + 2] = (byte)r; 
            buffer[index + 3] = (byte)a; 
        } 

        public ImageSource Generate() 
        { 
            Stream memoryStream = new MemoryStream(buffer); 
            // Convert to StreamImageSource. 
            ImageSource imageSource = ImageSource.FromStream(() => { return memoryStream; }); 
            
            return imageSource; 
        } 
    }    

We then want to make a red square with a white border via the following method:

public ImageSource GenerateBmp(int rows, int cols, Color color)
        {
            BmpMaker bmpMaker = new BmpMaker(rows,cols);
            //background color to white
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    bmpMaker.SetPixel(i, j,Color.White);
                }
            }
            //draw a square
            int marginX = rows / 10;
            int marginY = cols / 10;
            for (int row = marginX; row < (rows - marginX); row++)
            {
                for (int col = marginY; col < (cols - marginY); col++)
                {
                    bmpMaker.SetPixel(row, col, color);
                }
            }
            ImageSource resultImage = bmpMaker.Generate();
            return resultImage;
        }

The results are that this works on iOS/Android/Windows Phone 8.0, but not Windows Phone 8.1

*Reproduction

https://www.dropbox.com/sh/xq492dkw7lyhudy/AACVC473CTEXCbOxmgExg6Lda?dl=0%3E

Ensure in the App.cs file that the following is not commented out:

            var csTab = new TabbedPage();
            csTab.Children.Add(new GeneratedImages { Title = "Generated" });
            csTab.Children.Add(new LocalImages { Title = "Local", Icon = "csharp.png" });
            csTab.Children.Add(new DownloadImages { Title = "Download", Icon = "csharp.png" });
            csTab.Children.Add(new EmbeddedImages { Title = "Embedded", Icon = "csharp.png" });
            MainPage = csTab;

1. Run the Application

2. Notice that the screen sits on an empty page instead of creating the respective tabbed pages

Currently there is another known issue with EmbeddedImages on Windows Phone 8.1 that is logged here:

https://bugzilla.xamarin.com/show_bug.cgi?id=30687

*Xamarin Forms Version

1.4.2

*Version Information

Xamarin 3.11.590

Xamarin.Android 5.1.3.1

Xamarin.iOS 8.10.1.0
Comment 1 Rui Marinho 2015-06-15 14:10:26 UTC
Should be fixed in 1.4.3 final
Comment 2 Parmendra Kumar 2015-06-17 08:18:18 UTC
I have checked this issue with Xamarin.Forms 1.4.3.6364-pre3 and its working
fine at my end.

Hence closing this issue.


Environment info:

Microsoft Visual Studio Professional 2013
Version 12.0.30723.00 Update 3
Microsoft .NET Framework
Version 4.5.51641

Xamarin   3.11.590.0 
Xamarin.Android   5.1.3.1 
Xamarin.iOS   8.10.1.0