views:

41

answers:

2

I'm trying to build a treeview like file list in a richtext box.

It should look like an explorer treeview. My code is able to get an resize the icon, but the transparency is missing (light gray background instead of transparency). What do I need to change here? Is the Image format wrong? Is there a better way to add an image to a richtextbox?

// Get file info
FileInfo f = new FileInfo("myfile.name");
// Get icon for fileinfo
Icon ico = Icon.ExtractAssociatedIcon(f);
// Convert icon to bitmap
Bitmap bm = ico.ToBitmap();
// create new image with desired size
Bitmap img = new Bitmap(16,16,PixelFormat.Frmat32bpRgb);
// Create graphics with desired sized image
Graphics g = Graphics.FormImage(img);
// set interpolation mode
g.InterpolationMode = InterpolationMode.HighQualityBiCubic;
// draw/resize image
g.DrawImage(bm, new Rectangle(0,0,16,16), new Rectangle(0, 0, bm.Width, bm,Height), GraphicalUnit.Pixel);
// Paste to clipboard
Clipboard.SetImage(bm);
// Paste in RichtextBox
rtb.Paste();

Example:

alt text

Edit:

I've figured out that the image is transparent, but using Clipboard.SetImage() doesn't publish it as transparent image.

Any ideas why and what can I do to fix it? Do I need to switch to a differn textbox control?

A: 

Try

img.MakeTransparent();

after you contruct it.

Note that this will change your PixelFormat to Format32bppArgb.

bufferz
I've tried that before - no change... I've tried: img.MakeTransparent(Color.Transparent) too...
Andreas Rehm
A: 

I've had some luck going through Graphics.

Bitmap b = new Bitmap(pbAssetLoaded.Width, pbAssetLoaded.Height);
using (Graphics g = Graphics.FromImage(b))
{
    g.DrawIcon(SystemIcons.Information, 0, 0);
}

This draws the icon with transparency to the Bitmap.

villecoder
I've figured out that the image is transparent, but using Clipboard.SetImage() doesn't publish it as transparent image.
Andreas Rehm