Function LoadImageFromFile(FileName : String) : TBitMap;
var
jp : TJPEGImage; //Requires the "jpeg" unit added to "uses" clause.
bm : TBitmap;
Mf : TMetaFile;
Ic : TIcon;
Gif : TGIFImage;
png : TPngImage;
Ext : String;
Ext1 : String;
St : TMemoryStream;
Begin
If Not FileExists(FileName) then
Raise Exception.create('El archivo "'+FileName+'" no existe');
Result := Nil;
Ext := Trim(AnsiLowerCase(ExtractFileExt(FileName)));
St := TMemoryStream.Create;
St.LoadFromFile(FileName);
St.Position := 0;
Ext1 := AnsiLowerCase(GetImageFormat(St));
If Ext1 <> '' then //Revisa el formato real del archivo, sin importar el nombre del archivo.
Ext := Ext1;
If not ((ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') or
(ext = '.ico') or (ext = '.emf') or (ext = '.wmf') or
(ext = '.gif') or (ext = '.png')) then
Raise Exception.Create('El formato "'+Ext+'" del archivo no es soportado');
St.Position := 0;
Bm := TBitMap.Create;
Try
If (ext = '.jpg') or (ext = '.jpeg') then
Begin
jp := TJPEGImage.Create;
Jp.LoadFromStream(St);
bm.Assign(Jp);
Result := Bm;
FreeAndNil(Jp);
End
Else
If (ext = '.gif') then
Begin
Gif := TGIFImage.Create;
Gif.LoadFromStream(St);
bm.Assign(Gif);
FreeAndNil(Gif);
End
Else
If (ext = '.png') then
Begin
Png := TPngImage.Create;
Png.LoadFromStream(St);
bm.Assign(Png);
Result := Bm;
FreeAndNil(Png);
End
Else
If (ext = '.ico') then
Begin
Ic := TIcon.Create;
Ic.LoadFromStream(St);
Bm.Width := Ic.Width;
Bm.Height := Ic.Height;
Bm.Canvas.Draw(0,0,Ic);
Result := Bm;
FreeAndNil(Ic);
End
Else
If (ext = '.emf') or (ext = '.wmf') then
Begin
Mf := TMetaFile.Create;
Mf.LoadFromStream(St);
Bm.Width := Mf.Width;
Bm.Height := Mf.Height;
Bm.Canvas.Draw(0,0,Mf);
Result := Bm;
FreeAndNil(Mf);
End
Else
If (ext = '.bmp') then
Begin
Bm.LoadFromStream(St);
Result := Bm;
End
Else
Raise exception.create('El formato del archivo "'+FileName+'" no es soportado');
Finally
End;
End;
Function LoadGraphicFromFile(FileName : String) : TGraphic;
var
jp : TJPEGImage; //Requires the "jpeg" unit added to "uses" clause.
bm : TBitmap;
Mf : TMetaFile;
Ic : TIcon;
Gif : TGIFImage;
png : TPngImage;
Ext : String;
Ext1 : String;
St : TMemoryStream;
Begin
If Not FileExists(FileName) then
Raise Exception.create('El archivo "'+FileName+'" no existe');
Result := Nil;
Ext := Trim(AnsiLowerCase(ExtractFileExt(FileName)));
St := TMemoryStream.Create;
St.LoadFromFile(FileName);
St.Position := 0;
Ext1 := AnsiLowerCase(GetImageFormat(St));
If Ext1 <> '' then //Revisa el formato real del archivo, sin importar el nombre del archivo.
Ext := Ext1;
If not ((ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') or
(ext = '.ico') or (ext = '.emf') or (ext = '.wmf') or
(ext = '.gif') or (ext = '.png')) then
Raise Exception.Create('El formato "'+Ext+'" del archivo no es soportado');
St.Position := 0;
Try
If (ext = '.jpg') or (ext = '.jpeg') then
Begin
jp := TJPEGImage.Create;
Jp.LoadFromStream(St);
Result := Jp;
End
Else
If (ext = '.gif') then
Begin
Gif := TGIFImage.Create;
Gif.LoadFromStream(St);
Result := Gif;
End
Else
If (ext = '.png') then
Begin
Png := TPngImage.Create;
Png.LoadFromStream(St);
Result := Png;
End
Else
If (ext = '.ico') then
Begin
Ic := TIcon.Create;
Ic.LoadFromStream(St);
Result := Ic;
End
Else
If (ext = '.emf') or (ext = '.wmf') then
Begin
Mf := TMetaFile.Create;
Mf.LoadFromStream(St);
Result := Mf;
End
Else
If (ext = '.bmp') then
Begin
Bm := TBitMap.Create;
Bm.LoadFromStream(St);
Result := Bm;
End
Else
Raise exception.create('El formato del archivo "'+FileName+'" no es soportado');
Finally
St.Free;
End;
End;
Function LoadGraphicFromStream(St : TStream) : TGraphic;
var
jp : TJPEGImage; //Requires the "jpeg" unit added to "uses" clause.
bm : TBitmap;
Mf : TMetaFile;
Ic : TIcon;
Gif : TGIFImage;
png : TPngImage;
Ext : String;
Begin
If Not Assigned(St) then
Raise Exception.create('El formato de Imágen no es válida');
If St.Size < 40 then
Raise Exception.create('El formato de Imágen no es válida');
Result := Nil;
St.Position := 0;
Ext := AnsiLowerCase(GetImageFormat(St));
If not ((ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') or
(ext = '.ico') or (ext = '.emf') or (ext = '.wmf') or
(ext = '.gif') or (ext = '.png')) then
Raise Exception.Create('El formato "'+Ext+'" del archivo no es soportado');
St.Position := 0;
Try
If (ext = '.jpg') or (ext = '.jpeg') then
Begin
jp := TJPEGImage.Create;
Jp.LoadFromStream(St);
Result := Jp;
End
Else
If (ext = '.gif') then
Begin
Gif := TGIFImage.Create;
Gif.LoadFromStream(St);
Result := Gif;
End
Else
If (ext = '.png') then
Begin
Png := TPngImage.Create;
Png.LoadFromStream(St);
Result := Png;
End
Else
If (ext = '.ico') then
Begin
Ic := TIcon.Create;
Ic.LoadFromStream(St);
Result := Ic;
End
Else
If (ext = '.emf') or (ext = '.wmf') then
Begin
Mf := TMetaFile.Create;
Mf.LoadFromStream(St);
Result := Mf;
End
Else
If (ext = '.bmp') then
Begin
Bm := TBitMap.Create;
Bm.LoadFromStream(St);
Result := Bm;
End
Else
Raise exception.create('El formato del archivo "'+Ext+'" no es soportado');
Finally
End;
End;
Function GraphicToBitmap(Graphic : TGraphic) : TBitMap;
var
jp : TJPEGImage; //Requires the "jpeg" unit added to "uses" clause.
bm : TBitmap;
Mf : TMetaFile;
Ic : TIcon;
Gif : TGIFImage;
png : TPngImage;
Begin
Bm := TBitMap.Create;
Try
If Graphic is TJPEGImage then
Begin
jp := TJPEGImage(Graphic);
bm.Assign(Jp);
//Result := Bm;
End
Else
If Graphic is TGIFImage then
Begin
Gif := TGIFImage(Graphic);
bm.Assign(Gif);
//Result := Bm;
End
Else
If Graphic is TPngImage then
Begin
Png := TPngImage(Graphic);
bm.Assign(Png);
//Result := Bm;
End
Else
If Graphic is TIcon then
Begin
Ic := TIcon(Graphic);
Bm.Width := Ic.Width;
Bm.Height := Ic.Height;
Bm.Canvas.Draw(0,0,Ic);
//Result := Bm;
End
Else
If Graphic is TMetaFile then
Begin
Mf := TMetaFile(Graphic);
Bm.Width := Mf.Width;
Bm.Height := Mf.Height;
Bm.Canvas.Draw(0,0,Mf);
//Result := Bm;
End
Else
If Graphic is TBitMap then
Begin
//Result := Bm;
End
Else
Raise exception.create('El formato del archivo no es soportado');
Finally
End;
Result := Bm;
End;
Function ScaleImage(Image : TBitMap; Alto : Integer) : TBitMap;
var
bm : TBitmap;
X, Y : Integer;
begin
If (Image = Nil) then
Raise Exception.Create('La imagen no es válida');
Y := Alto; // altura máxima
If Y <= 0 then Y := 100;
Bm := TBitMap.Create;
Try
X := Trunc(Image.Width*Y/Image.Height);
If X > (Y*1.8) then
X := Trunc(y*1.8);
Bm.Width := X;
Bm.Height := Y;
Bm.Canvas.StretchDraw(Rect(0,0,X,Y),Image);
Result := Bm;
Finally
End;
End;
Function BitMapToJPG(Bm : TBitMap; CompressRate : Integer) : TJPEGImage;
Var
jp : TJPEGImage; //Requires the "jpeg" unit added to "uses" clause.
Begin
If Bm = Nil then
Raise Exception.Create('La imagen no ha sido asignada');
jp := TJPEGImage.Create;
Try
Jp.CompressionQuality := CompressRate;
Jp.Assign(Bm);
Result := Jp;
Finally
End;
End;
Function CrearMiniaturaImg(FileName : String; Alto : Integer) : TJPegImage;
Begin
Result := BitMapToJPG(ScaleImage(LoadImageFromFile(FileName),Alto), 80);
End;
Function GetImageFormat(Stream : TStream) : String;
Var
FirstBytes: AnsiString;
begin
Result := '';
if (Stream <> nil) and (Stream.Size > 40) then
begin
try
SetLength(FirstBytes, 8);
Stream.Read(FirstBytes[1], 8);
if Copy(FirstBytes, 1, 2) = 'BM' then
Result := '.bmp'
else if FirstBytes = #137'PNG'#13#10#26#10 then
Result := '.png'
else if Copy(FirstBytes, 1, 3) = 'GIF' then
Result := '.gif'
else if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
Result := '.jpg'
else if Copy(FirstBytes, 1, 3) = #$49#$49#$2A then
Result := '.tif'
else if Copy(FirstBytes, 1, 3) = #$0#$0#$01 then
Result := '.ico'
Finally
end;
end;
End;
procedure HideApplication(Application : TApplication);
begin
ShowWindow(Application.Handle, SW_HIDE) ;
SetWindowLong(Application.Handle, GWL_EXSTYLE,
getWindowLong(Application.Handle, GWL_EXSTYLE) or
WS_EX_TOOLWINDOW) ;
ShowWindow(Application.Handle, SW_SHOW) ;
end;
procedure ScreenShot(DestBitmap : TBitmap) ;
var
DC : HDC;
begin
DC := GetDC (GetDesktopWindow) ;
try
DestBitmap.Width := GetDeviceCaps (DC, HORZRES) ;
DestBitmap.Height := GetDeviceCaps (DC, VERTRES) ;
BitBlt(DestBitmap.Canvas.Handle,
0,
0,
DestBitmap.Width,
DestBitmap.Height,
DC,
0,
0,
SRCCOPY) ;
finally
ReleaseDC (GetDesktopWindow, DC) ;
end;
end;