Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icon to Image - transparency issue

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?

like image 938
Andreas Rehm Avatar asked Nov 06 '22 08:11

Andreas Rehm


1 Answers

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.

like image 164
villecoder Avatar answered Nov 09 '22 12:11

villecoder