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
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls,axctrls,JPEG;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormDestroy(Sender: TObject);
procedure Edit1Change(Sender: TObject);
private
{ Déclarations privées }
bmp,bmpmemoire : TBitmap;
col,colprec : TColor;
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
Europe : array[0..37] 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..37] 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);
max : integer = 37;
implementation
{$R *.dfm}
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 OleGraphic: TOleGraphic; FS: TFileStream; ext: string;
JPEGImage: TJPEGImage; img: tImage; pl: tPanel;
begin
if not FileExists(nomFichierImg) then begin
showMessage(nomFichierImg + ' : n''existe pas'); Result := nil; EXIT;
end;
ext := UpperCase(ExtractFileExt(nomFichierImg));
if (ext = '') or (pos(ext, FormatsSupportes) = 0) then
begin showMessage(ext + ' = Format non supporté par BMP24fromFile');
Result := nil; EXIT;
end;
if ext = '.BMP' then begin
Result := tBitmap.create;
Result.PixelFormat := pf24Bit;
Result.LoadFromFile(nomFichierImg);
EXIT;
end;
if ext = '.JPG' then begin
try
JPEGImage := TJPEGImage.Create;
Result := TBitmap.Create;
Result.PixelFormat := pf24Bit;
try // test de chargement
JPEGImage.LoadFromFile(nomFichierImg);
JPEGImage.DIBNeeded;
Result.Assign(JPEGImage);
finally
JPEGImage.Free;
end;
except
Result := nil;
end;
EXIT;
end;
OleGraphic := TOleGraphic.Create;
FS := TFileStream.Create(nomFichierImg, fmOpenRead or fmSharedenyNone);
img := tImage.Create(pl); //<- petit inconvénient : nécessite un AOwner
// et on peut ignorer l'[Avertissement] ... La variable 'pl' n'est peut-être pas initialisée
// ça marche quand-même.
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; // BMP24fromFile
procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := true;
bmpmemoire := BMP24fromFile('C:\Users\valentin\Desktop\géoval\europe.EMF');
ClientWidth:=bmpmemoire.Width;
ClientHeight:=bmpmemoire.Height;
PaintBox1.Width := bmpmemoire.Width;
PaintBox1.Height := bmpmemoire.Height;
bmp := TBitmap.Create;
bmp.Assign(bmpmemoire);
colprec:=clwhite;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
Paintbox1.Canvas.Draw(0,0,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
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:='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.Edit1Change(Sender: TObject);
var i : integer;
begin
For i:=0 to max do
begin
if Edit1.Text = europe[i] then
begin
bmp.Assign(bmpMemoire);
col:=tab[i];
fond(bmp,clgreen);
Paintbox1.Invalidate;
end;
end;
end;
end. |
Partager