I use C#, VS 2008, winforms app, for download images JPG, automatized way.
Some images haven't downloaded right. I think many bytes (more 70% bytes of image corrupted) not write well in filesystem (folder). I try view the image JPG "corrupt" (using Preview view in WinXP) and I can view 20%-30% of imagen,but 70%-80% is wrong, and I see gray color (I test that the color is Color [A=255, R=128, G=128, B=128]).
I have a image "corrupted". Issues: Photoshop detect it is corrupted. Anothers applications cannot open it. Paint can open it. Windows Preview can open it to.
Now, I have this code:
public static bool EsImagenJpegCorrectaCompletamente(string filePath)
{
try
{
if (!IsJpeg(filePath)) return false;
using (var test = Image.FromFile(filePath))
{
var b = new Bitmap(test);
var size = b.Size;
var limiteEnEjeX = (int) (size.Width/2);
if (limiteEnEjeX == 0) limiteEnEjeX = size.Width;
// Color [A=255, R=128, G=128, B=128]
var colorGray = Color.FromArgb(255, 128, 128, 128);
var todosLosPixelesEnRangoDeterminadoSonGray = true;
for (var y = size.Height - 1; y > size.Height - 11; y--)
{
for (int x = 0; x < limiteEnEjeX; x++)
{
var colorPixel = b.GetPixel(x, y);
if (!colorPixel.Equals(colorGray))
{
todosLosPixelesEnRangoDeterminadoSonGray = false;
break;
}
Trace.WriteLine("x: " + x + "y: " + y + " " + colorPixel.ToString());
}
}
// La imagen no se ha guardado en disco correctamente
if (todosLosPixelesEnRangoDeterminadoSonGray) return false;
return true;
}
}
catch (OutOfMemoryException ex)
{
// Image.FromFile will throw this if file is invalid.
// Don't ask me why.
return false;
}
}
I need automatized way for detect those images JPG "corrupt", all cases (100%). any sample code, suggestions?