【问题标题】:Setting the background image of a form using an external file - Windows Forms使用外部文件设置窗体的背景图像 - Windows 窗体
【发布时间】:2014-03-27 02:54:23
【问题描述】:

我正在尝试为我的 Windows Froms 应用程序添加一个选项,该选项将允许用户将某个表单的背景更改为他计算机中的图片(比如说 .png 类型的文件)...

如何从计算机中获取文件并将其“转换”为图像类型?我是否需要以某种方式将其添加到 properties.resources lib? (如果是,怎么做?)。

我尝试过这样做(显然,它没有用......):

var FD = new System.Windows.Forms.OpenFileDialog();
FD.Filter = "png files|*.png";
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string fileToOpen = FD.FileName;

    System.IO.FileInfo File = new System.IO.FileInfo(FD.FileName);
    BackgroundImage = FD.OpenFile();
}

【问题讨论】:

  • 如果您希望在用户关闭应用程序并重新打开它时使用图像,您必须找到一种方法来保存图像文件及其路径。查看答案here
  • 不是我需要的,但无论如何都能提供帮助......谢谢!

标签: c# winforms


【解决方案1】:

替换此行

      BackgroundImage = FD.OpenFile();

到这个

      BackgroundImage = Image.FromFile(FD.FileName); 

【讨论】:

【解决方案2】:

尝试这样在表单中绘制加载的图像

OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
  Graphics graphics = this.CreateGraphics();
  Image image = new Bitmap(openDialog.FileName);
  graphics.DrawImage(image, new Point(0, 0));
}

如果需要,您可以添加过滤器选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多