Technologie de conversion de format d'image commune Delphi (2)
Auteur:lyboy99
Courriel : [email protected]
URL : http://hnh.126.com
Vous fournir plusieurs méthodes de conversion de format d'image couramment utilisées et leurs fonctions de conversion
J'espère que ça t'aide
1. Convertir TxT en GIF
2. Convertir le format WMF au format BMP
3. Convertir le format BMP au format WMF
4.TBitmaps vers les régions Windows
-------------------------------------------------- --------------------------
TxT en GIF
-------------------------------------------------- ------
PROcédure TxtToGif (txt, FileName : String);
var
temp : TBitmap ;
GIF : TGIFImage ;
commencer
temp:=TBitmap.Create;
essayer
temp.Hauteur :=400 ;
temp.Width :=60;
temp.Transparent:=True;
temp.Canvas.Brush.Color:=colFondo.ColorValue;
temp.Canvas.Font.Name:=Fuente.FontName;
temp.Canvas.Font.Color:=colFuente.ColorValue;
temp.Canvas.TextOut (10,10,txt);
Imagen.Picture.Assign(nil);
GIF := TGIFImage.Create;
essayer
GIF.Assign(Temp);
//Enregistrer le GIF
GIF.SaveToFile(FileName);
Imagen.Picture.Assign (GIF);
enfin
GIF.Gratuit ;
fin;
Enfin
temp.Détruire ;
Fin;
fin;
-------------------------------------------------- -------------------
2. Convertir le format WMF au format BMP
-------------------------------------------------- -------------------
procédure WmfToBmp(FicheroWmf,FicheroBmp:string);
var
Métafichier :TMétafichier ;
Bmp:TBitmap;
commencer
Métafichier :=TMetaFile.create ;
{Créer un bitmap temporel}
Bmp:=TBitmap.create;
{Charger le métafichier}
MetaFile.LoadFromFile(FicheroWmf);
{Dessinez le métafichier dans le canevas de Bitmap}
avec Bmp faire
commencer
Height:=Métafichier.Height;
Largeur :=Métafichier.Largeur ;
Canvas.Draw(0,0,MetaFile);
{Enregistrez le BMP}
SaveToFile(FicheroBmp);
{BMP gratuit}
Gratuit;
fin;
{Métafichier gratuit}
MetaFile.Free;
fin;
-------------------------------------------------- -------------------
3. Convertir le format BMP au format WMF
-------------------------------------------------- -------------------
procédure BmpToWmf (BmpFile,WmfFile:string);
var
MétaFichier : TMetaFile ;
MFCanvas : TMetaFileCanvas ;
BMP : TBitmap ;
commencer
{Créer des intérimaires}
MetaFile := TMetaFile.Create;
BMP := TBitmap.create;
BMP.LoadFromFile(BmpFile);
{Igualemos tama?os}
{Égalisation des tailles}
MetaFile.Height := BMP.Height;
MetaFile.Width := BMP.Width;
{Créer un canevas pour le métafichier}
MFCanvas:=TMetafileCanvas.Create(MetaFile, 0);
avec MFCanvas faire
commencer
{Dessinez le BMP dans le canevas}
Dessiner(0, 0, BMP);
{Libérez la toile}
Gratuit;
fin;
{Libérez le BMP}
BMP.Gratuit ;
avec MetaFile faire
commencer
{Enregistrez le métafichier}
EnregistrerDansFichier(WmfFichier);
{Libérez-le...}
Gratuit;
fin;
fin;
-------------------------------------------------- -------------------
4.TBitmaps vers les régions Windows
-------------------------------------------------- -------------------
function BitmapToRegion(bmp: TBitmap; TransparentColor: TColor=clBlack;
RedTol : Octet=1 ; GreenTol : Octet=1 ; BlueTol : Octet=1) : HRGN ;
const
Unité d'allocation = 100 ;
taper
PRectArray = ^TRectArray;
TRectArray = Array[0..(MaxInt div SizeOf(TRect))-1] de TRect ;
var
pr : PRectArray ;
h:HRGN;
Données Rgn : PRgnData ;
lr, lg, lb, hr, hg, hb : octet ;
x,y,x0 : entier ;
b : PByteArray ;
ScanLinePtr : pointeur ;
ScanLineInc : entier ;
maxRects : cardinal ;
commencer
Résultat := 0;
{ Gardez à portée de main les valeurs les plus basses et les plus élevées pour les pixels "transparents" }
lr := GetRValue(TransparentColor);
lg := GetGValue(TransparentColor);
lb := GetBValue(TransparentColor);
h := Min($ff, lr + RedTol);
hg := Min($ff, lg + GreenTol);
hb := Min($ff, lb + BlueTol);
bmp.PixelFormat := pf32bit;
maxRects := AllocUnit;
GetMem(RgnData,SizeOf(RGNDATAHEADER) + (SizeOf(TRect) * maxRects));
essayer
avec RgnData^.rdh faire
commencer
dwSize := SizeOf(RGNDATAHEADER);
iType := RDH_RECTANGLES;
nCompte := 0 ;
nRgnTaille := 0;
SetRect(rcBound, MAXLONG, MAXLONG, 0, 0);
fin;
ScanLinePtr := bmp.ScanLine[0];
ScanLineInc := Integer(bmp.ScanLine[1]) - Integer(ScanLinePtr);
pour y := 0 à bmp.Hauteur - 1 faire
commencer
x := 0 ;
while x < bmp.Width faire
commencer
x0 := x;
while x < bmp.Width faire
commencer
b := @PByteArray(ScanLinePtr)[x*SizeOf(TRGBQuad)];
// BGR-RGB : les BMP Windows 32bpp sont constitués de quads BGRa (et non de RGBa)
si (b[2] >= lr) et (b[2] <= hr) et
(b[1] >= lg) et (b[1] <= hg) et
(b[0] >= lb) et (b[0] <= hb) alors
Pause ; // le pixel est transparent
Inc(x);
fin;
{ testez pour voir si nous avons une zone non transparente dans l'image }
si x > x0 alors
commencer
{ augmenter RgnData de AllocUnit rects si nous dépassons maxRects }
si RgnData^.rdh.nCount >= maxRects alors
commencer
Inc(maxRects,AllocUnit);
ReallocMem(RgnData,SizeOf(RGNDATAHEADER) + (SizeOf(TRect) * MaxRects));
fin;
{Ajoutez le rect (x0, y)-(x, y+1) comme nouvelle zone visible dans la région }
pr := @RgnData^.Buffer; // Buffer est un tableau de rects
avec RgnData^.rdh faire
commencer
SetRect(pr[nCount], x0, y, x, y+1);
{ ajuster le rectangle délimité de la région si nous sommes "hors limites" }
si x0 < rcBound.Left alors rcBound.Left := x0;
si y < rcBound.Top alors rcBound.Top := y;
si x > rcBound.Right alors rcBound.Right := x;
si y+1 > rcBound.Bottom alors rcBound.Bottom := y+1;
Inc(nCompte);
fin;
fin ; // si x > x0
si RgnData^.rdh.nCount = 2000 alors
commencer
h := ExtCreateRegion(nil, SizeOf(RGNDATAHEADER) + (SizeOf(TRect) * maxRects), RgnData^);
si Résultat > 0 alors
start // Développe la région actuelle
CombineRgn(Résultat, Résultat, h, RGN_OR);
SupprimerObjet(h);
fin
else // Première région, assignez-la au résultat
Résultat := h;
RgnData^.rdh.nCount := 0;
SetRect(RgnData^.rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
fin;
Inc(x);
end; // analyse chaque octet d'échantillon de l'image
Inc(Entier(ScanLinePtr), ScanLineInc);
fin;
{ besoin d'appeler ExCreateRegion une fois de plus car nous aurions pu partir }
{ un RgnData avec moins de 2000 rects, donc il n'a pas encore été créé/combiné }
h := ExtCreateRegion(nil, SizeOf(RGNDATAHEADER) + (SizeOf(TRect) * MaxRects), RgnData^);
si Résultat > 0 alors
commencer
CombineRgn(Résultat, Résultat, h, RGN_OR);
SupprimerObjet(h);
fin
autre
Résultat := h;
enfin
FreeMem(RgnData,SizeOf(RGNDATAHEADER) + (SizeOf(TRect) * MaxRects));
fin;
-------------------------------------------------- --------------------------------