我想在 Visual Studio 2010,c# 中的按钮(它是复选框,但外观像按钮)上放置标志性符号。所以有人能告诉我该怎么做吗?
问问题
7823 次
3 回答
2
设置 Image 属性或通过这样的代码button.Image = new Bitmap("Click.jpg");
于 2012-07-12T13:45:37.673 回答
2
选择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);
于 2012-07-12T13:53:33.053 回答
1
于 2012-07-12T13:44:18.913 回答