Skip to content

Instantly share code, notes, and snippets.

@gtechsltn
Forked from ChuckSavage/AppendImageExtension.cs
Created August 24, 2021 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtechsltn/9caeb4ba96750a51748cecc6a7b365ba to your computer and use it in GitHub Desktop.
Save gtechsltn/9caeb4ba96750a51748cecc6a7b365ba to your computer and use it in GitHub Desktop.
C# Is file an image and get its type
// Includes a mini-program for checking and fixing files that have no extension
// Only checks for the most common types
// If you create a better version, please upload it here.
using System;
using System.Collections.Generic;
using System.IO;
namespace AppendJPG
{
class Program
{
static void Main(string[] args)
{
foreach (string file in Directory.EnumerateFiles(Environment.CurrentDirectory))
{
//var x = Path.GetExtension(file);
if (Path.GetExtension(file).Length == 0)
{
//var n = Path.GetFileNameWithoutExtension(file);
IsImageExtension.ImageType type;
if (file.IsImage(out type))
{
var ext = type.ToString().ToLower();
//Console.WriteLine(ext);
File.Move(file, file + "." + ext);
}
//var t = type;
}
}
}
}
public static class IsImageExtension
{
static List<string> jpg;
static List<string> bmp;
static List<string> gif;
static List<string> png;
public enum ImageType
{
JPG,
BMP,
GIF,
PNG,
NONE
}
const string JPG = "FF";
const string BMP = "42";
const string GIF = "47";
const string PNG = "89";
static IsImageExtension()
{
jpg = new List<string> { "FF", "D8" };
bmp = new List<string> { "42", "4D" };
gif = new List<string> { "47", "49", "46" };
png = new List<string> { "89", "50", "4E", "47", "0D", "0A", "1A", "0A" };
}
public static bool IsImage(this string file, out ImageType type)
{
type = ImageType.NONE;
if (string.IsNullOrWhiteSpace(file)) return false;
if (!File.Exists(file)) return false;
using (var stream = File.OpenRead(file))
return stream.IsImage(out type);
}
public static bool IsImage(this Stream stream, out ImageType type)
{
type = ImageType.NONE;
stream.Seek(0, SeekOrigin.Begin);
string bit = stream.ReadByte().ToString("X2");
switch (bit)
{
case JPG:
if (stream.IsImage(jpg))
{
type = ImageType.JPG;
return true;
}
break;
case BMP:
if (stream.IsImage(bmp))
{
type = ImageType.BMP;
return true;
}
break;
case GIF:
if (stream.IsImage(gif))
{
type = ImageType.GIF;
return true;
}
break;
case PNG:
if (stream.IsImage(png))
{
type = ImageType.PNG;
return true;
}
break;
default:
break;
}
return false;
}
public static bool IsImage(this Stream stream, List<string> comparer)
{
stream.Seek(0, SeekOrigin.Begin);
foreach (string c in comparer)
{
string bit = stream.ReadByte().ToString("X2");
if (0 != string.Compare(bit, c))
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment