c# - 在 C# 图形位图中放置文本坐标的位置

标签 c# canvas bitmap grasshopper rhino-commons

我编写了一个 C# 渲染方法,将热图渲染到 Grasshopper Canvas 上。 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;
                }
            }
        }

目前,此渲染方法将这样的图像绘制到 Canvas 上:

enter image description here

话虽如此,我想在顶部放置一些标题文本,并在 X 轴和 Y 轴上放置标签,就像标准热图一样。但是,我对graphics组件的理解太有限了,还望各位大侠指点迷津。

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

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

最佳答案

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

enter image description here

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

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

关于c# - 在 C# 图形位图中放置文本坐标的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998413/

相关文章:

c# - EF 6 连接池和存储的查询字符串 RAM 泄漏

c# - 关于 XAML、代码隐藏和 MVVM

delphi - 为什么 tfVerticalCenter 和 tfBottom 在 TCanvas.TextRect 中不能按预期工作?

android - 在 Android 中通过 Canvas 创建一个空位图并进行绘图

c - 从文件中读取模式并在 C 中创建该模式的 bmp 图像

c# - 如何获取每个 DbContext 下的所有 DbContext 类和所有 DbSet

c# - 面向方面编程?

javascript - 在工具提示中显示标签但不在 chartjs 折线图的 x 轴中显示标签

javascript - HTML5 canvas 游戏将子弹射向鼠标点。

javascript - EaseJS 将颜色滤镜应用于位图