1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, axctrls, JPEG;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
private
{ Déclarations privées }
Coef: Double; // Mémoriser le coefficient de réduction ou d'agrandissement
InitialCaption: String; // Mémoriser le caption de la fenêtre et éviter un nom en dur dans le code
bmp, bmpmemoire: TBitmap;
col, colprec: TColor;
procedure AdjustPaintBox;
procedure fond(bitmap: TBitmap; color: TColor);
function TripletoColor(RGBTriple: pRGBTriple): TColor;
function BMP24fromFile(const nomFichierImg: string): TBitMap;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
const
max = 37;
Europe: array[0..max] of string = ('Russie', 'Biélorussie', 'Ukraine',
'Moldavie', 'Roumanie', 'Bulgarie', 'Macédoine', 'Grèce', 'Finlande',
'Estonie', 'Lettonie', 'Lituanie', 'Pologne', 'Slovaquie', 'Hongrie',
'Croatie', 'Bosnie Herzegovine', 'Serbie', 'Monténégro', 'Albanie', 'Suède',
'Norvège', 'Danemark', 'Allemagne', 'République Tchèque', 'Autriche',
'Slovénie', 'Suisse', 'Italie', 'Islande', 'Irlande', 'Royaume-Uni',
'Pays-Bas', 'Belgique', 'Luxembourg', 'France', 'Espagne', 'Portugal');
tab: array[0..max] of TColor = ($98316F, $983170, $98306F, $97316F, $98316E,
$98326F, $99316F, $97306E, $993271, $98316D, $982F6F, $96316F, $962F6E,
$982F70, $982F71, $982E6F, $982E70, $982E71, $9A2E70, $9A306E, $9A316E,
$9A2F6E, $9A2E6E, $982E6D, $982F6D, $98306D, $992F6D, $99306D, $99316D,
$9A2F6D, $9A306D, $9A316D, $992E6D, $9A2E6D, $99326F, $98326E, $983270,
$98326D);
implementation
{$R *.dfm}
uses
Math; // pour la fonction Min
function TForm1.BMP24fromFile(const nomFichierImg: string): TBitMap;
// ajouter axCtrls et Jpeg dans le uses pour TOleGraphic, pas besoin de GIFImage
const
FormatsSupportes = '.BMP.DIB.GIF.ICO.JIF.JPG.WMF.EMF';
var
ext: string;
//
function LoadBMP: TBitmap;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf24Bit;
Result.LoadFromFile(nomFichierImg);
end;
//
function LoadJPEG: TBitmap;
var
JPEGImage: TJPEGImage;
begin
Result := TBitmap.Create;
try
JPEGImage := TJPEGImage.Create;
Result.PixelFormat := pf24Bit;
try // test de chargement
JPEGImage.LoadFromFile(nomFichierImg);
JPEGImage.DIBNeeded;
Result.Assign(JPEGImage);
finally
JPEGImage.Free;
end;
except
Result.Free;
Result := nil;
end;
end;
//
function LoadOleGraphic: TBitmap;
var
OleGraphic: TOleGraphic;
FS: TFileStream;
Img: TImage;
begin
OleGraphic := TOleGraphic.Create;
FS := TFileStream.Create(nomFichierImg, fmOpenRead or fmSharedenyNone);
Img := TImage.Create(nil);
try
OleGraphic.LoadFromStream(FS);
Img.Picture.Assign(OleGraphic);
Result := TBitmap.create;
with Result do
begin
PixelFormat := pf24Bit;
Width := Img.Picture.Width;
Height := Img.Picture.Height;
Canvas.Draw(0, 0, Img.Picture.Graphic);
end;
finally
FS.Free;
Img.free;
OleGraphic.Free;
end;
end;
//
begin
Result := nil;
if not FileExists(nomFichierImg) then
begin
ShowMessage(nomFichierImg + ' : n''existe pas');
end
else
begin
ext := UpperCase(ExtractFileExt(nomFichierImg));
if (ext = '') or (Pos(ext, FormatsSupportes) = 0) then
begin
ShowMessage(ext + ' = Format non supporté par BMP24fromFile');
end
else if ext = '.BMP' then
Result := LoadBMP
else if ext = '.JPG' then
Result := LoadJPEG
else
Result := LoadOleGraphic;
end;
end; // BMP24fromFile
procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
bmpmemoire := BMP24fromFile('C:\Users\valentin\Desktop\géoval\europe.EMF');
//
AdjustPaintBox;
//
bmp := TBitmap.Create;
bmp.Assign(bmpmemoire);
colprec := clwhite;
//
InitialCaption := Caption;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
Paintbox1.Canvas.StretchDraw(Rect(0, 0, PaintBox1.Width, PaintBox1.Height), bmp);
end;
procedure TForm1.fond(bitmap: TBitmap; color: TColor);
var
y: integer;
p: pRGBTriple;
begin
bitmap.PixelFormat := pf24Bit;
p := bitmap.ScanLine[bitmap.Height - 1];
for y := 0 to bitmap.Width * bitmap.Height - 1 do
begin
if tripletocolor(p) = col then
begin
p^.rgbtBlue := getBvalue(color);
p^.rgbtGreen := getGvalue(color);
p^.rgbtRed := getRvalue(color);
end;
inc(p);
end;
end;
procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
i: Integer;
begin
// Transposer les coordonnées
X := Round(X / Coef);
Y := Round(Y / Coef);
// Faire la recherche
col := GetPixel(bmpmemoire.Canvas.Handle, X, Y);
if col = colPrec then
Exit;
bmp.Assign(bmpmemoire);
Paintbox1.Invalidate;
for i := 0 to max do
begin
if (col = tab[i]) then
begin
Caption := europe[i];
fond(bmp, clblue);
break;
end
else
begin
if col <> clblue then
bmp.Assign(bmpmemoire);
Caption := InitialCaption; // 'Form1';
end;
end;
Paintbox1.Invalidate;
colPrec := col;
end;
function TForm1.TripletoColor(RGBTriple: PRGBTriple): TColor;
begin
result := RGB(RGBTriple.rgbtRed, RGBTriple.rgbtGreen, RGBTriple.rgbtBlue);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
bmp.Free;
bmpmemoire.Free;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
AdjustPaintBox;
end;
procedure TForm1.AdjustPaintBox;
begin
// Coefficient de réduction ou d'agrandissement à appliquer à la paintbox en
// fonction du bitmap image en mémoire
Coef := Min(ClientWidth / bmpmemoire.Width, ClientHeight / bmpmemoire.Height);
// Redimensionnement du PaintBox
PaintBox1.Width := Round(bmpmemoire.Width * Coef);
PaintBox1.Height := Round(bmpmemoire.Height * Coef);
PaintBox1.Left := (ClientWidth - PaintBox1.Width) div 2;
PaintBox1.Top := (ClientHeight - PaintBox1.Height) div 2;
end;
end. |
Partager