【问题标题】:Bitmap ArgumentException位图参数异常
【发布时间】:2013-01-29 22:56:29
【问题描述】:

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

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

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

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

【问题讨论】:

  • ArgumentException 中的错误信息是什么? (顺便说一句,在构造函数中加载图像不是我的一杯茶)

标签: c# exception bitmap


【解决方案1】:

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

【讨论】:

  • 原来我把文件放在了错误的项目中。当我将文件放在 C:\ 中时,项目运行良好。然后我意识到我的错误。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2012-01-27
    相关资源
    最近更新 更多