图标到图像 – 透明度问题
我正在尝试在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();
例:
编辑:
我已经发现图像是透明的,但使用Clipboard.SetImage()不会将其发布为透明图像。
任何想法为什么以及我可以做些什么来解决它? 我是否需要切换到不同的文本框控件?
我有幸运过Graphics。
Bitmap b = new Bitmap(pbAssetLoaded.Width, pbAssetLoaded.Height); using (Graphics g = Graphics.FromImage(b)) { g.DrawIcon(SystemIcons.Information, 0, 0); }
这将为Bitmap绘制透明图标。
尝试
img.MakeTransparent();
在你构建它之后。
请注意,这会将PixelFormat更改为Format32bppArgb
。