Mapsui: Layer SymbolStyle issue

Created on 19 Apr 2018  路  11Comments  路  Source: Mapsui/Mapsui

Hi,

based on Mapsui samples I'm trying to create a layer with SymbolStyle.
For example:

```c#
var image = new FileStream(@"loc.png", FileMode.Open);
loc

var bitmapId = BitmapRegistry.Instance.Register(image);
double scale = 1.0;
return new SymbolStyle {BitmapId = bitmapId, SymbolScale = scale, SymbolOffset = new Offset(0, 32)};
//return new SymbolStyle { BitmapId = bitmapId, SymbolType = SymbolType.Svg, SymbolScale = scale, SymbolOffset = new Offset(0.0, 0.5, true) };

and this works fine.

But, when I try use another image, exactly when I try use an icon from the project Resources it doesn't work.
When I鈥檓 trying to add a feature to the layer, nothing happens (the layer is empty without any error).

```c#
var resourceSet = Icons.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
var objb = resourceSet.GetObject("green");
byte[] bytesBitmap = new byte[] { };
if (objb != null)
    if (objb is Bitmap bitmap)
        bytesBitmap = bitmap.ToByteArray();

var image = new MemoryStream(bytesBitmap);

For a test I鈥檝e saved the icon into PNG (MemoryStream->FileStream->PNGfile) and try with the file (similar to the example) but also failed.

c# var image = new FileStream(@"green.png", FileMode.Open); ![green](https://user-images.githubusercontent.com/15933140/38961055-16158e16-4367-11e8-8092-c843575dac38.png)
Is there any restriction or requirement for the file format etc?

Best Regards,
艁ukasz

(1.3.3-beta.1.328)

question

All 11 comments

UPDATE:
It works for the PNG file:
green

but still the issue exists for RESX (IconResources)
Is any problem with the icons images format/style etc? Or with MemoryStream?

The IconResource type of Window is a special format and not an image. Perhaps this helps you.

thanks, but no :(

after this:
c# var resourceSet = Icons.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true); var objb = `resourceSet.GetObject("green");
the objb is already System.Drawing.Bitmap (not System.Drawing.Icon)

(this works: FileStream(@"loc.png", FileMode.Open))
I've added loc.png to the IconResources and try resourceSet.GetObject("loc") and is the same issue :(

Help! It's blocker for me.

The Mapsui WPF renderer uses the BitmapImage class. So the formats supported are those that are supported by BitmapImage. Here is Microsofts documentation. I don't see a listing of formats, but I would assume it supports most common formats.

Here is our code:

```c#
public static BitmapImage ToBitmapImage(this Stream imageData)
{
imageData.Position = 0;

        var bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = imageData;
        bitmapImage.EndInit();
        return bitmapImage;
    }

```

Perhaps there is an issue with the format, but there probably is a workaround.

Can you view the green.png that you have written to disk with any image viewer?

The System.Drawing.Bitmap.ToByteArray is an extension method? How is it implemented?

What if you use this implementation:

c# var memoryStream = new MemoryStream(); systemDrawingBitmap.Save(memoryStream, ImageFormat.Png); memoryStream.Position = 0; // Not sure if this is needed here memoryStream.ToArray();

Can you view the green.png that you have written to disk with any image viewer?

Yes, I can with any desktop app (except web-browsers, the file is here: https://user-images.githubusercontent.com/15933140/38961055-16158e16-4367-11e8-8092-c843575dac38.png).

How is it implemented?

What do you mean? (pls look at first comment)

But this works fine now:

var resourceSet = Icons.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
var objb = resourceSet.GetObject("green") as System.Drawing.Bitmap;

var memoryStream = new MemoryStream();
objb.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
memoryStream.Position = 0;

var bitmapId = BitmapRegistry.Instance.Register(memoryStream);

double scale = 1.0;

return new SymbolStyle {BitmapId = bitmapId, SymbolScale = scale, SymbolOffset = new Offset(0, 32)};

Thanks!

About 'how is it implemented'. You showed this line:

bytesBitmap = bitmap.ToByteArray();

ToByteArray is not a member of System.Drawing.Bitmap. So I assume this is an extension method. Is this correct? If so, how is this extension method implemented?

Perhaps you should replace it's implementation with the objb.Save code you use now.

Yes.
The final solution:


var resourceSet = Icons.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
var objb = resourceSet.GetObject("green");

if (objb != null && objb is Bitmap bitmap)
{
    var image = new MemoryStream();
    bitmap.Save(image, System.Drawing.Imaging.ImageFormat.Png);
    image.Position = 0;

    if (image.Length <= 0)
        throw new Exception();

    var bitmapId = BitmapRegistry.Instance.Register(image);

    double scale = 1.0;

    return new SymbolStyle {BitmapId = bitmapId, SymbolScale = scale, SymbolOffset = new Offset(0, 32)};
}
else
    throw new Exception();

Thanks :)

var objGreen = Icons.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true).GetObject("green") as System.Drawing.Bitmap;

if (objGreen == null)
    throw new Exception();

var image = new MemoryStream();
objGreen.Save(image, System.Drawing.Imaging.ImageFormat.Png);

//if (image.Length <= 0)
//  throw new Exception();

//image.Position = 0;

var bitmapId = BitmapRegistry.Instance.Register(image);

double scale = 1.0;

return new SymbolStyle {BitmapId = bitmapId, SymbolScale = scale, SymbolOffset = new Offset(0, 32)};

lol now I get it! (how is it implemented)

in
bitmap.ToByteArray();

is

public static byte[] ToByteArray(this Image image)
{
    using (MemoryStream ms = new MemoryStream())
    {
        image.Save(ms, ImageFormat.Tiff);
        return ms.ToArray();
    }
}

should be
ImageFormat.Png

and works :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukaszpitulski picture lukaszpitulski  路  3Comments

lukaszpitulski picture lukaszpitulski  路  5Comments

charlenni picture charlenni  路  3Comments

kisslori picture kisslori  路  8Comments

willy40 picture willy40  路  6Comments