Git Product home page Git Product logo

Comments (8)

AngusJohnson avatar AngusJohnson commented on May 27, 2024 1
  Image1.Picture.Bitmap.PixelFormat:= pf32bit;
  Image1.Picture.Bitmap.SetSize(img.Width, img.Height);
  img.CopyToDc(Image1.Picture.Bitmap.Canvas.Handle);

  img.SetSize(Image1.Picture.Bitmap.Width, Image1.Picture.Bitmap.Height);
  img.CopyFromDC(Image1.Picture.Bitmap.Canvas.Handle, img.Bounds);
  img.saveTofile('test.bmp');

from image32.

Escain avatar Escain commented on May 27, 2024

Thank you :-)

from image32.

Escain avatar Escain commented on May 27, 2024

Hi,

I hope this message find you well.

Is there an alternative for non-windows Lazarus?

from image32.

AngusJohnson avatar AngusJohnson commented on May 27, 2024
procedure CopyImage32ToBitmap(img: TImage32; bmp: TBitmap);
var
  ms: TMemoryStream;
  bmpFormat: TImageFormat_BMP;
begin
  ms := TMemoryStream.create;
  bmpFormat := TImageFormat_BMP.Create;
  try
     bmpFormat.IncludeFileHeaderInSaveStream := true;
     bmpFormat.SaveToStream(ms, img);
     ms.Position := 0;
     bmp.LoadFromStream(ms, ms.Size);
  finally
    ms.Free;
    bmpFormat.Free;
  end;
end;

procedure CopyBitmapToImage32(bmp: TBitmap; img: TImage32);
var
  ms: TMemoryStream;
  bmpFormat: TImageFormat_BMP;
begin
  ms := TMemoryStream.create;
  bmpFormat := TImageFormat_BMP.Create;
  try
    bmp.SaveToStream(ms);
    ms.Position := 0;
    bmpFormat.LoadFromStream(ms, img);
  finally
    ms.Free;
    bmpFormat.Free;
  end;
end;

from image32.

Escain avatar Escain commented on May 27, 2024

Thanks you for your answer.

Maybe some people uses this library to load and save images into files, however I believe that most people want to load images into their GUI. Most GUIs are made of TControls, some are custom, but most are standard or inherited from standard TControls. For instance, our custom button control (showing svg images) inherit from TBitButton, which accepts TBitmap.
Unfortunately, most of Delphi/Fmx/Lazarus works with TBitmaps, and consequently, code that could need Image32 soon or late needs to convert those TImage32 to TBitmap.
This is just a little context to explain why I think that converting from and toward TBitmap (or TPictures) makes the cornerstone of this library.

Few months ago, we had CopyToBitmap and CopyFromBitmap for this purpose, but since latest changes, they are only usable under VCL Windows, which makes them mostly useless.

Saving into a stream and re-loading it could possibly work but:

  • BMP does not accept transparency, consequently, your example above will not work most of the time.
  • PNG could work, but TPngImage is not supported by Lazarus, and PNG format is not supported by Delphi.
  • PNG image compression, creating the headers, color tables, etc. Saving into a stream is a huge overload for something that is probably called quite few times for drawing the GUI.

IMHO, it would be great to have something cross-platform, something like CopyToBitmap and CopyFromBitmap. Working on Delphi, Lazarus, Windows/OSX/Linux, etc.

What, would be really fantastic :-)

I created the following functions to make the new CopyToBitmap compatible with the previous overload:

 {$IF DEFINED(VCL)}
class procedure ClearBitmapToTransparent(ABitmap: TBitmap);
var
  x, y: Integer;
  p: PRGBQuad;
begin
  if not Assigned(ABitmap) then Exit;

  // Ensure the bitmap format supports transparency
  ABitmap.PixelFormat := pf32bit;

  // Access the raw bitmap data
  ABitmap.Handle;

  // Loop through all pixels
  for y := 0 to ABitmap.Height - 1 do
  begin
    p := ABitmap.ScanLine[y];
    for x := 0 to ABitmap.Width - 1 do
    begin
      p^.rgbReserved := 0;
      p^.rgbRed := 0;
      p^.rgbGreen := 0;
      p^.rgbBlue := 0;
      Inc(p);
    end;
  end;

  ABitmap.Modified := True;
end;
{$ELSE}
  // TODO something similar for LCL
{$ENDIF}


class procedure CopyToBitmap(origImg32: TImage32; destBmp: TBitmap);
begin
  if Assigned(destBmp) then
  begin
    {$IF DEFINED(VCL)}
      destBmp.SetSize(origImg32.Width, origImg32.Height);
      destBmp.PixelFormat := pf32bit;
      destBmp.AlphaFormat := afDefined;
      ClearBitmapToTransparent(destBmp);
      origImg32.CopyToDc(destBmp.Canvas.Handle, 0, 0, origImg32.HasTransparency);
    {$ELSE}
      // TODO Non-Windows or Non-VCL needs fix on Image32
    {$ENDIF}
  end;
end;

from image32.

AngusJohnson avatar AngusJohnson commented on May 27, 2024

Maybe some people uses this library to load and save images into files, however I believe that most people want to load images into their GUI. Most GUIs are made of TControls, some are custom, but most are standard or inherited from standard TControls. For instance, our custom button control (showing svg images) inherit from TBitButton, which accepts TBitmap. Unfortunately, most of Delphi/Fmx/Lazarus works with TBitmaps, and consequently, code that could need Image32 soon or late needs to convert those TImage32 to TBitmap. This is just a little context to explain why I think that converting from and toward TBitmap (or TPictures) makes the cornerstone of this library.

I agree that it's very likely that most users will use this library in combination with either FMX or VCL. However I don't want the library to be tied to either of these, but I do also want the library to be easy to use.

Few months ago, we had CopyToBitmap and CopyFromBitmap for this purpose, but since latest changes, they are only usable under VCL Windows, which makes them mostly useless.

No, certainly not useless (though perhaps useless to you 😁).
Nevertheless, I will concede that removing these 2 methods may not have been well thought out.

Saving into a stream and re-loading it could possibly work but:

  • BMP does not accept transparency, consequently, your example above will not work most of the time.

Yes, but that's because there's a bug in TImageFormat_BMP.LoadFromStream in Img32.Fmt.BMP.pas (and I'm about to upload the fix for that).

  • PNG could work, but TPngImage is not supported by Lazarus, and PNG format is not supported by Delphi.
  • PNG image compression, creating the headers, color tables, etc. Saving into a stream is a huge overload for something that is probably called quite few times for drawing the GUI.

However the TBitmap class doesn't directly support these formats.

IMHO, it would be great to have something cross-platform, something like CopyToBitmap and CopyFromBitmap. Working on Delphi, Lazarus, Windows/OSX/Linux, etc.

I agree, and I'll see if I can fix that. 🤞

from image32.

AngusJohnson avatar AngusJohnson commented on May 27, 2024

Should be fixed now 😁.

from image32.

Escain avatar Escain commented on May 27, 2024

That was quick! Thank you :-)

I created a PR to fix few minor compatibilities with Lazarus.
Also, transparent bitmap was black without setting up pf32bit and afDefined.

After this, it seems to work well (not yet fully tested, but at least the majority) on all 3 platforms I am working on: VCL+Windows, LCL+Windows, and LCL+Linux.

After this, I still get a glitch in some cases on VCL:

      bmp := TBitmap.Create();
      img32 := TImage32.Create();
      img32_2 := TImage32.Create();
      
      img32.LoadFromFile('button_logo.png');
      img32.CopyToBitmap(bmp);
      bmp.SaveToFile('test1.png'); //Comment this out, and then, test2.png is ok.
      img32_2.CopyFromBitmap(bmp);
      img32_2.SaveToFile('test2.png');
      FreeAndNil(img32);
      FreeAndNil(img32_2);
      FreeAndNil(bmp);

button_logo
Test2

I wonder if you know the underlying reason for this?

from image32.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.