2010-09-20 131 views
5

我试图在richtext框中构建一个像树形视图文件列表。图标图像 - 透明度问题

它应该看起来像一个资源管理器树视图。我的代码能够调整图标的大小,但缺少透明度(浅灰色背景而不是透明度)。我需要在这里更改什么?图像格式是否错误? 有没有更好的方式将图像添加到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(); 

实施例:

alt text

编辑:

我已想出该图像是透明的,但使用Clipboard.SetImage()不发布为透明的图像。

任何想法为什么和我能做些什么来解决它?我是否需要切换到不同的文本框控件?

+0

我不明白。为什么不实际使用TreeView? ImageList没有麻烦的图标。 – 2010-09-20 23:19:36

+0

因为我需要打印它 - 如果我使用树视图,这是多个页面的问题......并且我使用imagelist作为缓存 - 这只是当前问题的一部分。 – 2010-09-21 22:54:48

回答

0

尝试

img.MakeTransparent(); 

你contruct后。

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

+0

我试过之前 - 没有改变...我试过了:img.MakeTransparent(Color.Transparent)... – 2010-09-21 22:54:09

2

我已经有一些运气通过图形。

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

这会在位图上绘制透明图标。

+0

我已经知道图像是透明的,但使用Clipboard.SetImage()不会将其发布为透明图像。 – 2010-10-07 22:02:16

+1

没有为我工作,仍然得到一个黑色的背景 – Prat 2014-01-27 11:50:30