Comments (8)
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.
Thank you :-)
from image32.
Hi,
I hope this message find you well.
Is there an alternative for non-windows Lazarus?
from image32.
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.
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 TControl
s, some are custom, but most are standard or inherited from standard TControl
s. 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 TPicture
s) 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.
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
TControl
s, some are custom, but most are standard or inherited from standardTControl
s. For instance, our custom button control (showing svg images) inherit fromTBitButton
, which acceptsTBitmap
. Unfortunately, most of Delphi/Fmx/Lazarus works with TBitmaps, and consequently, code that could need Image32 soon or late needs to convert thoseTImage32
toTBitmap
. This is just a little context to explain why I think that converting from and towardTBitmap
(orTPicture
s) 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
andCopyFromBitmap
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
andCopyFromBitmap
. Working on Delphi, Lazarus, Windows/OSX/Linux, etc.
I agree, and I'll see if I can fix that. 🤞
from image32.
Should be fixed now 😁.
from image32.
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);
I wonder if you know the underlying reason for this?
from image32.
Related Issues (20)
- Refresh Bug - Layer 301 example HOT 4
- Glitch when resizing TBitmap HOT 13
- rotate(270) does not adjust with and height for viewport and image when rendering HOT 1
- Wrong Encoding Detection when file has no BOM
- TImageFormat_QOI.SaveToStream declaration error
- Delphi 12 Packages
- Typos in Package HOT 2
- Layered images on top of a TPanel HOT 1
- SetRoundMode should be used with try/finally
- [Feature Request] A flag to enable/disable CropTransparentPixels
- SetRoundMode should not be used at all ;) HOT 13
- Issues when building Mac Os 64 Bit HOT 10
- (out of topic) Simple Sudoku HOT 2
- FR: SVG Image element HOT 7
- Png file shows only part of the SVG file HOT 4
- BicubicResample() and tiny height HOT 4
- Delphi 12.1 compilation for IOS and Android error HOT 1
- Wrong colors HOT 2
- [dcc64 Error] Unit1.pas(216): E2003 Undeclared identifier: 'InterpolatePoints' when complie Vectorize demo HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from image32.