【问题标题】:Icon to Image - transparency issue图标到图像 - 透明度问题
【发布时间】:2011-04-14 22:32:09
【问题描述】:

我正在尝试在富文本框中构建类似树视图的文件列表。

它应该看起来像一个资源管理器树视图。我的代码能够调整图标大小,但缺少透明度(浅灰色背景而不是透明度)。我需要在这里改变什么?图片格式错了吗? 有没有更好的方法将图像添加到富文本框?

// 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 可以轻松制作图标。
  • 因为我需要打印它——如果我使用树视图,多页就会出现问题......而且我使用图像列表作为缓存——这只是当前问题的一部分。跨度>

标签: c# .net image icons transparency


【解决方案1】:

我在 Graphics 上遇到了一些运气。

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

这会在位图上绘制具有透明度的图标。

【讨论】:

  • 我发现图像是透明的,但使用 Clipboard.SetImage() 不会将其发布为透明图像。
  • 对我不起作用,仍然是黑色背景
【解决方案2】:

试试

img.MakeTransparent();

在你建造之后。

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

【讨论】:

  • 我之前尝试过 - 没有变化...我也尝试过:img.MakeTransparent(Color.Transparent)...
猜你喜欢
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 2010-11-09
  • 2011-01-28
  • 2012-03-25
  • 2010-10-30
  • 2015-10-08
  • 2011-09-08
相关资源
最近更新 更多