选择复选框的Image 属性。选择Local resource > Import 并导航到您的图标文件。默认情况下不会显示图标文件,因此您需要选择All Files (*.*)过滤器。
如果你想从代码中设置图标,你可以这样做:
checkBox.Image = new Icon(pathToIconFile).ToBitmap();
更新:您不能缩放或拉伸通过Image 属性分配的图像。在这种情况下,您需要改用 BackgrounImage 属性:
checkBox.BackgroundImage = new Icon(pathToIconFile).ToBitmap();
checkBox.BackgroundImageLayout = ImageLayout.Stretch;
您也可以通过编程方式调整图像大小,或通过OnPaint 方法手动绘制,但这需要更多的努力。
更新:调整图像大小
public static Bitmap ResizeImage(Image image, Size size)
{
Bitmap result = new Bitmap(size.Width, size.Height);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
return result;
}
用法:
const int padding = 6;
Size size = new Size(checkBox.Width - padding, checkBox.Height - padding);
checkBox.Image = ResizeImage(new Icon(pathToIconFile).ToBitmap(), size);