2013-01-22 76 views
1

我在我的VB项目中有一个功能,我扫描一个图像,然后我可以改变对比度。 我扫描并保存C:\temp\my_img.tif打开并保存问题

winform中,图像显示在PictureBox中。

如果我在img.Save("C:\temp\my_img.tif", ImageFormat.Tiff)的对比度函数集中得到了“GDI +中发生了一般性错误。”。但是,如果我将filename设置为其他内容,则工作得很好。

那么,如何在保存之前释放使用过的图像呢?

的整体功能,简而言之:

Sub setContrast(ByVal C As Single) 
    'filename(1) ia a "global" variable that stores the used file path, in this case "C:\temp\my_img.tif" 

    Dim img As Image = Image.FromFile(filename(1)) '<--- I get the image 

    'A bunch of contrast stuff in some rows..... 

    'Here, i should release the image... 

    img.Save(filename(1), ImageFormat.Tiff) '<---Tries to save 

    PictureBox1.Refresh() 
End Sub 
+0

这实际上并没有回答你的问题,但'Image'实现了'IDisposable',所以你应该在完成之后处置它,或者更好的是,将它放在'Using'块中。 –

回答

1
使用不同的文件名

保存它,然后,如果必要的话,删除旧文件并重新命名新文件,以配合老,具有此Disposed预先设定为Image

Image.FromFile

文件保持锁定,直到Image设置。

有没有措辞别处,说,如果同一个Image实例试图Save回文件,这是莫名其妙合作周围。