如何设置ContextMenuStrip图像平滑模式?

时间:2019-03-08 11:33:22

标签: c# winforms contextmenustrip .net-4.6.1

我希望提高ToolStripMenuItem的图像质量,以使它们看起来不像素化。

源代码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);
}

屏幕截图

context menu screenshot

图像文件

original image

源代码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

保持长宽比:

aspect ratio kept

问题是相同的。

0 个答案:

没有答案