我从应用资源中将图像插入RichTextBox。图像格式为PNG,背景是透明的。插入后,图像背景为灰色。如何将图像背景设置为透明?
我目前的代码:
private Hashtable icons = null;
private void LoadIcons()
{
icons = new Hashtable(3);
icons.Add("[inf]", Properties.Resources.inf);
icons.Add("[ok]", Properties.Resources.ok);
icons.Add("[err]", Properties.Resources.err);
}
private void SetIcons()
{
richTextBox.ReadOnly = false;
foreach (string icon in icons.Keys)
{
while (richTextBox.Text.Contains(icon))
{
IDataObject tmpClibboard = Clipboard.GetDataObject();
int index = richTextBox.Text.IndexOf(icon);
richTextBox.Select(index, icon.Length);
Clipboard.SetImage((Image)icons[icon]);
richTextBox.Paste();
Clipboard.SetDataObject(tmpClibboard);
}
}
richTextBox.ReadOnly = true;
}
private void richTextBox_TextChanged(object sender, EventArgs e)
{
SetIcons();
}
答案 0 :(得分:1)
我遇到了同样的问题,我的解决方案是使用您的图标大小创建新的空位图,然后将其背景设置为richtextbox背景颜色。之后,我用图形对象绘制了上一个位图上的图标。
以下是代码:
创建一个具有图标大小的位图(此处为来自资源文件的警告)
Bitmap img = new Bitmap(Icons.warning.Width, Icons.warning.Height);
从此位图创建图形对象
Graphics graphics = Graphics.FromImage(img);
在richtextbox背景上设置位图的背景
graphics.Clear(richTextBox.BackColor);
然后在位图上叠加您的图标
graphics.DrawImage(Icons.warning,Point.Empty);
ps:抱歉我的英文不好;)
和平:)
答案 1 :(得分:0)
<强> There is no such thing as true transparency in a WinForms Control. Transparent mode inherits the default background of its parent. The way I have worked around it in the past has been to use the OnPaint event and then use the Graphics.DrawString method to position the text where I want it.
强>
尝试