Précédent   Forum du club des développeurs et IT Pro > Environnements de développement > Delphi > Contribuez
Contribuez Proposez vos articles, cours, tutoriels, FAQ, sources et autres ressources pour la rubrique Delphi.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 10/01/2011, 20h10   #1
ouiouioui
Membre Expert
 
Avatar de ouiouioui
 
Homme Alexandre
Administrateur systèmes et réseaux
Inscription : août 2006
Messages : 885
Détails du profil
Informations personnelles :
Nom : Homme Alexandre
Âge : 31
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Administrateur systèmes et réseaux
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : août 2006
Messages : 885
Points : 1 201
Points : 1 201
Envoyer un message via MSN à ouiouioui
Par défaut [Delphi] Drag Drop image conversion

Bonjour, j'ai écris sa pour récupérer une image au format jpg et png dans une autre form lorsque un utilisateur glisse une image au format png, jpg ou bmp sur un un TImage, mais il faut utiliser un TPanel pour gérer le glisser déposer ou alors j'ai zappé un truc.

j'ai ajouté un projet exemple, je compte sur vos conseil éclairé pour me dire ce qui n'est pas top

compatible D2009 +

Code pascal :
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
Unit ImageDropDown;
 
Interface
 
Uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Jpeg, pngimage, ShellAPI, ExtCtrls;
 
Type
  TFormatIMG = Set Of (fiJPG, fiBMP, fiPNG);
 
  TImageDropDown<T: TGraphic, Constructor> = Class
  Private
    originalImageWindowProc          : TWndMethod;
    FImage                           : TImage;
    FPanel                           : TPanel;
    FIMG                             : T;
    FFormatIMG                       : TFormatIMG;
    FFileName                        : String;
    FOnBeforeLoadImg, FOnAfterLoadImg: TNotifyEvent;
    Procedure ImageDrop(Var Msg: TWMDROPFILES);
  Public
    Procedure ImageWindowProc(Var Msg: TMessage);
    Procedure LoadIMG(Const sImgFileName: String);
    Procedure ClearIMG;
    Procedure SelectIMG;
    Constructor Create(AImage: TImage; APanel: TPanel);
    Destructor Destroy; Override;
    Property IMG: T Read FIMG Write FIMG;
    Property FormatIMG: TFormatIMG Read FFormatIMG Write FFormatIMG;
    Property FileName: String Read FFileName;
    Property OnBeforeLoadImg: TNotifyEvent Read FOnBeforeLoadImg Write FOnBeforeLoadImg;
    Property OnAfterLoadImg: TNotifyEvent Read FOnAfterLoadImg Write FOnAfterLoadImg;
  End;
 
Resourcestring
  lng_CannotLoadLotOfImg = 'Vous ne pouvez pas déposer plusieurs images en même temps.';
  lng_AllImages = 'Tous les formats images supportés';
  lng_JPGImages = 'Images JPEG';
  lng_BMPImages = 'Images BMP';
  lng_PNGImages = 'images PNG';
  lng_FileExtIsBad = 'file extension is incorrect';
 
Implementation
 
Procedure TImageDropDown<T>.ClearIMG;
Begin
  FImage.Picture.Assign(NIL);
  FreeAndNil(FIMG);
End;
 
Procedure TImageDropDown<T>.LoadIMG(Const sImgFileName: String);
Var
  Bmp    : TBitmap;
  Jpg    : TJPEGImage;
  Png    : TPngImage;
  FileExt: String;
Begin
  Try
    If Assigned(OnBeforeLoadImg) Then
      OnBeforeLoadImg(Self);
 
    FileExt := ExtractFileExt(sImgFileName);
    If ((FileExt = '.jpg') Or (FileExt = '.jpeg')) And (fiJPG In FormatIMG) Then
    Begin
      Jpg := TJPEGImage.Create;
      Try
        Jpg.LoadFromFile(sImgFileName);
 
        If Not Assigned(IMG) Then
          FIMG := T.Create;
 
        // cannot assign jpg to png directly
        If FIMG.ClassNameIs('TPngImage') Then
        Begin
          Bmp := TBitmap.Create;
          Try
            Bmp.Assign(Jpg);
            FIMG.Assign(Bmp);
          Finally
            Bmp.Free;
          End;
        End
        Else
          FIMG.Assign(Jpg);
 
        FImage.Picture.Assign(Jpg);
      Finally
        Jpg.Free;
      End;
    End
    Else If (FileExt = '.bmp') And (fiBMP In FormatIMG) Then
    Begin
      Bmp := TBitmap.Create;
      Try
        Bmp.LoadFromFile(sImgFileName);
 
        If Not Assigned(IMG) Then
          FIMG := T.Create;
 
        FIMG.Assign(Bmp);
        FImage.Picture.Assign(Bmp);
      Finally
        Bmp.Free;
      End;
    End
    Else If (FileExt = '.png') And (fiPNG In FormatIMG) Then
    Begin
      Png := TPngImage.Create;
      Try
        Png.LoadFromFile(sImgFileName);
 
        If Not Assigned(IMG) Then
          FIMG := T.Create;
 
        // cannot assign png to jpg directly
        If FIMG.ClassNameIs('TJPEGImage') Then
        Begin
          Bmp := TBitmap.Create;
          Try
            Bmp.Assign(Png);
            FIMG.Assign(Bmp);
          Finally
            Bmp.Free;
          End;
        End
        Else
          FIMG.Assign(Png);
 
        FImage.Picture.Assign(Png);
      Finally
        Png.Free;
      End;
    End
    Else
      Raise Exception.Create(lng_FileExtIsBad);
 
    FFileName := ExtractFileName(sImgFileName);
 
    If Assigned(OnAfterLoadImg) Then
      OnAfterLoadImg(Self);
  Except
    FreeAndNil(FIMG);
    Raise;
  End;
End;
 
Procedure TImageDropDown<T>.SelectIMG;
Var
  sFileMask: String;
Begin
  sFileMask   := '';
  If fiJPG In FormatIMG Then
    sFileMask := sFileMask + '*.jpg;*.jpeg;';
  If fiBMP In FormatIMG Then
    sFileMask := sFileMask + '*.bmp;';
  If fiPNG In FormatIMG Then
    sFileMask := sFileMask + '*.png';
  If (sFileMask <> '') And (sFileMask[Length(sFileMask) - 1] = ';') Then
    SetLength(sFileMask, -1);
 
  // if vista or +
  If Win32MajorVersion >= 6 Then
  Begin
    With TFileOpenDialog.Create(NIL) Do
      Try
        If fiJPG In FormatIMG Then
          DefaultExtension := 'jpg'
        Else If fiBMP In FormatIMG Then
          DefaultExtension := 'bmp'
        Else If fiPNG In FormatIMG Then
          DefaultExtension := 'png';
 
        With FileTypes.Add Do
        Begin
          DisplayName := lng_AllImages;
          FileMask    := sFileMask;
        End;
 
        If fiJPG In FormatIMG Then
        Begin
          With FileTypes.Add Do
          Begin
            DisplayName := lng_JPGImages;
            FileMask    := '*.jpg;*.jpeg';
          End;
        End;
 
        If fiBMP In FormatIMG Then
        Begin
          With FileTypes.Add Do
          Begin
            DisplayName := lng_BMPImages;
            FileMask    := '*.bmp';
          End;
        End;
 
        If fiPNG In FormatIMG Then
        Begin
          With FileTypes.Add Do
          Begin
            DisplayName := lng_PNGImages;
            FileMask    := '*.png';
          End;
        End;
 
        If Execute Then
          LoadIMG(FileName);
      Finally
        Free;
      End;
  End
  Else
  Begin
    With TOpenDialog.Create(NIL) Do
      Try
        sFileMask   := lng_AllImages + '|' + sFileMask + '|';
        If fiJPG In FormatIMG Then
          sFileMask := sFileMask + lng_JPGImages + '|*.jpg;*.jpeg|';
        If fiBMP In FormatIMG Then
          sFileMask := sFileMask + lng_BMPImages + '|*.bmp|';;
        If fiPNG In FormatIMG Then
          sFileMask := sFileMask + lng_PNGImages + '|*.png';
        If (sFileMask <> '') And (sFileMask[Length(sFileMask) - 1] = '|') Then
          SetLength(sFileMask, -1);
 
        Filter  := sFileMask;
        Options := [ofHideReadOnly, ofFileMustExist, ofEnableSizing];
 
        If Execute Then
          LoadIMG(FileName);
      Finally
        Free;
      End;
  End;
End;
 
Procedure TImageDropDown<T>.ImageWindowProc(Var Msg: TMessage);
Begin
  If Msg.Msg = WM_DROPFILES Then
    ImageDrop(TWMDROPFILES(Msg))
  Else
    originalImageWindowProc(Msg);
End;
 
Constructor TImageDropDown<T>.Create(AImage: TImage; APanel: TPanel);
Begin
  FormatIMG               := [fiJPG, fiBMP, fiPNG];
  FImage                  := AImage;
  FPanel                  := APanel;
  FFileName               := '';
  FOnBeforeLoadImg        := NIL;
  FOnAfterLoadImg         := NIL;
 
  originalImageWindowProc := APanel.WindowProc;
  APanel.WindowProc       := ImageWindowProc;
  DragAcceptFiles(APanel.Handle, True);
End;
 
Destructor TImageDropDown<T>.Destroy;
Begin
  If Assigned(FPanel) Then
  Begin
    FPanel.WindowProc := originalImageWindowProc;
    DragAcceptFiles(FPanel.Handle, False);
  End;
 
  If Assigned(FIMG) Then
    FIMG.Free;
 
  Inherited;
End;
 
Procedure TImageDropDown<T>.ImageDrop(Var Msg: TWMDROPFILES);
Var
  numFiles: Longint;
  Buffer  : Array [0 .. MAX_PATH] Of Char;
Begin
  numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, NIL, 0);
  If numFiles = 1 Then
  Begin
    DragQueryFile(Msg.Drop, 0, @Buffer, sizeof(Buffer));
    LoadIMG(Buffer);
  End
  Else
    Raise Exception.Create(lng_CannotLoadLotOfImg);
End;
 
End.
Fichiers attachés
Type de fichier : zip image drag and drop.zip (3,7 Ko, 20 affichages)
__________________
Il existe 3 sortes de gens: ceux qui savent compter et ceux qui ne savent pas.
ouiouioui est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 23h19.


 
 
 
 
Partenaires

Hébergement Web