Setting Canvas.Pixels data just results in black lazarus / 39-Graphics ---------------------------------------------------------------------- zellfaze · 2013-05-08 19:55 I'm having a ton of trouble trying to work with Canvas.Pixels on a TBitmap object. I've been trying to get the following function working.  I understand that this function actually does nothing, but if I can get it working, then I can modify it to do what I need to do.  I am less worried about speed, and more worried about readability, so I don't want to muck with any of the ScanLine based stuff if at all possible. Would anyone be willing to help me out?  When I run this function it inevitably returns a TBitmap object that is just a black box. Code: [Select]function copyWithCanvas(Bitmap: TBitmap): TBitmap; var   DestBitmap: TBitmap;   x,y: Integer; begin   DestBitmap := TBitmap.create;   DestBitmap.PixelFormat:= bitmap.PixelFormat;   //Set the width and height of our destination to match the source   DestBitmap.Width  := Bitmap.Width;   DestBitmap.Height := Bitmap.Height;   //Begin to copy   x := 0;   y := 0;   //Copy each column   while (x < DestBitmap.width) do   begin     //Copy each row      while (y < DestBitmap.height) do      begin         //Copy the pixels themselves         DestBitmap.canvas.Pixels[x, y] := Bitmap.canvas.Pixels[x, y];         y := y+1;      end;      x := x+1;   end;   Result := DestBitmap; end; EDIT: Modified function name ---------------------------------------------------------------------- zellfaze · 2013-05-08 20:09 Code that calls the function is (snipped): Code: [Select]  original := TBitmap.Create;   try     // Load from disk     original.LoadFromFile(filename);     //Mirror     original := copyWithCanvas(original);     // Write back to another disk file     original.SaveToFile('output.bmp');   finally     original.Free;   end; ---------------------------------------------------------------------- Blaazen · 2013-05-08 20:35 You reset y only once. You have to reset it (y:=0;) before each loop: Code: [Select]//Begin to copy   x := 0;   //Copy each column   while (x < DestBitmap.width) do   begin     //Copy each row      y:=0;      while (y < DestBitmap.height) do      begin         //Copy the pixels themselves         DestBitmap.canvas.Pixels[x, y] := Bitmap.canvas.Pixels[x, y];         y := y+1;      end;      x := x+1;   end; ---------------------------------------------------------------------- zellfaze · 2013-05-09 12:32 *facepalm* Thank you.  I can't believe that I didn't catch that. ---------------------------------------------------------------------- Blaazen · 2013-05-09 12:37 I think for..to loops can do the same job in 3 lines. ---------------------------------------------------------------------- zellfaze · 2013-05-09 14:24 They can.  Though once I modified the code to do what I actually needed it to do the Begin and End Statements needed to stay.  The for..to loop is still more efficient though.  I'll likely switch to that. This is what I actually was planning on making using that code: https://github.com/zellfaze/lazarus-mirror-bitmap There seemed to be a lack of instruction on how to mirror a bitmap when Googling.  At some point I'll figure out how to do it with Scan Line and add some documented functions to show how to do that to that unit. Thank you Blaazen for your help.  I still can't believe I didn't notice that problem...