C# 图标到图像-透明度问题

C# 图标到图像-透明度问题,c#,.net,image,icons,transparency,C#,.net,Image,Icons,Transparency,我试图在richtext框中构建一个类似treeview的文件列表 它应该看起来像一个探索者树视图。我的代码可以调整图标的大小,但是缺少透明度(浅灰色背景而不是透明度)。我需要在这里换什么?图像格式是否错误? 有没有更好的方法将图像添加到richtextbox // Get file info FileInfo f = new FileInfo("myfile.name"); // Get icon for fileinfo Icon ico = Icon.ExtractAssociatedIc

我试图在richtext框中构建一个类似treeview的文件列表

它应该看起来像一个探索者树视图。我的代码可以调整图标的大小,但是缺少透明度(浅灰色背景而不是透明度)。我需要在这里换什么?图像格式是否错误? 有没有更好的方法将图像添加到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();
例如:

编辑:


我发现图像是透明的,但使用剪贴板。SetImage()不会将其发布为透明图像

有什么想法吗?为什么?我能做些什么来修复它?我需要切换到不同的文本框控件吗?

试试看

img.MakeTransparent();
在你建造它之后


请注意,这会将您的像素格式更改为
Format32bppArgb

我很幸运地浏览了图形

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

这会将具有透明度的图标绘制到位图。

我不明白。为什么不使用树状视图呢?ImageList不需要麻烦就可以打印图标。因为我需要打印它-如果我使用树状视图,那么多个页面都有问题。。。我正在使用imagelist作为缓存-这只是当前问题的一部分。我以前尝试过-没有更改。。。我也尝试过:img.MakeTransparent(Color.Transparent)…我已经知道图像是透明的,但是使用剪贴板。SetImage()不会将其发布为透明图像。对我来说不起作用,仍然得到黑色背景