2010-09-20 85 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