Adjusting the physical dimensions of a Bitmap object

1.2k views Asked by At

I have a script that returns a heatmap based on a List of Color objects (they're RGB values derived from a Gradient component in a graphical "coding" software called Grasshopper), which looks like this:

enter image description here

Below is an excerpt of my C# heatmap-drawing method that returns a Bitmap.

  private Bitmap DrawHeatmap(List<Color> colors, int U, int V){
    colorHeatmapArray = new Color[colors.Count()];

    for(int i = 0; i < colors.Count(); i++){
      colorHeatmapArray[i] = colors[i];
    }

    // Create heatmap image.
    Bitmap map = new Bitmap(U, V, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    int x = 0;
    int y = 0;

    for(int i = 0; i < colors.Count(); i++){
      Color color = colorHeatmapArray[i];
      map.SetPixel(x, y, color);
      y++;
      if (y >= map.Height){
        y = 0;
        x++;
      }
      if (x >= map.Width){
        break;
      }
    }
    return map;
  }

The method I used to save the image is like this:

  private void saveBMP(){
    _heatmap.Save(Path); // Path is just a string declared somewhere
  }

_heatmap is an instance variable, declared like this: private Bitmap _heatmap;, where I stored the Bitmap object, using the DrawHeatmap() method.

The way I displayed the image on the "canvas" of Grasshopper relies on some Grasshopper-specific method, specifically, this snippet

RectangleF rec = Component.Attributes.Bounds;
rec.X = rec.Right + 10;
rec.Height = Height;
rec.Width = Width;

canvas.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
canvas.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
canvas.Graphics.DrawImage(_heatmap, GH_Convert.ToRectangle(rec));
canvas.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
canvas.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Default;
canvas.Graphics.DrawRectangle(Pens.Black, GH_Convert.ToRectangle(rec));

However, when I save the Bitmap object, the result I get is a slightly taller version of what I have on the canvas, which looks like this:

enter image description here

Doesn't look very pretty does it?

My question is - on calling the saveBMP() method, is there a way to manipulate the Bitmap to adjust the dimensions so it looks remotely like what I have on the canvas?

2

There are 2 answers

0
theGreenCabbage On

After some Googling it looks like I found a solution from this link

Specifically:

enter image description here

3
Dave On

Assuming the _heatmap is set from the output of the DrawHeatmap method, then its size should be being set at the point of initialisation in that method to U by V pixels. Once it's saved, verify from the saved file what the size of the output file (ie are it's dimensions as expected given the value of U and V that enter DrawHeatmap?

When you are drawing to the rectangle in the latter code section, are you using the same Height and Width values as earlier?