c# - 如何设置 ContextMenuStrip 图像平滑模式?
问题描述
我希望ToolStripMenuItem
s 的图像质量更好,这样它们看起来就不会像素化。
源代码 1
private void button2_Click(object sender, EventArgs e)
{
ContextMenuStrip cms = new ContextMenuStrip();
cms.Items.Add("Test", Properties.Resources.path1092);
cms.Show(button2, Cursor.Position);
}
截屏
图像文件
源代码 2
这里我使用这种方法:
internal static Bitmap ResizeToFitBoundingBox(Image image, in Rectangle box)
{
float maxHeight = box.Width;
float maxWidth = box.Height;
float x = Math.Min(maxWidth / image.Width,
maxHeight / image.Height);
float newW = (float)image.Width * x;
float newH = (float)image.Height * x;
var bmp = new Bitmap((int)Math.Round(maxWidth),
(int)Math.Round(maxHeight));
bmp.MakeTransparent(Color.Empty);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.DrawImage(image, (bmp.Width - newW) / 2,
(bmp.Height - newH) / 2, newW, newH);
}
return bmp;
}
第一种方法如下所示:
private void button2_Click(object sender, EventArgs e)
{
ContextMenuStrip cms = new ContextMenuStrip();
ToolStripMenuItem item = new ToolStripMenuItem("Test");
cms.Items.Add(item);
cms.ImageList = new ImageList();
cms.ImageList.ColorDepth = ColorDepth.Depth32Bit;
cms.ImageList.TransparentColor = Color.Empty;
cms.ImageList.ImageSize = new Size(128, 128);
cms.ImageList.Images.Add(ResizeToFitBoundingBox(Properties.Resources.path1092,
new Rectangle(Point.Empty, new Size(128, 128))));
item.ImageTransparentColor = Color.Empty;
item.ImageIndex = 0;
item.ImageTransparentColor = Color.Empty;
cms.Show(button2, Cursor.Position);
}
截图 2
长宽比保持:
问题是一样的。
解决方案
推荐阅读
- java - Java:此代码片段的时间复杂度与嵌套循环:数据结构和算法
- javascript - 在状态更新时使用 eventListener 进行指数重新渲染
- r - 为什么 geom_pattern= gradient 不会在 RMarkdown 子页面中显示图形?
- javascript - Vue - 根据倒数计时器提前设置引导5进度条宽度
- c# - 有没有更简单的方法来转换 DateTime?到 DateTime.Date.ToString()
- python - 如何使用 matplotlib 在同一张图上绘制 2 组 (x,y) 值作为散点图(由线连接)?
- sql - 将最后一个外观添加到表中
- python - 我正在尝试完成一个程序,我会自动从 kithnyc.com 网站购买鞋子
- python - 如何使用 numpy 从 3x3 矩阵中选择 2x2 矩阵?
- python - numpy数组索引:每行的不同列