robhasacamera
robhasacamera

Reputation: 3346

Bitmap ArgumentException

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());
        }
    }
}

Upvotes: 1

Views: 2718

Answers (2)

Toby Person
Toby Person

Reputation: 211

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 :)

Upvotes: 1

itsme86
itsme86

Reputation: 19526

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.

Upvotes: 0

Related Questions