Ver Mensaje Individual
  #4  
Antiguo 10-10-2016
bucanero bucanero is offline
Miembro
 
Registrado: nov 2013
Ubicación: Almería, España
Posts: 208
Reputación: 11
bucanero Va camino a la fama
Hola a todos,

yo utilizo las siguiente función para determinar el tipo imagen para las extensiones mas conocidas,

Código Delphi [-]
interface
...
   function  GetImageFormat(const AFileName: String; OmitirExtension:Boolean=true): String; overload;
   function  GetImageFormat(Stream: TStream; var fileType:String):boolean; overload;
...
implementation
...
function GetImageFormat(const AFileName: String; OmitirExtension:Boolean=true): String;
///  Si el parametro omitirExtension esta a true, solamente devuelve una extensión
///  cuando las cabezeras del fichero son realmente reconocidas
var
  ext: string;
  FileStream: TFileStream;
begin
  ext := lowerCase(extractFileExt(AFileName));
  try
    FileStream := TFileStream.Create(AFileName, fmOpenRead);
    try
      if not GetImageFormat(FileStream, result) and
       not omitirExtension then
           // si no se reconoce el fichero por su primeros caractes, se devuelve la propia extensión del fichero
           Result:=ext;
    finally
      FileStream.free;
    end;
  except
  end;
end;

function GetImageFormat(Stream: TStream; var fileType:string):Boolean;
var
  FirstBytes: AnsiString;
begin
  result:=False;
  fileType := '';
  if (Stream <> nil) and (Stream.Size > 40) then
  try
    SetLength(FirstBytes, 8);
    Stream.Read(FirstBytes[1], 8);
    if (Copy(FirstBytes, 1, 2) = 'BM') then
      fileType := '.bmp'
    else if (FirstBytes = #137'PNG'#13#10#26#10) then
      fileType := '.png'
    else if (Copy(FirstBytes, 1, 3) = 'GIF') then
      fileType := '.gif'
    else if  (Copy(FirstBytes, 1, 2) = #$FF#$D8) then
      fileType := '.jpg'
    else if (Copy(FirstBytes, 1, 3) = #$49#$49#$2A) then
      fileType := '.tif'
    else if (Copy(FirstBytes, 1, 3) = #$0#$0#$01) then
      fileType := '.ico';
  finally
    result:=(fileType<>'');
  end;
end;
...
end.

Para el resto de extensiones simplemente hay que buscar las cabezeras que utilizan y añadirlas a la lista

Un saludo
Responder Con Cita