我正在尝试创建一个示例程序,但我在创建它所需的位图时遇到了问题。当我尝试运行下面的代码时,我得到了一个ArgumentException。
我认为这是被抛出的,因为它在磁盘上找不到文件。如果是这种情况,我应该将文件放在项目中的什么位置,以便它可以找到它?我已经尝试将该文件放在主项目目录中,并尝试将其放在debug和release文件夹中。
如果这不是导致问题的原因,有人能给我指出正确的方向吗?
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());
}
}
}发布于 2013-01-30 07:04:08
它之所以抛出这个错误,是因为它找不到JPG,因此无法创建正确的位图对象。您需要将图像文件放在与EXE相同的文件夹中(Debug?Release?),或指定镜像的完整路径(例如C:/jug.jpg)。希望这能有所帮助:)
发布于 2013-01-30 06:59:34
您需要将该文件放在与可执行文件相同的文件夹中(bin\Debug或bin\Release)。您可以将它放在bin文件夹中,只需使用@"..\jug.jpg“作为路径,这样它就可以在调试和发布模式下工作。
https://stackoverflow.com/questions/14593609
复制相似问题