5

I found the next code to convert ISymbol into bitmap image, but how to know the suitable image size? (suppose we have a ITextSymbol)

private static Image PointSymbolToImage(ISymbol symbol, Size imageSize)
{
    double x = imageSize.Width / 2;
    double y = (imageSize.Height - 1) / 2;

    IPoint point = new PointClass();
    point.PutCoords(x, y);

    Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height);
    Graphics g = Graphics.FromImage(bmp);

    symbol.SetupDC(g.GetHdc().ToInt32(), null);
    try { symbol.Draw(point); }
    catch { }
    symbol.ResetDC();

    g.Dispose();

    return (Image)bmp;
}

For more details:

I used QueryBoundary but the dimensions returned after transforming to screen coordinates were wrong. Next is my code:

Here symbol is ITextSymbol and (x, y) are the lat, lon coordinates of the symbol.

IPolygon boundary = new PolygonClass();
boundary.SetEmpty();

IDisplay screenDisplay = axMapControl1.ActiveView.ScreenDisplay as IDisplay;
IDisplayTransformation displayTransformation = screenDisplay.DisplayTransformation;
IPoint point = new PointClass();
point.PutCoords(x, y);

symbol.QueryBoundary(screenDisplay.hDC, displayTransformation, point, boundary);

IPoint mapPoint = new PointClass();

int minX, minY;
mapPoint.PutCoords(boundary.Envelope.XMin, boundary.Envelope.YMin);
displayTransformation.FromMapPoint(mapPoint, out minX, out minY);

int maxX, maxY;
mapPoint.PutCoords(boundary.Envelope.XMax, boundary.Envelope.YMax);
displayTransformation.FromMapPoint(mapPoint, out maxX, out maxY);
1
2

If you need a formula for converting between points and pixels try here: Convert Pixels to Points - stackoverflow.com

2

Try using ISymbol.QueryBoundary to get a polygon, then transform the polygon's envelope (via the displaytransformation) back into pixels.

3
  • I've already tried it, but the returned dimensions were different from the suitable ones.
    – Angie
    Jun 27 '12 at 10:08
  • +1 I believe this is the best approach. I've successfully used these exact steps to get the image size realiably.
    – Petr Krebs
    Jun 28 '12 at 16:56
  • Would you please share the steps you've followed. I added some details to my question about what I've tested. could you please check it, maybe I made some mistakes.
    – Angie
    Jul 3 '12 at 12:18
2

to solve problem when ISymbol is ITextSymbol, i used this:

if (symbol is ITextSymbol)
{
    using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
    {
        stdole.IFontDisp font = (symbol as ITextSymbol).Font;
        size = graphics.MeasureString((symbol as ITextSymbol).Text, new Font(font.Name, (float)font.Size, FontStyle.Regular, GraphicsUnit.Point));

        size.Width = (size.Width / 72) * graphics.DpiX;
        size.Height = (size.Height / 72) * graphics.DpiY;
    }
}

( thanks blah238 )

0

I partially solved the problem. the next code shows how:

SizeF getSymbolSize(ISymbol symbol)
{
    SizeF size;

    if (symbol is ITextSymbol)
    {
        using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
        {
            stdole.IFontDisp font = (symbol as ITextSymbol).Font;
            size = graphics.MeasureString((symbol as ITextSymbol).Text, new Font(font.Name, (float)font.Weight, FontStyle.Regular, GraphicsUnit.Point));
        }
    }
    else if (symbol is IMarkerSymbol)
    {
        float s = (float)(symbol as IMarkerSymbol).Size;

        size = new SizeF(s, s);
    }
    else
    {
        size = new SizeF(150.0f, 150.0f);
    }

    return size;
}
1
  • This method does not solve the problem, it does not give the suitable image size. I hope someone has a better idea.
    – Angie
    Jun 11 '12 at 23:19
0

For a picture marker, you can try this:

if (symbol is IPictureMarkerSymbol)
            {
                IPictureMarkerSymbol pMarker = symbol as IPictureMarkerSymbol;
                pic = pMarker.Picture;
                size = new Size(pic.Width, pic.Height);
            }

And for convert the marker to image:

Image img = AxHostConverter.PictureDispToImage(pic);

more information about AxHostConverter class can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.