2014-01-16 181 views
0

我目前正在研究将位图渲染到Grasshopper画布上的直方图渲染器。共有两个位图的是,他们都低于渲染位图的问题

private readonly Bitmap _image;

解释和:

private readonly Bitmap _overlayedImage;

Bitmap实例名为_image看起来是这样的:

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

虽然t他Bitmap实例名称为_overlayedImage看起来是这样的:

enter image description here

基本上,_overlayedImage是使用_image位图创建位图,顾名思义,覆盖文本(你可以在看我发布的图片)并添加了黑色背景。这是它是如何分配

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

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


我现在有一个问题是多方面的。

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

enter image description here

的代码是这样的:

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像素。


我对这个长得不错的问题表示歉意,但我不认为我可以用任何其他方式解释它。

+1

你的两个位图有不同的尺寸。你应该使用不同的地图[i] .Width/Height来渲染? (因为你的_overlayedImage是size的3倍) –

+0

有趣的是,当我试图找出'_maps'的'width'和'height'时,我的'Render()'根本不会被调用,因为默认的Grasshopper行为如果尺寸不匹配,则不调用Render()。有关如何查找'_maps'的维度的任何想法? – theGreenCabbage

+0

哦,我想出了如何找出价值。 – theGreenCabbage

回答

0

原来有两个问题:

在我用我的_overlayedImage.Dispose()主要方法,从而有效地破坏了图像之前就被显示在画布上。

此外,我的问题隔离也是正确的。这条线导致的事渲染正确:

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

结果组件:

enter image description here