图标转图像-透明度问题

5
我正在尝试在富文本框中构建类似文件列表的树形视图。它应该看起来像资源管理器树形视图。我的代码可以获取并调整图标大小,但透明度缺失(浅灰色背景代替透明)。我需要在这里做什么更改?图片格式是否有误?是否有更好的方法将图像添加到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();

示例:

替代文本

编辑:

我已经发现这个图片是透明的,但是使用Clipboard.SetImage()将其发布为不透明图像。

有没有什么想法,我该如何解决?我需要切换到另一个文本框控件吗?


我不理解。为什么不实际使用TreeView呢?ImageList可以轻松处理图标。 - Hans Passant
因为我需要打印它 - 如果我使用树形视图,会出现多页问题...而且我正在使用一个 imagelist 作为缓存 - 这只是当前问题的一部分。 - Andreas Rehm
2个回答

2

我在处理图形方面有一些运气。

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

这将带有透明度的图标绘制到位图中。


我已经发现这张图片是透明的,但是使用Clipboard.SetImage()方法不能将其发布为透明图片。 - Andreas Rehm
1
对我没有用,仍然得到黑色背景。 - Prat

0

尝试

img.MakeTransparent();

在你构建它之后。

请注意,这将更改您的PixelFormat为Format32bppArgb


我以前试过了 - 没有变化... 我也试过:img.MakeTransparent(Color.Transparent)... - Andreas Rehm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接