Delphi-PRAXiS
Seite 2 von 4     12 34      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   PNG zur Laufzeit in ImageList einfügen (https://www.delphipraxis.net/150954-png-zur-laufzeit-imagelist-einfuegen.html)

Bernhard Geyer 10. Feb 2011 15:20

AW: PNG zur Laufzeit in ImageList einfügen
 
Im Downloadberech zu Delphi müsste es doch ein PNGImageListe geben? Damit bleibt semitransparenz erhalten.

Ansonsten schau dir mal die Kompos von LMD an. Dort gibt es auch eine PNG-Imageliste.

Uwe Raabe 10. Feb 2011 15:31

AW: PNG zur Laufzeit in ImageList einfügen
 
Vielleicht macht die TPngImageList der PngComponents das ja besser. Da kann man direkt ein TPngImage in die Liste einfügen. Habe ich aber jetzt nicht ausprobiert.

EWeiss 10. Feb 2011 15:32

AW: PNG zur Laufzeit in ImageList einfügen
 
Du kannst über einen umweg die PNG's mit GDI+ laden und zu TBitmap umwandeln
ohne den Alphachannel zu verlieren.

Schau mal mein Sample GDIClock_v14 an.
Delphi-Quellcode:
var
  hbmReturn : HBITMAP;
Delphi-Quellcode:
     
hbmReturn := LoadSourceImage('xxx.png');
if hbmReturn <> 0 then
begin
  xxxImg := TBitmap.Create;
  xxxImg.Handle := hbmReturn;
end;
Delphi-Quellcode:
// This function is used to load PNG file which has alpha channel and
//  returns the handle to bitmap.
// The rgbReserved element of bitmap gets the data of alpha channel.
function LoadSourceImage(ImgFile : WideString) : HBITMAP;
var
   img: cardinal;
   hbmReturn: HBITMAP;
   format : integer;
begin
   Result := 0;

   if not GDIReady then
      exit;
   if not FileExists(ImgFile) then
      exit;

   GdipLoadImageFromFile(PWideChar(ImgFile), img);

   if img <> 0 then
   begin
     GdipGetImagePixelFormat(img, format);
     if format = PixelFormat32bppARGB then  // Accept files with alpha channel
     begin
       GdipCreateHBITMAPFromBitmap(pointer(img), hbmReturn, $000000);
       if hbmReturn <> 0 then
          Result := hbmReturn;
     end;

     GdipDisposeImage(img);
   end;
end;
gruss

David Martens 10. Feb 2011 17:12

AW: PNG zur Laufzeit in ImageList einfügen
 
Danke erstmal an alle.

Ich habe es fast hinbekommen. Die TToolButtons haben nicht mehr hingehauen, aber das konnte ich mit dem TAdvDockPanel von TMS erschlagen. ABER

Ich habe Text auf einige Bilder geschrieben:
Delphi-Quellcode:
  Image.Canvas.Brush.Style := bsClear;

  Image.Canvas.Font. Name := 'Small Fonts';
  Image.Canvas.Font. Size := 5;
  Image.Canvas.Font. Color := Color;

  Image.Canvas.TextOut(16 - Image.Canvas.TextWidth(Text), - 2, Text);
Leider hat der bei einem PNG den Alphachannel vom Untergrund. Kann ich denn den Text direkt auf das PNGImage schreiben ohne den Umweg über den Canvas?

Das wäre SUPER dann hab ich es genau so wie es es wollte.

Danke
David

P.S.: es hat übrigens mit TPNGImageList geklappt.
@EWeiss: sorry, aber eine weitere DLL an alle Benutzer ausliefern wollte ich dann doch nicht.

Thom 10. Feb 2011 17:14

AW: PNG zur Laufzeit in ImageList einfügen
 
:gruebel:
Ähm... Worin besteht das Problem?
Wird einem Bitmap ein PNG-Bild zugewiesen, wird automatisch (falls vorhanden) die Transparenz übergeben und das Bitmap hat 32 Bit Farbtiefe (RGBA). Wird das Bitmap jetzt zur Image-Liste hinzugefügt, wird auch dabei die Transparenz berücksichtigt. Einzige Bedingung dabei: TImageList.ColorDepth muß auf cd32Bit stehen.

Delphi-Quellcode:
procedure TForm1.FormShow(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp:=TBitmap.Create;
  try
    //Die Übergabe der Bilddaten erfolgt dabei von
    //TPNGImage.AssignTo(Bmp):
    Bmp.Assign(Image1.Picture.Graphic);
    //Die Maske ist egal, weil der Alpha-Kanal schon
    //im Bitmap enthalten ist:
    ImageList1.Add(Bmp,nil);
  finally
    Bmp.Free;
  end;
end;
Weshalb also die ganzen Kopfstände mit GDI+, TPngImageList und Co.?

P.S.:
Text wird nun einmal ohne Hintergrund gerendert. Willst Du das nicht, setze
Delphi-Quellcode:
Canvas.TextFlags:=Canvas.TextFlags or ETO_OPAQUE;
//...

himitsu 10. Feb 2011 17:18

AW: PNG zur Laufzeit in ImageList einfügen
 
Sicher daß TBitmap Transparenz kann?

Ich dachte das hätte im 32-Bit-Modus RGBP und nicht RGBA?
(Red, Green, Blue, Palette/Alpha)

Thom 10. Feb 2011 17:36

AW: PNG zur Laufzeit in ImageList einfügen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Ja, TBitmap unterstütz den Alpha-Kanal.
Ich hatte das vor einigen Tage getestet, als ich eigene Marker-Icons für Google Maps erstellen wollte.
Anhang 33333
Gezeichnet wird dabei in ein TPNGImage, was für sich genommen noch nichts besonderes ist. Erst bei der Erstellung des Schattenbildes wird es interessant:
Dabei wird das PNG-Bild als Maske samt Transparenz in ein Bitmap kopiert, perspektivisch verzerrt, gefiltert und wieder zurück in ein (neues) PNG-Image kopiert - und das alles unter Beibehaltung der Transparenz.

David Martens 10. Feb 2011 17:38

AW: PNG zur Laufzeit in ImageList einfügen
 
Also ich hab folgendes probiert, aber geht noch nicht:

Delphi-Quellcode:
      Image := TPngImage.Create;
      Image.Transparent := true;
      Image := GetPNG('ABC');

      if Image <> nil then
      begin
        if Color <> clNone then
        begin
          Image2 := TPngImage.CreateBlank(COLOR_GRAYSCALE, 1, 16, 16);

          Image2.Canvas.Brush.Style := bsClear;

          Image2.Canvas.Font. Name := 'Small Fonts';
          Image2.Canvas.Font. Size := 5;
          Image2.Canvas.Font. Color := clBlack;

          Image2.Canvas.TextOut(16 - Image.Canvas.TextWidth(Text), - 2, Text); // HIER schreibe ich den Text ins Image2

          for y := 0 to Image2.Height - 1 do
          begin
            for x := 0 to Image2.Width - 1 do
            begin
              // HIER wird der Text ins Image "kopiert"
              if Image2.Canvas.Pixels[x, y] = clBlack then
                Image.Pixels[x, y] := clBlack;
            end;
          end;
        end;
      end;

      with Image do
      begin
        for y := 0 to Height - 1 do
        begin
          pb := AlphaScanline[y];
          for x := 0 to Width - 1 do
          begin
            ColorRGBToHLS(ImageColor, NewHue, NewLuminance, NewSaturation);

            // HIER soll der Text im Image nicht überschrieben werden
            if Pixels[x, y] <> clBlack then
              Pixels[x, y] := ColorHLSToRGB(NewHue, Trunc((pb^[x] + NewLuminance) / 2), NewSaturation);
          end;
        end;
      end;
Lieder sehe ich noch keinen Text. Wenn ich TextOut dierekt auf Image mache, sehe ich nur dort Text wo im Ursprungsimage keine Transparenz vorhanden ist.

P.S.: ach so mit ColorHLSToRGB(NewHue, Trunc((pb^[x] + NewLuminance) / 2), NewSaturation); wird das PNG (hat nur den Alphachannel) in der "neuen" Farbe eingefärbt.

David Martens 10. Feb 2011 17:39

AW: PNG zur Laufzeit in ImageList einfügen
 
@Thom: und das Ganze funktioniert auch noch mit TImageList?

EWeiss 10. Feb 2011 17:46

AW: PNG zur Laufzeit in ImageList einfügen
 
Zitat:

EWeiss: sorry, aber eine weitere DLL an alle Benutzer ausliefern wollte ich dann doch nicht.
He?
Was für eine DLL?
GDI+ ist auf allen Systemen enthalten.

Zitat:

Weshalb also die ganzen Kopfstände mit GDI+, TPngImageList und Co.?
Weil GDI+ und das ist Fakt einfach das beste ist wenn es um PNG Dateien geht.
Zumindest meine Erfahrung.

gruss


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:42 Uhr.
Seite 2 von 4     12 34      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz