Einzelnen Beitrag anzeigen

tomkupitz

Registriert seit: 26. Jan 2011
341 Beiträge
 
Delphi 12 Athens
 
#18

AW: PNG und Canvas via BMP und anderes

  Alt 11. Apr 2024, 20:11
Hallo,

hier ein kleines Bsp. zum spielen mit x und y:

PAS ###

Code:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtDlgs, Vcl.ExtCtrls, PNGImage;

type
  TForm1 = class(TForm)
    Image1: TImage;
    PaintBox1: TPaintBox;
    procedure FormClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure CopyAlphaChannel(SrcImage: TPngImage; DstBitmap: TBitmap);
var x, y: Integer;
    AlphaSrcLine, SrcLine: VCL.Imaging.PngImage.pByteArray;
    DstLine: PByteArray;

begin
  if (SrcImage.Width<>DstBitmap.Width) or (SrcImage.Height<>DstBitmap.Height) then
    Exit;

  DstBitmap.PixelFormat:=pf32Bit;

  for y:=0 to SrcImage.Height-1 do
  begin
    AlphaSrcLine:=SrcImage.AlphaScanline[y];

    SrcLine:=SrcImage.Scanline[y];
    DstLine:=DstBitmap.Scanline[y];

    for x:=0 to SrcImage.Width-1 do
    begin
      DstLine^[4*x+0]:=SrcLine^[3*x+0];
      DstLine^[4*x+1]:=SrcLine^[3*x+1];
      DstLine^[4*x+2]:=SrcLine^[3*x+2];

      DstLine^[4*x+3]:=AlphaSrcLine^[x];
    end;
  end;

  DstBitmap.AlphaFormat:=afDefined;
end;

//

function Png32ToBmp32(PNGGraphic: TGraphic): TBitmap;
var png: TPNGImage;

begin
  png:=TPNGImage.Create;
  png.Assign(PNGGraphic);

  result:=TBitmap.Create;
  result.SetSize(png.Width, png.Height);

  CopyAlphaChannel(png, result);

  png.Free;
end;

//

procedure TForm1.FormClick(Sender: TObject);
var bmp: TBitmap;

begin
  bmp:=Png32ToBmp32(Image1.Picture.Graphic);
  PaintBox1.Canvas.Draw(0, 0, bmp);
  bmp.Free;
end;

end.
DFM ###

Code:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 410
  ClientWidth = 635
  Color = clWhite
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OnClick = FormClick
  TextHeight = 13
  object Image1: TImage
    Left = 32
    Top = 40
    Width = 105
    Height = 105
    Picture.Data = {
      0954506E67496D61676589504E470D0A1A0A0000000D49484452000000480000
      0048080600000055EDB3470000000473424954080808087C0864880000167349
      44415478DAD55C095C55D5D65FE70E5C665054440907D4145044E5234B9F658E
      519698FA896699E6904DF62C9BEB7D4F9BF3E94B33A7A7D9E4FCB4D91C51CA94
      02CD2111D1A7283209287081CBBDE7BCFF3AF71EBA5CCF810B025FED1F9BBDCF
      3EEBECE1BFD75A7BAD7DF6B90239828741472F8CEF4E8F8E0A27810C2811E472
      49223D09624781F40370198DE2F6483D11CB48A20BA04843FE07E42F912048B8
      2639E26FF3FEF3F4D4CA235459255253049351A0C97776A279E322C9DFDB2877
      59B0F75B870E84E1EA36E47B2386C97D16A8DCDE673A8AEE2583260BF4227756
      92EC756E4EBE40F3FE954625669B7C2DD7E6E5A1A7D573E268484CB0A369B919
      23FE8D26497854D0D12DA8C028DF41901094D44E2EA061692F91F83E2E76A154
      141C409DBC7485A62D4CA38C4BA58D0A0EF779D59C7E34B44F089AD1A10B8EC9
      24BA1B5D9B8DBE0F44F31ECEFDACD967B2E07E12A0598AF41B94381011E96241
      198D5FF013656497906032EA68CD5F6FA121BDDB2A8DF0C0A2905F8E898873D4
      7B5D231A0D73233B48126749245C24309480060F9FBA42F1AF24372A40CB1EEB
      4B63068231244199E92E680C7DA6BFA03B7A355034F2CCDEFB909D893453C120
      FB4A3925CCDF4FC25234346E400792D1B173D4446E1FCFFB34B4F3682C4F22EB
      448174BB3143F4EBD9221AF27C9226BD8F8F7A5315151564B3D954EFED583088
      62BAB494791D6DDC2708BA35C80734B8CF241549A23805E3FEC231063A79BE98
      84FCF509E4103F46753A927F221AEB42BECE9911281B9C34080D9F658012DE48
      A3B66DDB52EBD6ADA955AB56141010A0B449C1C1C172EA1A8A8A8AA8B2B252EE
      ACD56AA573E7CEC98071BA7E6E4FEAD9318047D64BD0E976039CA07A708D56BE
      1CFD9D823A37297D10F23724D8C54AA0C13A41F8C655D7D41720840CC415B8DC
      80F45269958E0E5DF6A7949C40321A8DAAB3C7E0FCAECED4F3CAAC2AB16B6019
      0D0C2DA41626AB6010A803FA9F08924710C36E0020C6A18C44E90E5CFCE20068
      0C37DD1AD9C3A0EBD0500E9524F1049A988FD6368B50D2E5563D15947B50E655
      1FCA28F6A5D22A83C2250610B741FE66A4DD90764319DA273F5CFB0976A21247
      2C70009E2E8A623A6EE5206FE54A3CF42275F433539780520AF1AD205FA38D0C
      3A9B013AE90141273C078EEADA607193A413E0A25BA1434B1C00D15B8873EB83
      B653D92524FF87CA3E11455D8519C05C367B52568917522F2AAE3452A54DE723
      8AD2209D4E371CF46C2E74C7E39EF59A59A22A6419A41F90EEC030F67AE8A8C4
      D7A38ADA7A57D24D00ABBD4F05F979580194E80394A7A1861740DFBA61DC243D
      8FFF6F0B79EB1378364FE05ECBFA03247E86D267A0702E9B214A3900E6DC356F
      CA2AF5A6128B91ACA2104976BD762F627B3CA26B043DA188DE6564BF44BA0A26
      5CAA8F014001A08EFE0C5439F90328BD4E82FD26B04E8D6F403B5912D9A29883
      A6A3ECC37AB2601EFECD06305B2CB078F2214A678A7DE87C890F155B8C3AABA8
      8B4313CF8263B863FA86B2BA9B41447F76B214E804F180AFD12AB603505D217A
      ED217A5E061BE9049A82FBEF619C81F5AA5814EF6725BD0DB8DD530F563F86E5
      30511274274BABF4F41F700CEB98CB659E5429EA3BB11E02F95892ADD91BE694
      FA2C149CFD42146D2F18F5427A906725850798114B29C8D3C2DC142B48C2A750
      C2E1EEB683E5608D90BF71CC25E4DABAF3101EF80E97934451282A846E492FF4
      A38CABBE5454618039A89B03B2E7115B3A81D99C0029F96B880B31BC377C0C56
      1BEBA6C896251402AEF2D0896D41B611F76F7373453B26146CBCDF468AE3558B
      54D944710B92C9109F8A9C32139D28F407F7F85099D5D00E75AE401CE92EEB82
      B6CACFCFAF1036D125C49CC0C0C03C5F5FDF0A4F4FCF0AD62FE5E5E5A6B2B232
      CFE2E2E236F9F9F9C188A1252525416079A3BB6D8016D6B138D504266F079D14
      115422AF7A10395E2937210EAD7BDC58C5009058179222899BD880B2D9F4E517
      4B3DE9D7820028621FAAB40AFD61A47D02D28E1AE2587D0D7D640D0D0DCD888C
      8C4CE9DCB9730680C9D3EBF5A23B9C82C1EA0A0A0A5AC340EC72FCF8F1BE5959
      59DD6130D669CC227B19CF3E68D4D39E365E951415740D66411983148079588F
      FBC3EA50272403542B8624ED136DD248AB24545E023847189C126FAAB00A4331
      C00DA8AF56F3DE603094C7C4C424C5C5C5ED072805004AA21B0818305DB97225
      282525E5B69F7FFE7970555555AD2E11C65A86F8A05E90FEDDDADB42D1ADAECA
      7AC94B0F4ED209FBB0CAC5D4F2B40D008D29075E1E1A48A6237BBB4DA4DC8BA5
      264ACB0FC44AE54D55A27E1C6EAFC13D93D60C00184BEFDEBD93060F1EFC35C4
      A9CC1D4EA96FDE6C367B2525250D075877C215F1AA85D682EC6C1D89FF6A0D4E
      EA0590D812F7D25B3B924EB71F20856A70502E2FF36C52F7BEEEA640C5C80E85
      2A4CE5653C253790CEC22A2EAFA2E118FC4690FA6A75282424E4747C7CFCFAB0
      B0B02C87CDD2A44A3A3B3BBBDD37DF7C33FEFCF9F311B53C5789E40170D2D660
      EF0AEADBA6983A639533EAC4DB41027B8ABC5500DAC500FD03654F5ECF9AE2E3
      E8C2D2122CE53FE7B5A09357FC6011EB7BE3C9DD10132D7B42ECD7AFDFF72347
      8EDCEEE1E161AD97EC287DFA7DCF45D961702B8083F43B77EE1C79F0E0C17866
      602D7103DD489351F821CCD74CB78414C95638ECA45781C1AB2AF42F0B791B12
      6E038B2551CD7D9F3DF0C487C1AE91189894BC9674CDA26F05433819245DD596
      58705505B8665DDFBE7D5388A85ECB7CEAB829ABD5CAFB6DFE685A3543BBB9CC
      FFFAEBAFD1DBB76F9F6AB158BC349ECB821EBBCDA4972E75C7F21F1B5C44811E
      55581DA583185FCCEF75CBAE4D1F361479AF92018A936F92C4A2D51F55A65F2C
      F5A21FB28328DBEC05079378B5BA5FAD73E0968A3163C62C8B888838A9B58A35
      17409C3F73E64C970D1B363C565151E1A341F31D404A0830592D0C10DB497074
      FB413292C02C5E769B4FFC0E86E53DF27607C25D78962D6A8328896F83B19F63
      D1FAE972109D84BD53250A93717F35FB52AE6C884A2D00E7C35EBD7A1DAB9748
      39855FC63EB44AADBCEFA6B553A96E1B4D359C3A75AA1B407A1C22E5A9266D00
      E82988D6925088DA80902B30242B59A257628C53014F2518E50E80F5138B985D
      E60561150AE2E1A0F512255D7E7A911F255F6E45D72A0DECC4B233DBDA75F610
      6C10ABB558C27FAACB0E6A4E0E52F2478E1C89D9B66DDB0CD84C7A95BE5D4336
      CAA417B37B065DA5FF012779196D9D40922689126FDDCE939FC95B3F5A9E2449
      107D75A41B89A7375DB31868DFA53658B57CE109D2123C344B6D96B08CEF193D
      7AF4E7E022CD4D2EC18DCD3000B4AA168004AD67DDA9FBEBAFBFBEEFD0A143F1
      6AFD07CD67F8F7409097856E6F9F4F617EE5ACB013F1E4169457CA75E5ACBF17
      85BF4B0E5C09D98D3898D38ACAAD862EDC7FB5253D2828E8D2CC99335F37994C
      965A2D51F738480BA0476E8483E4F158AD86E5CB973F939B9BDB5985A602A2D6
      1F96F6D1A8965729AE6D21F9186DD5ABA8BCD13A6B5438BD3A314A799F2473CD
      2180935BCEA22B2C55E31E9459274E9CB8B05BB76E190DD10FAE013A68A55A39
      74D0346AA00E720EB08FC2D6AC59330F6078A87211490FF819ABA46E8125D41F
      20C13692EF6D4ECE8256D60B1411E62F17B468D18206DF3596ED1D162D76424F
      A2D8CF1579AC563F8C1B376EADBCE8D5327B7F040E72E409BA687C5A5ADA1015
      9A0A24D190CA33FC7E24F3F821FAEDF811B9CD9317AED69C9DE8E8688291A774
      E831248B5D1B84BD533575EAD4F9703C2FD7D5B93F1040129CDDA065CB96BD06
      DFCDD39506C92B4816701E5639AD5BB7AE1A931A0061B9A6AE5DBB2A1DE24DFC
      7EAE2CD9BD7BF79F12131357DF28DB3B87A61631256CD9B225F1E8D1A377B896
      03A0D3883DEC5989DE7DF7DDEAF771351A9F33670E41E972361C2BD369B5D99B
      3469D23BD03DA7DD993D776696F3CDC1419CCFCACABA69D5AA55AF3856C01A34
      D04FCC0CA97CBD69D326CACCCCAC0910BFCC9B366D9AD219799FDAB5115F5FDF
      82B973E7BE08F0C43F23407CB968D1A2570A0B0B435568E621BEC3D7292929B4
      7BF7EE9A00858787D3D8B163957757BCCF33D69515E188EE18356AD4E6C66279
      2534978871F8FEFBEFE3939393EF732D07077D8F318F60CC8E1D3BC6F6534D80
      626363E9CE3BEFE4AC1E8467103BB8CEC2F8F1E3FF19151575DCDDD97377669B
      8B83389F9191110E253C4F85260FB1332ECDD7AE5DA30F3EF8A02640C3870FA7
      3E7DFAB0960A01FD29443FE70A205695D051CF07040494FC99012A2B2BF35AB8
      70E102AC667E2E3415C8F644CC6480962E5D5A13A0112346504C4C0C03140B30
      0E92E3B58D12FCFDFD739E78E289D7E0B9AB1FB7B881D09C2286D5490077BC98
      9F9FDFC1B95CB28761C06A774949092D59B2441D208471889FBB221F16167602
      F6CF22879FF3A7E520CE43C46660958A55A161AF6185C562A1D5AB57537171B1
      2A407C42E243D78A61FF244F9830616D7D3AF7470568FBF6EDE353535387AAD0
      3C87F80EDB406BD7AEA5BCBC3C55809E06FDBBAEAC09CF7D574242C286C66477
      2534A78871D8B973E7A803070EDCE35A0EC7763ED48B6C27C177D304E865C4D7
      5C91EFDFBFFFD7A0F977737250CC27CB67EB4C268BD6B30DE5A0A4A4A4117BF6
      ECB9DFB51CE11FB89CCBAF966AE320DE287ADDB562B681EEB9E79E4DCD09907F
      74545AF8DCC757E93CD5B7541A0AD0DEBD7BEFDEB76FDF7DAEE500E62D645FA8
      0BA04741B4C495FD2223239360077DD2D8ECCE414BC438F845F5480B7FE6F155
      7A6F6F4B63B5F7D5575F8D397CF8F008D77260F412338702506E6EAE2A400F20
      AE7545BE53A74E690F3DF4D0D2A6E0A0F4575E7FAAF4B7D3915A34FE3D238E84
      3FFBE44AE6A4C6E0A0CF3FFF7CCAA953A76E53A1E1D75F4B18204D1D043A3E46
      BBD7B5E2A0A0A0ACD9B367CF37180C6263036483F176E6ADC5D3015294168D9A
      B83510205ABE7CF9BCECECEC2E2A3A6834B25F949797D3871F7E289FB2AD6149
      3B38A823884E207A39B31F80297DFAE9A75F84C36A6E2C56770ED632B347E6DB
      8B67949E4CEFA545E31BD5E308C46DA5E106C40D83F780C3FA77A42D9DCB014E
      151206E0A4AAA1386CD830D9D540F004386711DBBA223C79F2E4B7BA76ED9AD9
      D81CA4E46DE672D3993717CD2AFD2DBD36713B0A715BA1A5B8EBCA676565B55F
      B972256F90B91E072C42EC826C912A4037DF7C338D1E3D5AB9FC0EF6C030D746
      060E1CB80D407ED3540071DE5666F6CA84B895FC965EA7B8E9BD3CABEA0BD0FE
      FDFBEF801D344185E610627FBE3E7BF62C6DDCB8B126409D3B7796B73BF8150E
      C23378E62D57F66CDDBAF5B9C71E7B8C4D808672B85BC166367B4027D52A6E7E
      7671E3D5ADB23E75AF58B162CEC58B17235CCB01CEEB485E62CCD2D2D268C78E
      1D350182132AEF283A06DF1740A5A8D81D3473E6CC97434242729A8A83AAC5AD
      BCDC94F9E6A2992527D3A3B468FCA37A1C0D9FF794DBE2565858D862F1E2C56F
      E252E74A83958B4F9CC9BB64BC59C69B663500E23065CA14F97301B27BF29982
      CAC1F2D8D8D86F61306E6D521672046B6999271437AF6E3D3539A96704ECA427
      56B2B8D5551F9FFE808B91A0C23D0588BCCB68E125FEA38F3EA29C9C9CEB011A
      326408EF1A2A33FF77242FB8CE82A7A7671996FB57030303AF35250729792BC4
      8D7512C42D5ACB92F68F62C50D13C0CBAB42ABBEB2B232EFF7DF7FFF6F66B339
      D09506096FFE3CC1F9AAAA2A7AEFBDF7AA31A901505858182526262AA84640CC
      7813FBBAB380F0CBBE80DDF425398EB9342540B5895B75AAD3891D664F5B16F4
      975B8F68D501D762285C8C714AD79C68F82DE10056D25C7EFCF871B6B4D50182
      AD438F3CF288F2250E8BD9568034CA95258D46A379FAF4E9F3838383F3EB2935
      0D0E1037936C275D2F6E62DB31A3B6B61B77DFF782C6F9C72B57AE04C2387C19
      869FBFEB3DC60E09EB1F1BE3E5FC46E33A8038F0BE348B994359DFEAB0AA8DAE
      3302D7E35787EB213635072979ABB9DC43D649274E453B384802385BDA8D1FCD
      1BEEA4D107E1B3CF3E9B0AD7224EA56EDE1DBD1B710717F306196CA41ADFA85D
      07102BE9499326C9DCE4A8E74BC4BBD456B4A143877E3A60C0807DCD05905DDC
      2A58DCC049E951C1F7C56F6B9F78FFB7CC085ACF61358A83C8C8C76854680E20
      3B58E19EE4E464393A07558386ED217E0DE49815FE32270DD1E44AC7C7EE00E6
      7BB0A1FED350D1694860712B3AF47354ABDB07A60A7AED63C5B077DAC12B7FD6
      62B15C775498B146BC05E392BF0B63BF8BFD2FF6C3EA0488BF0A7CF8E1871531
      E3F46F485E72E12039F5F1F1297EF0C107DF03E7E5340707A9F541ED7E7E7E7E
      10C0791A6E431B0DEE7E1FD9A7947EC2C2A61F7FFCF13A2C344D6297F7F47E64
      37A2FAAA750E4A3D17202D6AD5AA55C11F01A0A2A2A2C075EBD63D09E51CAA41
      F31BE25F902DE472C57BE7CF3FDD06085EBBBCA2C1EE515832925D19C4166AF4
      7E7E7EF913264C58121A1A9A4DFF8F213737B7CDA79F7E3A1B0AB79DDA7D8CA3
      147108C671D871CD6F5B29353555B5BE5A9DAAC8C848BAFBEEBBAB450D814FD8
      F3AEA25E6D664C2653597C7CFCC7D1D1D17C2AAD49B8C639EFBA5A9D387122EA
      CB2FBF7C081C11A021566CF3CCC265F5A1D1F3E7CFCB8EA9D6D7D5757A9D0909
      09D4AD5BB7DF1F1084B948DE10B4BF4D97FAF4E9B30B2BDC57DEDEDEE6E60008
      0AD673CF9E3D23781B15AE825EE33909202C805DF7AA3216DED658BF7E3DDB49
      9AE3AF1320BD5E4FB366CD9245CEB1AAF13ECA02943F5BCBF392BFBF7FDEE0C1
      83B700ACB4BADAB89170ECD8B1A85DBB768D85DE09A9AD3F006E195256CA5685
      A1B66DDB46E9E9E9B5D6EFD6BE45CB962D09FAA51A24C773CF3B56B7EBCE4E3B
      2B6958DBE7060E1CF86D8F1E3D4E4004EBBD7FA39687BF643C7DFA7477AC3C23
      B3B3B3BBAAD1BA74E9086E0D47313BA5B2DEE1FD663E49A6255AF50288C3A041
      83282E2E4ED92FE2C087CB5790C68F10B87456E2952E222222252A2A2A1526C1
      657EC75F1F50F8AC333CECB6D0333188B1E01856C2AA62A751C759247C307DBF
      6312E9975F7E2170DF8D03C462C6761173128F17913F92E59FAFF050A377F44D
      D580638F01B6D3D59B6EBA2903DC750160E5B238F2E70CCE5F1CC2B8F3641B06
      A00463B6432F5CB8D0ADB4B4B485A4F1A18A53FD9A63C2B3FC2D3E1FE494377B
      E0E1F3069AEAF25E2F80DAB56B4793274F561AB9195CC4E717FDEA9AF5BA8CB9
      C65EC59C0CDBDA9E3B0D7D148B2C83455BB76E2588EB8D01D4BB776FF9B590A3
      1116ABA9EE0CE40F0A1087D982FD88A1AAFFD5608004FBC72CF95AC6A2737056
      D44D1DD0540EE266A882C7DDA4DF4376516B5C80103A40BCCEB9C919C724FB4E
      1D2BC66872E31765EA99E72DD61348F94832BF09E66D97ABC81BDCA8E3027B06
      48CD8DCD41FCDEE8B49B00BD8584B76CF99A37B9F85B33FECA98DF2878371014
      76B5D98FDAC51C83C8BB0CCA97D31CF955C41037EA6377A807BB1D8D0690E304
      BE096D1493E3635EAD808679BF9ABFDE3BE72C6992FD6D421B6463A1287B821B
      6F469E41E79794BE82DD29161CAB0DFB4CB948CF20F28F9A1CC733BC38E422DA
      947A25FB3E8F62C48E8098F17EA9AE8EFEB11F762BA278E0C0814615310E1FA3
      33895AB344F6E5FD715C2E530650DD981B9F2F09B5FC7E90CA40890F5CC25196
      4D110606CFB0F951EB7766087F457651A3E92058C174EFBDF72A9DEA8899E75D
      C4301580AA30CB0B7039DF0194FC2B52BCD702BB87E0E953505050F58CD7059C
      33104AB87AF5AA0C4A4646069F9C978FA88C1F3F9E3A75EAA490F0D7CB7C8470
      A20640C9E8E3488C81BFA7AFF10EACC100F196C78C1933C8CBCB4B19007F47F6
      36F2BC9FCB53C7F6FA5E94B3139BE4BC8239DB19FC2C1C5839AFEC58D61514D7
      80F78B0B0B0B65A38E7FAECB3930F80C92639B981C8EF4FF227D9AEC0B043764
      46D9C7489F679C998E5FF1288735B5C27F0158892A7D2873AF54000000004945
      4E44AE426082}
  end
  object PaintBox1: TPaintBox
    Left = 143
    Top = 40
    Width = 105
    Height = 105
  end
end
Beste Grüße
  Mit Zitat antworten Zitat