title: Displaying thumbnail icons 128x128 pixels or larger in a grid in ListView
body: <h2>Original Question (see Update below)</h2>
  
  <p>I have a WinForms program that needs a decent scrollable icon control with large icons (128x128 or larger thumbnails, really) that can be clicked to hilight or double clicked to perform some action. Preferably there would be minimal wasted space (short filename captions might be needed below each icon; if the filename is too long I can add an ellipsis).</p>
  
  <p><a href="https://i.stack.imgur.com/YHOug.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YHOug.png" alt="finished version of listview with proper colors, spacing, etc."></a><br>
  <sub>(source: <a href="http://www.updike.org/images/listview-great.png" rel="nofollow noreferrer">updike.org</a>)</sub>  </p>
  
  <p>I tried using a ListView with LargeIcon (default .View) and the results are disappointing:</p>
  
  <p><a href="https://i.stack.imgur.com/j7wIp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j7wIp.png" alt="screenshot showing tiny icons in LargeIcon view"></a><br>
  <sub>(source: <a href="http://www.updike.org/images/listview-poor.png" rel="nofollow noreferrer">updike.org</a>)</sub>  </p>
  
  <p>Perhaps I am populating the control incorrectly? Code:</p>
  
  <pre><code>        ImageList ilist = new ImageList();
          this.listView.LargeImageList = ilist;
          int i = 0;
          foreach (GradorCacheFile gcf in gc.files)
          {
              Bitmap b = gcf.image128;
              ilist.Images.Add(b);
              ListViewItem lvi = new ListViewItem("text");
              lvi.ImageIndex = i;
              this.listView.Items.Add(lvi);
              i++;
          }
  </code></pre>
  
  <p>I need large icons with little empty space, not large empty space with embarrassingly small icons.</p>
  
  <ol>
  <li>Is there a .NET control that does what I need?
  
  <ul>
  <li>Is there a favorite third party control that does this?</li>
  <li>If not, which control would be best to inherit and tweak to make it work?</li>
  <li>Should I break down and make a custom Control (which I have plenty of experience with... just don't want to go to that extreme since that is somewhat involved).</li>
  </ul></li>
  </ol>
  
  <p>I found <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw.aspx" rel="nofollow noreferrer">this tutorial about OwnerDraw</a> but work from that basically amounts to number 3 or 4 above since that demo just shows how to spice up the rows in the details view.</p>
  
  <h2>Update</h2>
  
  <p>Adding the line</p>
  
  <pre><code>ilist.ImageSize = new Size(128, 128);
  </code></pre>
  
  <p>before the for loop fixed the size problem but now the images are palette-ized to 8-bit (looks like system colors?) even though the debugger shows that the images are inserted into the ImageList as 24bpp System.Drawing.Bitmap's:</p>
  
  <p><a href="https://i.stack.imgur.com/X7p25.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X7p25.png" alt="large icons, finally"></a><br>
  <sub>(source: <a href="http://www.updike.org/images/listview-poor2.png" rel="nofollow noreferrer">updike.org</a>)</sub>  </p>
  
  <ol>
  <li>How do I (can I?) make the images show in full 24 bit color?
  
  <ul>
  <li>The spacing around the icons is still rather wasteful... how do I fix that? Can I?</li>
  </ul></li>
  </ol>
  
  <h2>Update 2</h2>
  
  <p>Along with adding the line</p>
  
  <pre><code>ilist.ColorDepth = ColorDepth.Depth24Bit;
  </code></pre>
  
  <p>next after setting ilist.ImageSize, I followed arbiter's advice and changed the spacing:</p>
  
  <pre><code>[DllImport("user32.dll")]
  public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  
  public int MakeLong(short lowPart, short highPart)
  {
      return (int)(((ushort)lowPart) | (uint)(highPart &lt;&lt; 16));
  }
  
  public void ListView_SetSpacing(ListView listview, short cx, short cy)
  {
      const int LVM_FIRST = 0x1000;
      const int LVM_SETICONSPACING = LVM_FIRST + 53;
      // http://msdn.microsoft.com/en-us/library/bb761176(VS.85).aspx
      // minimum spacing = 4
      SendMessage(listview.Handle, LVM_SETICONSPACING,
      IntPtr.Zero, (IntPtr)MakeLong(cx, cy));
  
      // http://msdn.microsoft.com/en-us/library/bb775085(VS.85).aspx
      // DOESN'T WORK!
      // can't find ListView_SetIconSpacing in dll comctl32.dll
      //ListView_SetIconSpacing(listView.Handle, 5, 5);
  }
  
  ///////////////////////////////////////////////////////////
  
  ListView_SetSpacing(this.listView, 128 + 12, 128 + 4 + 20);
  </code></pre>
  
  <p>The ListView control may not be perfect or have the defaults I'd expect (like a Spacing property) but I'm glad I could tame it, in the end:</p>
  
  <p><a href="https://i.stack.imgur.com/YHOug.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YHOug.png" alt="alt text"></a><br>
  <sub>(source: <a href="http://www.updike.org/images/listview-great.png" rel="nofollow noreferrer">updike.org</a>)</sub>  </p>
  
  <p>By the way, to maintain the proper aspect ratio for the thumbnails, I had to make my own 128x128 bitmaps, clear the background to match the control, and center those images:</p>
  
  <pre><code>public void CenterDrawImage(Bitmap target, Color background, Bitmap centerme)
  {
      Graphics g = Graphics.FromImage(target);
      g.Clear(background);
      int x = (target.Width - centerme.Width) / 2;
      int y = (target.Height - centerme.Height) / 2;
      g.DrawImage(centerme, x, y);
      g.Dispose();
  }
  </code></pre>