Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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);
share|improve this question
    
Maybe you can find something useful in my comments to this related question: Create a summary table of the symbology of all datasets in multiple mxd's –  blah238 Jun 26 '12 at 1:26
add comment

4 Answers 4

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

share|improve this answer
add comment

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

share|improve this answer
    
I've already tried it, but the returned dimensions were different from the suitable ones. –  lio 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. –  lio Jul 3 '12 at 12:18
add comment

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 )

share|improve this answer
add comment

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;
}
share|improve this answer
    
This method does not solve the problem, it does not give the suitable image size. I hope someone has a better idea. –  lio Jun 11 '12 at 23:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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