ImageSource FromStream not working on IOS

bswanbswan USMember
edited November 7 in Xamarin.Forms

I am creating a Bitmap from raw pixel data and then using ImageSource.FromStream to load the image. It works on android and uwp. I manually create the bitmap with the following class.

`
public class BitmapBuilder
{
private const int BitmapTotalHeaderSize = 54;
private const int BitmapHeaderSize = 40;
private const int BitCount = 32;
private const int Planes = 1;
private const int DPI = 96;
private const int ColorsPerChannel = 4;

    private static byte[] s_BitmapHeaderByte;

    static BitmapBuilder()
    {
        CreateBitmapHeaderByte();
    }

    private static void CreateBitmapHeaderByte()
    {
        s_BitmapHeaderByte = new byte[BitmapTotalHeaderSize];
        s_BitmapHeaderByte[0] = (byte)'B';
        s_BitmapHeaderByte[1] = (byte)'M';
        //Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, buffer, 2, 4);                      // File Size    
        Buffer.BlockCopy(BitConverter.GetBytes(BitmapTotalHeaderSize), 0, s_BitmapHeaderByte, 6, 4);    // Data Offset
        Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 10, 4);                  // Padding
        Buffer.BlockCopy(BitConverter.GetBytes(BitmapHeaderSize), 0, s_BitmapHeaderByte, 14, 4);        // Header Size
        //Buffer.BlockCopy(BitConverter.GetBytes((int)width), 0, s_BitmapHeaderByte, 18, 4);            // Width
        //Buffer.BlockCopy(BitConverter.GetBytes((int)height), 0, s_BitmapHeaderByte, 22, 4);           // Height
        Buffer.BlockCopy(BitConverter.GetBytes(Planes), 0, s_BitmapHeaderByte, 26, 2);                  // Planes
        Buffer.BlockCopy(BitConverter.GetBytes(BitCount), 0, s_BitmapHeaderByte, 28, 2);                // Bit count
        Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 30, 4);                  // Compression
        Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 34, 4);                  // Image Size
        Buffer.BlockCopy(BitConverter.GetBytes(DPI), 0, s_BitmapHeaderByte, 38, 4);                     // XpixelPerM
        Buffer.BlockCopy(BitConverter.GetBytes(DPI), 0, s_BitmapHeaderByte, 42, 4);                     // YpixelPerM
        Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 46, 4);                  // Colors Used
        Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 50, 4);                  // Colors Important
    }

    public static byte[] GetBitmapFromRawData(IntPtr pixelData, int width, int height)
    {
        byte[] buffer = new byte[height * width * ColorsPerChannel + BitmapTotalHeaderSize];
        Buffer.BlockCopy(s_BitmapHeaderByte, 0, buffer, 0, s_BitmapHeaderByte.Length);
        Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, buffer, 2, 4);    // File Size    
        Buffer.BlockCopy(BitConverter.GetBytes((int)width), 0, buffer, 18, 4);      // Width
        Buffer.BlockCopy(BitConverter.GetBytes((int)height), 0, buffer, 22, 4);     // Height
        Marshal.Copy(pixelData, buffer, BitmapTotalHeaderSize, buffer.Length - BitmapTotalHeaderSize);  // Write raw data
        return buffer;
    }
}

`

Then I set the stream to the Image control
byte[] buffer = BitmapBuilder.GetBitmapFromRawData(pixelData, (int)width, (int)height); img.Source = ImageSource.FromStream(() => new MemoryStream(buffer));

It does not work on IOS.

I then saved the manually created bitmap and used it as a file. It works in android and UWP but not IOS.

`
string sFile = Path.Combine(documentDirectory, "test.pdf");

        if (File.Exists(sFile))
        {
            img.Source = ImageSource.FromStream(() => File.OpenRead(sFile));
        }

`

I am at a loss as why ImageSource.FromStream does not appear to be working on IOS.

Sign In or Register to comment.