【问题标题】:Where to place the coordinates of text in a C# graphic bitmap在 C# 图形位图中放置文本坐标的位置
【发布时间】:2014-01-08 14:29:56
【问题描述】:

我编写了一个 C# Render 方法,将热图渲染到 Grasshopper 画布上。 Grasshopper 是一个 Rhino 插件,它允许一个简单的 GUI 编程界面。

protected override void Render(Grasshopper.GUI.Canvas.GH_Canvas canvas, Graphics graphics, Grasshopper.GUI.Canvas.GH_CanvasChannel channel) {

            base.Render(canvas, graphics, channel);

            if (channel == Grasshopper.GUI.Canvas.GH_CanvasChannel.Wires) {
                var comp = Owner as KT_HeatmapComponent;
                if (comp == null)
                    return;

                List<HeatMap> maps = comp.CachedHeatmaps;
                if (maps == null)
                    return;

                if (maps.Count == 0)
                    return;

                int x = Convert.ToInt32(Bounds.X + Bounds.Width / 2);
                int y = Convert.ToInt32(Bounds.Bottom + 10);

                for (int i = 0; i < maps.Count; i++) {
                    Bitmap image = maps[i].Image;
                    if (image == null)
                        continue;

                    Rectangle mapBounds = new Rectangle(x, y, maps[i].Width, maps[i].Height);
                    //Rectangle mapBounds = new Rectangle(x, y, maps[i].Width * 10, maps[i].Height * 10);
                    mapBounds.X -= mapBounds.Width / 2;

                    Rectangle edgeBounds = mapBounds;
                    edgeBounds.Inflate(4, 4);

                    GH_Capsule capsule = GH_Capsule.CreateCapsule(edgeBounds, GH_Palette.Normal);
                    capsule.Render(graphics, Selected, false, false);
                    capsule.Dispose();

                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                    graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                    graphics.DrawImage(image, mapBounds);
                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Default;
                    graphics.DrawRectangle(Pens.Black, mapBounds);

                    y = edgeBounds.Bottom - (mapBounds.Height) - 4;
                }
            }
        }

目前,这种渲染方法会在画布上绘制这样的图像:

话虽如此,我想在顶部放置一些标题文本,并为 X 和 Y 轴添加标签,就像标准的热图图表一样。不过我对graphics组件的了解太有限了,还望各位大神帮忙。

我做了一些研究,似乎drawText() 方法可以做我想做的事:c# write text on bitmap

但我不确定在哪里指定坐标,同时在显示的图形顶部留一些空间来放置标题文本。

【问题讨论】:

    标签: c# canvas bitmap grasshopper rhino-commons


    【解决方案1】:

    GDI+ 使用的坐标系从左上角开始,即 (0,0) 右下角(fullimagewidth,fullimageheight)

    所以如果你需要在图像的左上角绘图,请使用

    //Position
    PointF drawPoint = new PointF(0F, 0F);
    // Draw string to screen.
    e.Graphics.DrawString("hey", drawFont, drawBrush, drawPoint);
    

    【讨论】:

    • 谢谢!让我试一试,
    • 不幸的是,这似乎不起作用。我想知道是不是因为先画了字符串,然后画了热图,覆盖了它。
    • 你定义了字体和画笔drawFont、drawBrush吗?我刚刚发布了一个伪代码。你也想要吗?
    • 我有drawFontnew Font("Thaoma", 8)drawBrushBrushes.Black。整个字符串:graphics.DrawString("yourText", new Font("Thaoma", 8), Brushes.Black, drawPoint);
    • 将颜色从黑色变为白色:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多