【问题标题】:Issues with Rendering a Bitmap渲染位图的问题
【发布时间】:2014-01-16 16:46:15
【问题描述】:

我目前正在开发一个将位图渲染到 Grasshopper 画布上的直方图渲染器。一共有两张位图,下面分别解释一下

private readonly Bitmap _image;

和:

private readonly Bitmap _overlayedImage;

名称为_imageBitmap 实例如下所示:

_bitmap http://puu.sh/6mUk4/20b879710a.png

而名称为 _overlayedImageBitmap 实例看起来像这样:

基本上,_overlayedImage 是使用_image 位图创建的位图,顾名思义,它覆盖了文本(您可以在我发布的图像中看到)并为其添加黑色背景。是这样分配的

_overlayedImage = overlayBitmap(_image, width * 3, height * 3, times, dates, colors);

* 3 用于调整图像大小)。


我目前遇到的一个问题是多方面的。

使用这种方法,我可以将_image 渲染到画布上。

代码是这样的:

    protected override void Render(Grasshopper.GUI.Canvas.GH_Canvas canvas, Graphics graphics, Grasshopper.GUI.Canvas.GH_CanvasChannel channel) {
        // Render the default component.
        base.Render(canvas, graphics, channel);

        // Now render our bitmap if it exists.
        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].overlayedImage;
                if (image == null)
                    continue;

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

                Rectangle edgeBounds = mapBounds;

                GH_Capsule capsule = GH_Capsule.CreateCapsule(edgeBounds, GH_Palette.Normal);
                capsule.Render(graphics, Selected, false, false);
                capsule.Dispose();
                graphics.DrawImage(image, mapBounds);
                graphics.DrawRectangle(Pens.Black, mapBounds);

                // some graphics interpolation and bicubic methods

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

根据comp.CachedHeatmaps; 是什么:

    private readonly List<HeatMap> _maps = new List<HeatMap>();

    internal List<HeatMap> CachedHeatmaps {
        get { return _maps; }
    }

但是,每当我尝试在 _overlayedImage 上使用 Render() 时,我都无法这样做。

我已将问题隔离到Render() 方法,似乎这条线

Rectangle mapBounds = new Rectangle(x, y, maps[i].Width, maps[i].Height);是主要问题,因为maps[i].Widthmaps[i].Height分别返回1100,巧合的是图例的尺寸,垂直100像素,水平1像素。


对于这个相当长的问题,我深表歉意,但我认为我无法用其他方式解释它。

【问题讨论】:

  • 您的两个位图具有不同的尺寸。您是否应该使用不同的地图[i].Width/Height 进行渲染? (因为你的 _overlayedImage 是大小的 3 倍)
  • 有趣的是,当我试图找出 _mapswidthheight 时,我的 Render() 甚至根本没有被调用,因为默认的 Grasshopper 行为不会调用Render() 如果尺寸不匹配。关于如何找到_maps 的尺寸的任何想法?
  • 哦,我想出了如何找出价值。
  • 你好天云。你是对的,有一个不匹配。我修好了-谢谢!!!

标签: c# canvas graphics bitmap grasshopper


【解决方案1】:

原来有两个问题:

在我的主要方法中,我使用了_overlayedImage.Dispose(),它甚至在图像显示到画布上之前就有效地破坏了图像。

另外,我的问题隔离也是正确的。此行导致事物正确呈现:

Rectangle mapBounds = new Rectangle(x, y, maps[i].overlayedImage.Width, maps[i].overlayedImage.Height);

结果组件:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多