1

I'm trying to create an example program and I'm having issues creating the Bitmap needed for it. When I try running the code below I'm getting an ArgumentException.

I think this is being thrown because it cannot find the file on the disk. If this is the case where do I put the file in my project so it can find it? I've tried putting the file in the main project directory and have tried placing it within the debug and release folders.

If this is not what is causing the issue can someone point me in the right direction?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace Given
{
    public class Photo : Form
    {
        Image image;

        public Photo()
        {
            image = new Bitmap("jug.jpg"); // ArgumentException thrown here
            this.Text = "Lemonade";
            this.Paint += new PaintEventHandler(Drawer);
        }

        public virtual void Drawer(Object source, PaintEventArgs e)
        {
            e.Graphics.DrawImage(image, 30, 20);
        }
    }
}

namespace Photo_Decorator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Given.Photo());
        }
    }
}
1
  • 2
    What is the error message in the ArgumentException? (btw, loading an image inside a constructor is not my cup of tea) Commented Jan 29, 2013 at 22:59

2 Answers 2

1

It's throwing this because it can't find the JPG, and therefore can't create a proper bitmap object. You need to either place the image file in the same folder as the EXE (Debug? Release?), or specify the entire path to the image (e.g C:/jug.jpg). Hope this helps :)

Sign up to request clarification or add additional context in comments.

1 Comment

Turns out I was placing the file in the wrong project. When I placed the file in C:\ the project ran fine. Then I realized my mistake.
0

You'd need to put the file in the same folder as the executable (bin\Debug or bin\Release). You could place it in the bin folder and just use @"..\jug.jpg" as the path instead so it worked in both Debug and Release modes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.