Pciture Box错误图像意外出现并且不会消失 [英] Pciture Box Error image appears unexpectedly and does not go away

查看:68
本文介绍了Pciture Box错误图像意外出现并且不会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于显示图片的表单(一次8个),并决定保留图片,删除图片或重命名图片。一切都按计划工作,直到我得到一个弹出消息框(MsgBox),当我试图重命名图像
,然后我得到我重命名的图片框中的错误图像,它将在我关闭表格并重新打开之前不要离开。 我重命名图像的方法是首先使用PictureBox00.Image.Dispose()处理图像以释放文件,以便我
可以重命名它,然后重命名后我使用PictureBox00.Image = Image重新加载图片框。 FromFile(imgFldr0Path& imgNames(viewFldr0Pntr,0))


如果我在这两个语句之间的任何时间获得一个MsgBox弹出窗口,我将在该图片框中获得Picture Error图像。 我发现了这一点,因为在我的原始代码中,我使用Try-Catch来捕获任何IOExceptions,例如重复的文件名。 
当我确实得到副本时,我会弹出一个MsgBox来告诉我有关错误的信息,以便我可以纠正它。 但正如我所说,我得到的错误图像不会消失。 我重写了我的代码,以明确检查各种错误,但没有解决
问题。 然后我再次重写我的代码以在处理图像之前检查重复项并且不起作用,因为我还在几个MsgBoxes中进行了调试(因为我认为它与IOException有关)。 所以现在用3种不同的方法重写我的代码
后,我很难过。 我不知道如何让它发挥作用。


有谁知道这是什么问题? 以下是代码的部分列表:


这是其中一个图片框的重命名点击事件处理程序

 Private Sub lblImgName00_Click(sender as Object,e As EventArgs)处理lblImgName00.Click'用于重命名图像
'调用getNewName方法传回新的名称是空白然后我们不更改名称
'使用新名称并调用名称更改方法
newFileName =""
GetNewName(lblImgName00.Text,imgFldrs(viewFldr0Pntr,0),imgFldr0Path& imgNames(viewFldr0Pntr,0),viewFldr0Pntr,0)
如果newFileName<> ""然后'我们需要更改文件夹和数组中的文件名
如果不是PictureBox00.Image是Nothing然后PictureBox00.Image.Dispose()
RenameImageFile(newFileName,imgFldrs(viewFldr0Pntr,0), imgFldr0Path& imgNames(viewFldr0Pntr,0),viewFldr0Pntr,0)
PictureBox00.Image = Image.FromFile(imgFldr0Path& imgNames(viewFldr0Pntr,0))
lblImgName00.Text = imgNames(viewFldr0Pntr,0)
结束如果
结束子

这是带有一些明确检查的getNewName方法:

 Private Sub GetNewName(ByVal curName As String,imgFldrPath As String,imgFilePath As String,imgFldrPntr As Integer,imgFilePntr As Integer)
'尝试这个以查看我是否可以修复图片框上出现错误图像。处理后出现IT,然后是msgbox
'在这种情况下,只需在此处测试,看看我们是否可以使用新名称并将其返回。如果我们可以在调用程序中调用一个方法来改变它

'TODO:完成062:将其设置为自动放入"!Covera"。作为默认编码在1.5.1.8
'TODO:Done 069:将InputBox的位置设置为项目窗口的中间位置。编码于1.5.1.13
'TODO:081:对于新名称,我们要进行以下更改:
'TD081:如果当前名称具有"封面"在其中然后将名称更改为!Covera if!Covera不存在

'如果文件名是!Covera我们要调用输入框
'如果名称是文件不包含"封面"然后我们想调用输入框
'如果文件名包含"cover",那么并且没有名为"!Covera"的文件。然后将文件重命名为"!Covera"
'如果文件存在则标记错误并将newFileName设置为空白

Dim CoveraExistsFlg As Boolean = False'标志告诉我们文件夹中是否有!Covera
newFileName =""
tstString2 = imgFldrPath& " \ Covera.jpg"!;
如果My.Computer.FileSystem.FileExists(imgFldrPath&" \!Covera.jpg")那么
CoveraExistsFlg = True
结束如果
如果curName.ToLower Like( " * cover *")而不是CoveraExistsFlg然后'这返回'!Covera"对于单击名称更改
newFileName ="!Covera"
Else
'需要弄清楚我们要将名称更改为
newFileName = InputBox(" Original Name is"& imgNames(imgFldrPntr,imgFilePntr)& vbCrLf& amp ;"输入没有延伸的新名称或按取消退出","!Covera",400,350)
如果newFileName ="!Covera"和CoveraExistsFlg然后
MsgBox("File!Covera.jpg alread exists。No Changes Made",MsgBoxStyle.Critical,"Renaming Error")
newFileName =""
结束如果
如果My.Computer.FileSystem.FileExists(imgFldrPath&" \"& newFileName&" .jpg")那么
MsgBox(" File" ;& newFileName&" .jpg alread exists。No Changes Made",MsgBoxStyle.Critical," Renaming Error")
newFileName =""
结束如果
结束如果

结束子


这是RenameImageFile方法:

 Private Sub RenameImageFile(ByVal newName As String,imgFldrPath As String,imgFilePath As String,imgFldrPntr As Integer,imgFilePntr As Integer )
Dim imgFileStats As System.IO.FileInfo
Dim newImageFilePath As String
imgFileStats = My.Computer.FileSystem.GetFileInfo(imgFilePath)
尝试
My.Computer。 FileSystem.RenameFile(imgFilePath,newName& imgFileStats.Extension)
newImageFilePath = imgFldrPath& " /" &安培; newName& imgFileStats.Extension
imgNames(imgFldrPntr,imgFilePntr)= newName& imgFileStats.Extension
Catch ex As System.IO.IOException
MsgBox(ex.Message,MsgBoxStyle.Critical,"重命名RenameImageFile方法中的错误")
Catch ex As Exception
MsgBox(ex.Message,MsgBoxStyle.Critical,"重命名错误不是IOException")
'MsgBox("发生了某种错误",MsgBoxStyle.Exclamation)
结束尝试
结束Sub


有没有人能够了解可能发生的事情?









解决方案


一些提示:


1。处理图片框的图像后,将图像属性设置为Nothing,更好:将图片框的图像转换为新变量,将Image属性设置为空,然后处理新的变量图像。


1a上。我宁愿使用从图片框中的文件创建的图像的新位图,因此在初始化Image变量(使用Image.FromStream)后立即释放磁盘上的文件


Something喜欢:

位图bmpForPicBox = null; 
using(Image img = Image.FromFile(myFileName))
{
bmpForPicBox = new Bitmap(img);
}

this.pictureBox1.Image = bmpForPicBox;


2。使用Path.Combine创建文件路径


3。在将图像分配给picBox之前,请使用File.Exists(yourNewFilePath)验证文件是否存在


问候,


 的Thorsten


I have a form that I use to display images (8 at a time) and decide to keep the image, delete it or rename it. Everything works as planned until I get to a point where I get a popup message box (MsgBox) when I am trying to rename an image and then I get the Error image in the Picture Box that I was renaming and it will not go away until I close the form and reopen it.  The way I rename the image is by first disposing the image using PictureBox00.Image.Dispose() to release the file so I can rename it, then after renaming it I reload the picture box using PictureBox00.Image = Image.FromFile(imgFldr0Path & imgNames(viewFldr0Pntr, 0))

If I get a MsgBox popup at anytime between those two statements I will get the Picture Error image in that Picture Box.  I found this out because in my original code I was using Try - Catch to catch any IOExceptions like duplicate file names.  When I did get a duplicate I would popup a MsgBox to tell me that about the error so I could correct it.  But as I said I get that Error Image that will not go away.  I rewrote my code to explicitly check for various errors but that did not fix the problem.  I then rewrote my code again to check for duplicates before disposing the image and that didn't work as I also tossed in a few MsgBoxes for debug (since I thought it was something to do with an IOException).  So now after rewriting my code in 3 different methods I am stumped.  I have no idea how to get this to work.

Does anyone know what the issue is?  Here is a partial listing of code:

This is the rename click event handler for one of the picture boxes

    Private Sub lblImgName00_Click(sender As Object, e As EventArgs) Handles lblImgName00.Click 'Used to rename the image
        'Call a getNewName method to pass back the new name of it is blank then we don't change the name
        'Use the new name and call a name change Method
        newFileName = ""
        GetNewName(lblImgName00.Text, imgFldrs(viewFldr0Pntr, 0), imgFldr0Path & imgNames(viewFldr0Pntr, 0), viewFldr0Pntr, 0)
        If newFileName <> "" Then 'We need to change the file name in the folder and in the arrays
            If Not PictureBox00.Image Is Nothing Then PictureBox00.Image.Dispose()
            RenameImageFile(newFileName, imgFldrs(viewFldr0Pntr, 0), imgFldr0Path & imgNames(viewFldr0Pntr, 0), viewFldr0Pntr, 0)
            PictureBox00.Image = Image.FromFile(imgFldr0Path & imgNames(viewFldr0Pntr, 0))
            lblImgName00.Text = imgNames(viewFldr0Pntr, 0)
        End If
    End Sub

This is the getNewName method with some explicit checks:

    Private Sub GetNewName(ByVal curName As String, imgFldrPath As String, imgFilePath As String, imgFldrPntr As Integer, imgFilePntr As Integer)
        'Trying this to see if I can fix the issue of having the error image appear on the picture boxes.  IT appeared after dispose and then a msgbox
        'In this case just test here to see if we can use a new name and return it. If we can then in the calling program call a method to change it

        'TODO: Done 062: Set this up to automatically put "!Covera" as the default Coded in 1.5.1.8
        'TODO: Done 069: set the position of the InputBox to the middle of the project window.  Coded in 1.5.1.13
        'TODO: 081: For the New Name we want to make the following changes:
        '  TD081: If the Current name has a "cover" in it then change the name to !Covera if !Covera Does not exist

        'If the name of the file is !Covera we want to call up the inputbox
        'If the name of the file does not contain "cover" then we want to call up the inputbox
        'if the name of the file contains "cover" and there is no file called "!Covera" then rename the file to "!Covera"
        'If the file exists then flag an error and set the newFileName to blank

        Dim CoveraExistsFlg As Boolean = False 'flag to tell us if there is a !Covera in the folder
        newFileName = ""
        tstString2 = imgFldrPath & "\!Covera.jpg"
        If My.Computer.FileSystem.FileExists(imgFldrPath & "\!Covera.jpg") Then
            CoveraExistsFlg = True
        End If
        If curName.ToLower Like ("*cover*") And Not CoveraExistsFlg Then 'This returns "!Covera" for the one click name change
            newFileName = "!Covera"
        Else
            'Need to figure out what we are going to change the name to
            newFileName = InputBox("Original Name is " & imgNames(imgFldrPntr, imgFilePntr) & vbCrLf & "Input the new name without an extention or press cancel to exit", , "!Covera", 400, 350)
            If newFileName = "!Covera" And CoveraExistsFlg Then
                MsgBox("File !Covera.jpg alread exists.  No Changes Made", MsgBoxStyle.Critical, "Renaming Error")
                newFileName = ""
            End If
            If My.Computer.FileSystem.FileExists(imgFldrPath & "\" & newFileName & ".jpg") Then
                MsgBox("File " & newFileName & ".jpg alread exists.  No Changes Made", MsgBoxStyle.Critical, "Renaming Error")
                newFileName = ""
            End If
        End If

    End Sub

Here is the RenameImageFile method:

    Private Sub RenameImageFile(ByVal newName As String, imgFldrPath As String, imgFilePath As String, imgFldrPntr As Integer, imgFilePntr As Integer)
        Dim imgFileStats As System.IO.FileInfo
        Dim newImageFilePath As String
        imgFileStats = My.Computer.FileSystem.GetFileInfo(imgFilePath)
        Try
            My.Computer.FileSystem.RenameFile(imgFilePath, newName & imgFileStats.Extension)
            newImageFilePath = imgFldrPath & "/" & newName & imgFileStats.Extension
            imgNames(imgFldrPntr, imgFilePntr) = newName & imgFileStats.Extension
        Catch ex As System.IO.IOException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Renaming Error in RenameImageFile Method")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Renaming Error not an IOException")
            'MsgBox("Some kind of error happened", MsgBoxStyle.Exclamation)
        End Try
    End Sub

Does anyone have an insight to what may be happening?

解决方案

Hi,

some hints:

1. After Disposing the picturebox's Image, set the Image property to Nothing, better: get the image of the picturebox into a new variable, set the Image property to nothing and then dispose the new variable-image.

1a. I'd rather use a new Bitmap of the Image created from file in the picturebox, so the file on disk is freed immediately after initializing the Image variable (of use Image.FromStream)

Something like:

            Bitmap bmpForPicBox = null;
            using (Image img = Image.FromFile(myFileName))
            {
                bmpForPicBox = new Bitmap(img);
            }

            this.pictureBox1.Image = bmpForPicBox;

2. Use Path.Combine to create file-paths

3. Before assigning the Image to the picBox, verify the file exists by using File.Exists(yourNewFilePath)

Regards,

  Thorsten


这篇关于Pciture Box错误图像意外出现并且不会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆