只需在C#中创建一个新的Windows窗体应用程序,并尝试使用此代码,它不能按预期工作:
private void Form1_Load(object sender, EventArgs e)
{
Bitmap rBitmap = new Bitmap(600, 500);
Graphics graphics = Graphics.FromImage(rBitmap);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, 600, 500), Color.DeepSkyBlue, Color.Green, LinearGradientMode.Vertical);
brush.SetSigmaBellShape(0.0f, 1f);
graphics.FillRectangle(brush, new Rectangle(0, 0, 600, 500));
Form1.ActiveForm.BackgroundImage = rBitmap;
}
表单加载默认值。如果向窗体中添加一个按钮并添加相同的代码,则可以根据需要设置窗体的背景图像。那么如何在Form1_Load事件中动态完成此操作,以便在程序打开时显示图像?
答案 0 :(得分:0)
在加载事件期间,表单未激活:
尝试:
this.BackgroundImage = rBitmap;
完成后处理你的物品:
Bitmap rBitmap = new Bitmap(600, 500);
using (Graphics g = Graphics.FromImage(rBitmap)) {
using (var br = new LinearGradientBrush(new Rectangle(0, 0, 600, 500),
Color.DeepSkyBlue, Color.Green, LinearGradientMode.Vertical)) {
br.SetSigmaBellShape(0.0f, 1f);
g.FillRectangle(br, new Rectangle(0, 0, 600, 500));
}
}
this.BackgroundImage = rBitmap;