Changing Form's Background Image Causes Exception Unhandled Problem

Viewed 29

I think the problem is caused by this code here:

this.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "door.png");

I want to use this code but I don't know how to solve this Exception Unhandled error that is seen in the below image:

enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimerUygulamalari
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            timer1.Start();
        }

        int say = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            say++;
            label1.Text = say.ToString();
            if (say == 10)
            {
                this.AutoSize = true;
                this.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "door.png");
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            }
        }


    }
}

enter image description here How can I solve this error? I want to be able to use this specific code method to give background image.

2 Answers

It should be:

@"\door.png"

It should not be:

"door.png"

Silly mistake...

You just need to add a \ slash at the begin of the string "door.png"

this.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\door.png");

Edit: Fixed the necessary folder path syntax

Related