Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异常:参数无效(将新图像传递到pictureBox时)_C#_Image_Dispose - Fatal编程技术网

C# 异常:参数无效(将新图像传递到pictureBox时)

C# 异常:参数无效(将新图像传递到pictureBox时),c#,image,dispose,C#,Image,Dispose,我已经在PictureBox控件中有了一个图像,现在我想向它传递一个新的图像 发生的是allpication处理(我捕获到一个异常:“参数无效”) 这是我的代码: using (Image img = Image.FromFile(open.FileName)) { part.Picture = img; pictureBox1.InitialImage = null;

我已经在PictureBox控件中有了一个图像,现在我想向它传递一个新的图像

发生的是allpication处理(我捕获到一个异常:“参数无效”)

这是我的代码:

            using (Image img = Image.FromFile(open.FileName))
            {
                part.Picture = img;
                pictureBox1.InitialImage = null;
                pictureBox1.Image = img;
            }    
因此,当代码退出方法时,它会很快地显示此和主窗体。我只在Form1开始的那一行捕捉到异常。在这件事上没有任何例外。 pictureBox在绘画(内部绘画活动)时一定是出了什么问题,但我并没有被误解

我真的不知道该如何解决这个问题了。我甚至尝试使用清除所有资源(通过调用垃圾收集),但似乎没有任何效果


还有一件事:“part”是列表的引用,因此当我尝试删除当前图像(用新图像替换)时,我遇到了另一个异常,如:

“该进程无法访问该文件,因为另一进程正在使用该文件”



这是否与第一个异常有关(当新图像未在pictureBox中绘制时)?

问题是,在执行此代码后,
pictureBox1。image
指的是已处理的
图像

如果您没有使用
将创建的
图像
包装在
中,则应纠正您的问题

Image img = Image.FromFile(open.FileName);
part.Picture = img;
pictureBox1.InitialImage = null;
pictureBox1.Image = img; // You can't dispose of this, or it won't be valid when PictureBox uses it!

您还可以执行以下操作:创建一个加载图像的方法,然后将其传递回图像控件。例如,这就是我在填充图像控件时使用的方法

我有一个windows窗体,其中有3个不同的图像,我想加载,但我只显示其中一个的代码,因为我对所有3个图像控件调用了相同的方法

    #region Codes for browsing for a picture
    /// <summary>
    /// this.picStudent the name of the Image Control
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnStudentPic_Click(object sender, EventArgs e)
    {
        Image picture = (Image)BrowseForPicture();
        this.picStudent.Image = picture;
        this.picStudent.SizeMode = PictureBoxSizeMode.StretchImage;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private Bitmap BrowseForPicture()
    {
       // Bitmap picture = null;

        try
        {
            if (this.fdlgStudentPic.ShowDialog() == DialogResult.OK)
            {
                byte[] imageBytes = File.ReadAllBytes(this.fdlgStudentPic.FileName);
                StudentPic = new Bitmap( this.fdlgStudentPic.FileName);
                StuInfo.StudentPic = imageBytes;
            }
            else
            {
                StudentPic = Properties.Resources.NoPhotoAvailable;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("That was not a picture.", "Browse for picture");
            StudentPic = this.BrowseForPicture();
        }

        return StudentPic;
    }
    #endregion
#用于浏览图片的区域代码
/// 
///this.picStudent是图像控件的名称
/// 
/// 
/// 
私有void btnStudentPic_单击(对象发送者,事件参数e)
{
图像图片=(图像)浏览图片();
this.picStudent.Image=图片;
this.picStudent.SizeMode=PictureBoxSizeMode.StretchImage;
}
/// 
/// 
/// 
/// 
私有位图BrowseForPicture()
{
//位图图片=空;
尝试
{
if(this.fdlgStudentPic.ShowDialog()==DialogResult.OK)
{
byte[]imageBytes=File.ReadAllBytes(此.fdlgStudentPic.FileName);
StudentPic=新位图(this.fdlgStudentPic.FileName);
StuInfo.StudentPic=imageBytes;
}
其他的
{
StudentPic=Properties.Resources.NoPhotoAvailable;
}
}
捕获(例外)
{
Show(“那不是一张图片。”,“浏览图片”);
StudentPic=this.BrowseForPicture();
}
返回学生图片;
}
#端区

是的,现在可以了,但奇怪的是,我几乎发誓我也尝试过这种方法。 好的,没关系,只是它能工作。 让我烦恼的是另一件事,在我看来,这和您的代码是一样的,但它不起作用,它再次尝试处理应用程序(有相同的例外)。 这是一个示例代码:

using(Image img = Image.FromFile(open.FileName))
{
   part.Picture = img; 
}
pictureBox1.InitialImage = null;
pictureBox1.Image = part.Picture;  //Picture  is a propery in a class
现在,我将一个实际图像传递到一个通用列表中,并尝试从中将新图像分配给pictureBox,但正如我所说的,异常被抛出(应用程序被终止)。
为什么?

正如里德所指出的,退出using()语句后,将处理从open.Filename中提取的图像。您的picturebox仍在内存中引用此图像,因此当它被释放时,您也会丢失存储在picturebox中的内容

你真正需要的是你正在拉的图像的唯一副本

    using (Image sourceImg = Image.FromFile(open.Filename))
    {
        Image clonedImg = new Bitmap(sourceImg.Width, sourceImg.Height, PixelFormat.Format32bppArgb);
        using (var copy = Graphics.FromImage(clonedImg))
        {
            copy.DrawImage(sourceImg, 0, 0);
        }
        pictureBox1.InitialImage = null;
        pictureBox1.Image = clonedImg;
    }

这样,一旦退出此块,您的文件将被解锁,并且您将在picturebox中保留图像的唯一副本。

请发布包含要传递的图像代码的完整方法。。这是一个非常简单的修复方法,但我希望先查看代码,这样我就不会为您提供错误的解决方案;GC.
be
pictureBox1.Image=img?什么是
部分
?在哪一行抛出异常?是否存在异常的堆栈跟踪?在更改图片框之前,检查图片框中是否已有图像会更好。如果图像确实存在,那么如何处理它?@user1556110这将很好-如果它没有在
part.Picture中使用(无论是什么)。。。我不想这样做,因为它也存储在其他地方…:)你就是那个人,但我还有一个问题,请阅读我的下一篇文章。@user1556110:pictureBox中已经有一个图像。我确实“处理”了它,将Image属性设置为null。这样可以吗?@MitjaBonca最好存储对它的引用,分配新的引用,然后自己调用Dispose()(如果您知道它没有在其他地方使用)当您使用
Using
块时,实例会在代码退出该块后立即被释放,它是否在其他地方引用了部分图像,但是部分图像已经有了一个实际的图像,不是吗?所以它应该会起作用。还是我错了?代码“part.Picture=img”只复制图像引用,不克隆图像。因此,当img在using{}块末尾被释放时,对图像的引用不再有效,part.Picture将包含一个空引用!我不知道我在想什么,参考资料只是指实际数据的副本。因此,一旦处置完毕,一切都失去了!又是Thx一号。很棒的解决方案。工作得很有魅力+1.