C#中设置窗口背景图像的问题

来源:百度知道 编辑:UC知道 时间:2024/05/13 02:27:04
我是初学者,哪位帮我看一下哪里出错了,这是一个设置窗口背景图的程序
using System.Windows.Forms;
using System.Drawing;
public class MyWindow:Form
{
public static void Main()
{
MyWindow mw=new MyWindow();
mw.Text="背景";
mw.BackgroundImage=Image.FromFile("F:\k.jpg");
Size tempsize=new Size();
tempsize.Width=mw.BackgroundImage.Width;
tempsize.Height=mw.BackgroundImage.Height;
mw.ClientSize=tempsize;
Application.Run(mw);
}
}
编译时下面这句出错不能通过:
mw.BackgroundImage=Image.FromFile("F:\k.jpg");
我用的是shorpdevelop编译的,显示错误是:
Unrecognized escape sequence (CS1009)

字符串中'\'前是需要加上'\'的!
把mw.BackgroundImage=Image.FromFile("F:\k.jpg");
改成mw.BackgroundImage=Image.FromFile("F:\\k.jpg");
这才是问题的关键所在!

using System.Windows.Forms;
using System.Drawing;

public class MyWindow //去掉对Form的继承
{
public static void Main()
{
MyWindow mw=new Form();//这样创建窗口。原来你的做法太诡异了。
mw.Text="背景";
mw.BackgroundImage=Image.FromFile("F:\k.jpg");
Size tempsize=new Size();
tempsize.Width=mw.BackgroundImage.Width;
tempsize.Height=mw.BackgroundImage.Height;
mw.ClientSize=tempsize;
Application.Run(mw);
}
}