首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >位图ArgumentException

位图ArgumentException
EN

Stack Overflow用户
提问于 2013-01-30 06:56:29
回答 2查看 2.2K关注 0票数 1

我正在尝试创建一个示例程序,但我在创建它所需的位图时遇到了问题。当我尝试运行下面的代码时,我得到了一个ArgumentException。

我认为这是被抛出的,因为它在磁盘上找不到文件。如果是这种情况,我应该将文件放在项目中的什么位置,以便它可以找到它?我已经尝试将该文件放在主项目目录中,并尝试将其放在debug和release文件夹中。

如果这不是导致问题的原因,有人能给我指出正确的方向吗?

代码语言:javascript
复制
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());
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-30 07:04:08

它之所以抛出这个错误,是因为它找不到JPG,因此无法创建正确的位图对象。您需要将图像文件放在与EXE相同的文件夹中(Debug?Release?),或指定镜像的完整路径(例如C:/jug.jpg)。希望这能有所帮助:)

票数 1
EN

Stack Overflow用户

发布于 2013-01-30 06:59:34

您需要将该文件放在与可执行文件相同的文件夹中(bin\Debug或bin\Release)。您可以将它放在bin文件夹中,只需使用@"..\jug.jpg“作为路径,这样它就可以在调试和发布模式下工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14593609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档