Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Nancy Melucci
seal-mask
.a{fill-rule:evenodd;}techdegree
Nancy Melucci
Front End Web Development Techdegree Student 34,491 Points

Visual Studio Project can't find embedded resource.

I have an assignment due, and I pretty much have it all coded out. The project takes text from a file, parses it into variables and an array, and makes a grade report for a student.

I can do everything right except that even though the image IS embedded in the project, I can't develop a path to the image that will allow me to share it with the instructor.

The absolute path works just fine, but it won't work when I upload the project. This is unacceptable because I need to have it work on his computer. I have tried SO many ways to make the project find the file that is IN it. But nothing works (Properties, Resources etc.)

I am reproducing the code below. I've been a member now for four years, and I do ask a lot of questions but I also do most of the work myself whenever I can. This is the difference between a C and a B or A, I would be grateful for any help.

I've commented the absolute path line. The image files are in the SAME folder and also have been embedded.

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

namespace FinalGradeForms
{
    public partial class Form1 : Form
    {

        string studentName = "";
        string course = "";
        string semesterYr = "";
        string picFile = "";
        string proj1 = "";
        string proj2 = "";
        string proj3 = "";
        string proj4 = "";
        string proj5 = "";
        string proj6 = "";
        string midterm = "";
        string final = "";
        int counter = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void MenuStrip_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName);
                for (counter = 0; counter <=12; counter++)
                {
                    if (sr.Peek() != -1) // EOF check
                    {
                        var splitLine = sr.ReadLine().Split(',');
                        studentName = splitLine[0];
                        course = splitLine[1];
                        semesterYr = splitLine[2]; 
                        picFile = splitLine[3];
                        proj1 = "Project 1: " + splitLine[4];
                        proj2 = "Project 2: " + splitLine[5];
                        proj3 = "Project 3: " + splitLine[6];
                        proj4 = "Project 4: "+ splitLine[7];
                        proj5 = "Project 5: " + splitLine[8];
                        proj6 = "Project 6: " + splitLine[9];
                        midterm = "Midterm: " + splitLine[10];
                        final = "Final: " + splitLine[11];

                        string[] scores = new string[] { proj1, proj2, proj3, proj4, proj5, proj6, midterm, final };
                        //Image image1 = Image.FromFile(picFile);
                        //pictureBox1.Image = image1; // this dead code is one of my many attempts //here is the absolute path that works.

                        Image image1 = Image.FromFile(@"C:\\Users\\Nancy\\Documents\\Visual Studio 2015\\Projects\\FinalGradeForms\\Resources\\" + picFile);
                        pictureBox1.Image = image1;

                        nameInfoLabel.Text = studentName;
                        nameInfoLabel.Visible = true;
                        courseInfoLabel.Text = course;
                        courseInfoLabel.Visible = true;
                        semesterInfoLabel.Text = semesterYr;
                        semesterInfoLabel.Visible = true;
                        recordLstBox.Items.AddRange(scores);


                    }
                }
                sr.Close();
            }

            }

        }
    }

1 Answer

Nancy Melucci
seal-mask
.a{fill-rule:evenodd;}techdegree
Nancy Melucci
Front End Web Development Techdegree Student 34,491 Points

After trying (literally) about 50 things, this seemed to work:

Image image = Image.FromFile(@"..\..\Pictures\"+text+".png");
this.pictureBox1.Image = image;

I didn't need the png part.