5 votos

Cómo obtener el tamaño exacto de la imagen ISymbol en píxeles

He encontrado el siguiente código para convertir ISymbol en imagen de mapa de bits, pero ¿cómo saber el tamaño de imagen adecuado? (supongamos que tenemos un 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;
}

Para más detalles:

Utilicé QueryBoundary pero las dimensiones devueltas después de la transformación a coordenadas de pantalla eran erróneas. El siguiente es mi código:

Aquí symbol es ITextSymbol y (x, y) son las coordenadas lat, lon del símbolo.

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);

0 votos

Quizás puedas encontrar algo útil en mis comentarios a esta pregunta relacionada: Crear una tabla resumen de la simbología de todos los conjuntos de datos en múltiples mxd's

2voto

saint_groceon Puntos 2696

Pruebe a utilizar ISymbol.QueryBoundary para obtener un polígono, y luego transformar la envolvente del polígono (a través de la transformación de la pantalla) de nuevo en píxeles.

0 votos

Ya lo he intentado, pero las dimensiones devueltas eran diferentes a las adecuadas.

0 votos

+1 Creo que este es el mejor enfoque. He utilizado con éxito estos pasos exactos para obtener el tamaño de la imagen realiably.

0 votos

¿Podría compartir los pasos que ha seguido? He añadido algunos detalles a mi pregunta sobre lo que he probado. podría por favor revisarlo, tal vez he cometido algunos errores.

2voto

auramo Puntos 161

Si necesitas una fórmula para convertir entre puntos y píxeles prueba aquí: Convertir píxeles en puntos - stackoverflow.com

2voto

3dinfluence Puntos 478

Para resolver el problema cuando ISymbol es ITextSymbol, utilicé esto:

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;
    }
}

( gracias blah238 )

0voto

3dinfluence Puntos 478

He resuelto parcialmente el problema. El siguiente código muestra cómo:

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;
}

0 votos

Este método no resuelve el problema, no da el tamaño de imagen adecuado. Espero que alguien tenga una idea mejor.

0voto

Paul Schreiber Puntos 48

Para un marcador de imágenes, puedes probar esto:

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

Y para convertir el marcador en imagen:

Image img = AxHostConverter.PictureDispToImage(pic);

Puede encontrar más información sobre la clase AxHostConverter aquí .

i-Ciencias.com

I-Ciencias es una comunidad de estudiantes y amantes de la ciencia en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X