0

In this code i'm getting the last image file i have 5723 image:

i = last_image_file();
Next_File = sf + @"\radar" + i.ToString("D6") + ".png";
pictureBox1.Load(Next_File);

In this case the last file is: radar005723.png Now the problem is that the file was donloaded not correctly either by internet connection problem but in this case i think the problem that my pc stuck frozen and i restarted the pc over so the download wasnt completed or the file got damaged.

How can i add to my code something that will check the image file before this one and if it is good file to show it in the pictureBox1 ? And the file before the last one is damaged too so check the one before and so untill he find the most last working/good file.

And then to delete all the damaged images.

EDIT**

I have changed the code to this:

I have already a method in my project that test for bad files so i did:

i = last_image_file();
Next_File = sf + @"\radar" + i.ToString("D6") + ".png";
bad_file = Bad_File_Testing(Next_File);

if (bad_file == true)
{
    File.Delete(Next_File);
}
else
{
    pictureBox1.Load(Next_File);
}    

But bad_file is not true so its keep going to the pictureBox1.Load(Next_File); and keep throwing the exception: Parameter is not valid I also tried to load the file with the program Paint and it couldnt load the file so i know the file is bad but why the method: Bad_File_testing didn't return true ?

This is the Bad_File_Testing method:

private bool Bad_File_Testing(string file_test)
{    
    try
    {
        Image radar = Bitmap.FromFile(bad_file_test_dir + testing_file);
        radar.Dispose();
        return true;     
    }
    catch
    {
        Logger.Write("Last radar image was damaged and have been deleted");
        return false; 
    }
}

The exception:

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
       at System.Windows.Forms.PictureBox.Load()
       at System.Windows.Forms.PictureBox.Load(String url)
       at mws.Form1.show_latest_downloaded_image() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 1862
       at mws.Form1..ctor() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 467
       at mws.Program.Main() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Program.cs:line 25
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

This is how im using Georgi method:

i = last_image_file();
                    Next_File = sf + @"\radar" + i.ToString("D6") + ".png";
                    bool bad_file = IsValidGDIPlusImage(Next_File);

                    if (bad_file == true)
                    {
                        File.Delete(Next_File);
                    }
                    else
                    {
                        pictureBox1.Load(Next_File);
                    }
user3200169
  • 275
  • 4
  • 17
  • All the files are 92.1 or 92.6 or 92.5kb and only the last file is 91.9kb and on my hard disk i don't see the image inside like preview but i see it all on the others and i can't load/edit the last image on any software. So why the methods my method and Georgi first method not return true ? – user3200169 Feb 25 '14 at 06:57

3 Answers3

1

I suppose that you are making wrong during testing for bad file logic in the Bad_File_Testing method. This method should return TRUE when it cannot open the file successfully, thus goes in the catch clause and should return FALSE when it can open the file successfully. So the corrected code is below:

private bool Bad_File_Testing(string file_test)
{    
    try
    {
        Image radar = Bitmap.FromFile(file_test); // argument value used
        radar.Dispose();
        return true;     
    }
    catch
    {
        Logger.Write("Last radar image was damaged and have been deleted");
        return false; 
    }
}

EDIT

Please have a look at the following code:

bool hasValidImage = false;
while(true)
{
    i = last_image_file();
    Next_File = sf + @"\radar" + i.ToString("D6") + ".png";
    bad_file = Bad_File_Testing(Next_File);

    if (bad_file == true)
    {
        File.Delete(Next_File);
    }
    else
    {
        hasValidImage = true;
        break;
    }  
}

if(hasValidImage)
{
    pictureBox1.Load(Next_File);
}

It would work as: if it's false and after deleting the file it will check the last file before the one deleted and if this one is good load it to the pictureBox1 if it's not good too delete it too and move to the next last one and so on untill it find the last good image and then load it to the pictureBox1.

Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20
  • Wasif that is working. Now how do i make in my code so if it's false and after deleting the file it will check the last file before the one deleted and if this one is good load it to the pictureBox1 if it's not good too delete it too and move to the next last one and so on untill it find the last good image and then load it to the pictureBox1 ? – user3200169 Feb 25 '14 at 07:11
1

I think you are looking for the following: If the last file is bad keep looking until you get the correct file.

while (true)
{
    int i = last_image_file(); // Get the last file in the folder if there are no files return -1
    if (i == -1) 
    {
        break; // Exit the loop
    }
    string Next_File = sf + @"\radar" + i.ToString("D6") + ".png";

    if (IsValidGDIPlusImage(Next_File))
    {
        pictureBox1.Load(Next_File);
        break; // Exit the loop
    }    
    else
    {
        File.Delete(Next_File);
    }
}
sh_kamalh
  • 3,853
  • 4
  • 38
  • 50
0

You can try to create an Image from it, if it throws exception, then it is corrupt.

public bool IsValidGDIPlusImage(string filename)
{
    try
    {
        using (var bmp = new Bitmap(filename))
        {
        }
        return true;
    }
    catch(Exception ex)
    {
        return false;
    }
}

and this is for byte[]

public bool IsValidGDIPlusImage(byte[] imageData)
{
    try
    {
        using (var ms = new MemoryStream(imageData))
        {
            using (var bmp = new Bitmap(ms))
            {
            }
        }
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

Both answers are from Read image and determine if its corrupt C#.

Community
  • 1
  • 1
Georgi-it
  • 3,676
  • 1
  • 20
  • 23
  • Georgi_it i tried your first method and also it didn't return true. Maybe there is something else with the file ? I can't load/edit the file in Paint but maybe the problem is something else ? Im adding the complete exception message im getting when trying to load the image to the pictureBox1. – user3200169 Feb 25 '14 at 06:54