I'm loading an image from disk and want to copy (part of) it to a second TImage:
Image1.Picture.LoadFromFile(S);
with Image1.Picture.Bitmap do
Image2.Canvas.CopyRect(Image2.Canvas.ClipRect, Canvas, Canvas.ClipRect);
Image2 just shows a white rectangle, and Image1 doesn't show the file from disk. If I remove the second statement Image1 does show the image. (Strangest thing: if I only comment out the CopyRect statement and leave the "with" line (empty statement) Image1 doesn't show either!)
How do I use CopyRect to copy part of an image?
edit
When I split the two statements into two separate actions (buttons) the following happens:
Image loads and shows in Image1
Image1 disappears(!), and Image2 shows a white rectangle.
BTW, I'm using Delphi 2009.
TCanvas.CopyRect copies the rectangle by using StretchBlt. StretchBlt requires a bitmap. If you're loading any other graphic type to your image then its Picture.Bitmap is empty. In fact the bitmap gets created just when you refer to it: with Image1.Picture.Bitmap do.
You can use a temporary bitmap for the cause:
var
Bmp: TBitmap;
begin
Image1.Picture.LoadFromFile(S);
Bmp := TBitmap.Create;
try
Bmp.Assign(Image1.Picture.Graphic);
with Bmp do
Image2.Canvas.CopyRect(Image2.Canvas.ClipRect, Canvas, Canvas.ClipRect);
finally
Bmp.Free;
..
Related
I am trying to create a custom icon button with a transparent bitmap image, below the code.
The icon is stored in an ImageList connected to an ActionList.
bitmap := TBitmap.Create;
BmpObj := TMemoryStream.Create;
try
ImageList.GetBitmap(ActionList.Actions[i].ImageIndex, bitmap);
bitmap.Transparent := TRUE;
bitmap.TransparentColor := clWhite;
bitmap.Canvas.Brush.Color := clWhite;
bitmap.SaveToStream(BmpObj);
finally
BmpObj.Free;
bitmap.Free;
end;
I don't know what I am missing.. Anyone has an idea about this problem?
Without better knowledge about the components I asked about, I show the basic way of having transparent image on many Windows controls.
Take a TImageList and fill it with the images you want to use. The lower left pixel determines the transparent color. In my example black numbers are placed on white background.
On the button, set property Images to your ImageList and ImageIndex to a valid image number (0 .. ). The image will appear on the button, with the white colored areas transparent.
In the image below, I include a TImage with the bitmap so you can see the actual colors.
Note no code required.
In Delphi 10.4, I have loaded a PNG image (32BPP with Alpha Channel Transparency) at DESIGN-TIME in the Picture property of a TImage. Here is how it looks at run-time:
Here is a copy of the TImage component (which can be pasted at design-time in any Delphi VCL Application project):
object Image1: TImage
Left = 46
Top = 200
Width = 32
Height = 32
AutoSize = True
Center = True
Picture.Data = {
0954506E67496D61676589504E470D0A1A0A0000000D49484452000000200000
00200806000000737A7AF40000000473424954080808087C086488000006F749
44415478DAB557694C545714BE33C3BE8A0CEBB096224EA94241B16AACA68520
6DA1D1D485B00A1421DA0A36A9B64D9AFE68AA69A340451B4A41405903D6A5D4
0AB4285B59442236884C054640C080EC8475A6DF19670803B40C466F72F3DEBB
E7DC73BE77EED92EE78083195BB74A87310E872D1E52261E9D6409CD8FD9CB1A
9CD3EEF66C9B99E11800682FD62F9DBA3F34AE155CD9F212016C788500CCD2FB
12069002002FB8F2C1336658C9CCCC8C595B5B333E9FCF74747494D827262658
5F5F1FEBECEC64DDDDDD4C2291BC180011756DCCDDDD9DB9B8B8700C0D0DB781
F23EE69B988E98AB3149D300A608F32FCCC2D1D1D18AC6C646697D7D3D1B1F1F
7F7E001D1C4DDE9FF61BB8BABABA2158390E2B38AA625AA9544AE77672727232
A3ACAC4C72E7CE9DE703D06624E0D55B39D740F146B960311E7C7CEBE2FD1814
24C0D4126D6DED20ACA5606D06F4C778B791F3D7E211D4D2D2222A2C2C645353
532B03D0C2B7E5DDB35C2B81A04758F91CCF6A0827538FE15D909E9E3E3A3838
C8A2A3A3D53435351F81660E3C6E780AC1F32D9EB6E01BC6DC03BF28CECDCD55
02B12C0091B135EFAEA5300B020EE25C47F4F5F58F40681CBE33E16C41999999
32566F6F6FE6EAEA7A06B443A07D353636F60D9C541FDF4998FBB13685E9FBF0
E1C3E24B972E91655403D0A9BB9A57C45FC36EDDBAC56C6C6CC811AF41E07B10
10505C5C9CAD385B3B3B3BB66FDF3E3FD02E83562A168BDFA9ADAD65BEBEBE4C
4B4BEB14D663C91260DD585A5A2A229ACA51A008C3D0D0500AC32710C687996D
9293933B07060664341E8FC76263634DF0ECC1E7087CC3283E3E5E6A6E6E4EC0
084496DC12B538822D29292992E1E1E19501387AF4A8B58686861842FA6062D3
C4C44425F6C0C040261008C450640D808EA03FA410B4B5B5657BF7EED5E772B9
F7C839B13F0CE199565252B23C00B1BA1E2F47DD8C6133B3B7B77F1B024A20A0
1C7FB89D12CEFC41494A4F4FAF083C9EE0F1E9E8E8B8A170382B2B2B0627F507
2D934214FB8500285D16C03FE48482D7964F692B1CB0D0F6828282721500D800
80F085038015BEABA9A939BE2C80475AAB78D70C6CC98918D2F0699830069B63
E17C090B938A9A9A1A333636FE043CF1E0F901611B035F99A3D3319A98987882
5E047A596B6BEB0E959D70FDFAF5CCC7C7E7276C8EC0E688FCFCFC54C4B412BB
A9A929454A1814FD0C9E94F2F2F28FAAAAAAE6E854CC6262622CE00B5DA077F7
F4F408540680F8673B77EE3C0F2121D81C92969676A1B7B757891DF5821D3E7C
9852723A78322A2A2A422B2B2B957800500DA1494969020074560A200DC283B1
3918002EFE078040F06480E70200842C04101212C2B3B0B098067D1200B4570A
2019C2C3B1393C2F2FEF7C5B5B9B123BF508E1E1E107E445291500221602A023
803FD111407F8FE5F2D550439F97CB336106060674C671107E849C1085256161
9D4792A266E563F02490133E7DFA344691296950B644CA563861797B7BFB7625
00280F7F4F334EA81A93A671197B5D660154C326544395E34BC50100DFDFBE7D
FBD81C002C34F4E819BF7BD7C2698F437F4781C340E7AF5C297BA3896FC7BB6F
E9F45212D1D5AB57CB39A79E35A565542A2BAC5D867B579937A949669B843DA2
68C7818E7CB196D15BBFE959CBCC871856A4E2B2E9E9E91D30B192503A263426
73A9B8BFBFFFC6CCCC8C8C6664644447A448C522EC5F2B4BC5B1424BE66AA4CB
2A9E0CB1660B27B679F3E60388E314EEECCCEF82C1EE0FC71EDC1BFFACAC5126
04C54800211D10D03F32326272EEDC39250001010194F3A929B1C21FBE7AF6EC
D9564A44541151A8F4F1138DF206251CFDE2F9EBD7AF2B3B1E75B99191915C24
8A2A307A80311B9D6E007531F0581616164656E801CD140A6C9392923A868686
647B29CBC1C3F9EAEAEA149BC3C892ABE3E2E2A4F4E7FEFEFE0C8DCC5C399E9D
9DDD929A9A2A210B2EF27C373737E6E9E9B906CCB59806D81007109F5EB97285
098542CA8897B1EE47B9A0A8A8E8624343836C1FB5EA50F40180FC02DA1F6848
BC6EDEBCC976EFDE4DCAE71A12CC8DD5D5D52234AAB27D8B0050BADCB56B1773
7474F4C62729D3244BE08FA3807804B17E086B67B0968772BB3F2B2B4BB6CFCB
CB8BC0FF08DA41D0BE84654EA03493D9955A3294F0E29C9C9CB93BC352F731AA
DBD44030642C6F6CCEC3D49777C35F605266A1223089352B34A583D494464545
6920C15083608CE982B98E2D684AF103C5D9D9D96C7E815A12000D4A2A7E7E7E
CCC1C1C1099F19F3DAF276522207F5359CF1243C5D82B38EC45A22FD29E8DDA4
58CE5F8BBF0DEAEAEA12D131CE57FEBF0014C7E1E1E1C1B66EDDCA8573ADF462
42ADFB0980CBA8ABAB93504A86F32DD6A18A308AEF4D9B36316767670E2CB30D
20E65FCD8C481F9B773583F24228AE686E6E96C2E1D8C27CB162008A410D07B5
DF4B5D4EA9CF479F27BB9CC23919F2FCA25BD052E35F02ED3A5D68222C670000
000049454E44AE426082}
Proportional = True
end
Now, at run-time, I try to save this image as a BITMAP image file (.bmp):
procedure TForm1.btnSaveTImageToBitmapClick(Sender: TObject);
begin
Image1.Picture.Bitmap.SaveToFile('Y:\Downloads\test.bmp');
end;
But when I execute this code at run-time, something very strange happens: The TImage component VANISHES (it becomes apparently INVISIBLE!) and the created .bmp file has a ZERO size (0 bytes).
However, when instead of the above code I execute this code:
procedure TForm1.btnSaveTImageToBitmapClick(Sender: TObject);
begin
Image1.Picture.SaveToFile('Y:\Downloads\test.bmp');
end;
...then the resulting file is a .PNG file with a WRONG .BMP file extension!
So how can I save this image as a valid BITMAP (.BMP) file?
EDIT: The answer by #Andreas Rejbrand and #Remy Lebeau has provided extensive explanations and ways to solve the problem.
You must create a TBitmap and assign the Picture.Graphic to it:
procedure TForm3.btnSaveClick(Sender: TObject);
var
bm: TBitmap;
begin
bm := TBitmap.Create;
try
bm.Assign(Image1.Picture.Graphic);
bm.SaveToFile('C:\Users\Andreas Rejbrand\Desktop\bitmap.bmp');
finally
bm.Free;
end;
end;
Your approach:
Image1.Picture.Bitmap.SaveToFile('Y:\Downloads\test.bmp');
doesn't work because the Picture doesn't contain a bitmap. From the documentation of the TPicture.Bitmap property:
Use Bitmap to reference the picture object when it contains a bitmap. If Bitmap is referenced when the picture contains a Metafile or Icon graphic, the graphic won't be converted (Types of Graphic Objects). Instead, the original contents of the picture are discarded and Bitmap returns a new, blank bitmap.
This approach:
Image1.Picture.SaveToFile('Y:\Downloads\test.bmp');
does exactly what it is supposed to do. It saves the Picture.Graphic as-is, which happens to be a PNG image in your case, to the file you specified. You chose a strange extension for a PNG image, but that's not something the VCL tries to correct.
Handling PNG transparency
If the PNG file has an alpha channel, bm.Assign will very faithfully create a bitmap image with an alpha channel. That's great, in one sense, because then you don't lose any information: the BMP you get contains all graphic data from the PNG, including the entire alpha channel. So you can, in principle, render this BMP onto any background and will look as nice as the PNG would look.
But in another way, it's not that great, after all. Because almost no image viewers or editors support BMPs with alpha channels. These applications will probably ignore the alpha channel, effectively making every pixel fully opaque. That will look horrible.
So if you want to create a normal BMP, without alpha channel, you need to "render" the PNG, using its alpha channel, onto a background of your choice, and then save the non-transparent result. The BMP you get will not contain the alpha channel of the original PNG, so you lose information. The BMP will not be transparent. For instance, if you draw it onto a white background, you will always have this white rectangle behind your icon/subject, even if you place it on a red area of the screen.
procedure TForm3.Image1Click(Sender: TObject);
var
bm: TBitmap;
begin
bm := TBitmap.Create;
try
bm.SetSize(Image1.Picture.Width, Image1.Picture.Height);
bm.Canvas.Brush.Color := clSkyBlue; {*}
bm.Canvas.FillRect(Rect(0, 0, bm.Width, bm.Height)); {*}
bm.Canvas.Draw(0, 0, Image1.Picture.Graphic);
bm.SaveToFile('C:\Users\Andreas Rejbrand\Desktop\bitmap2_1.bmp');
finally
bm.Free;
end;
end;
The lines with asterisks (*) can be omitted if you are happy with the default background color, which is white.
I want to take a screenshot of my page and put the result into a bitmap, Because there is a scrollbar on the page, i have to take several screenshots, and i want to merge those bitmaps.
if have used this code to make a screenshot and save it: Take a screenshot of a particular area in Delphi 7
i used the code to merge them from this page http://www.delphigroups.info/2/8/309463.html
if i copied it directly it would result in the first image being used, and i white rectangle for the second. so i tried to change it a little bit, and now i'm getting both images in one file.
This is the code i use to concatenate the bitmaps:
function ConcatenateBitmaps(const MainBitmap: TBitmap; const BitmapToAdd:
TBitmap): TBitmap;
begin
Result := MainBitmap;
If BitmapToAdd.Width > MainBitmap.Width then
Result.Width := BitmapToAdd.Width;
Result.Height := MainBitmap.Height + MainBitmap.Height;
Result.Canvas.CopyRect(
Rect(0,MainBitmap.Height,BitmapToAdd.Width,BitmapToAdd.Height),
BitmapToAdd.Canvas,
Rect(0,0,BitmapToAdd.Width,BitmapToAdd.Height)
);
end;
The problem is that te second image is being flipped, vertical and horizontal;
What am i doing wrong here?
EDIT:
An example of the result, the first image is good, the second image is flipped:
as i see now, my description was wrong, it's horizontaly mirrored, and verticaly flipped
Cause and quickfix:
The problem is in this part:
Rect(0,MainBitmap.Height,BitmapToAdd.Width,BitmapToAdd.Height)
You make a rectangle of which the top is the total height of the resulting image, and the bottom is the height of the bitmap to add. So this rectangle is basically inverted (its bottom is above its top).
And it's likely deformed as well, since the height of this rectangle is not the height of the bitmap to add.
The quickfix would be:
Rect(0,Result.Height- BitmapToAdd.Height,BitmapToAdd.Width,Result.Height)
Other issues and confusion:
But I think the cause of your confusion is because you think that Result and MainBitmap are two different bitmaps, while actually they are both references to the same bitmap. The assignment you do in the beginning just copies the reference, not the actual TBitmap object.
In addition, you mix up 'height' and 'bottom'. TRect expects you to set top and bottom coordinates, not top and height. This, together with the previous issue, causes not only that the bitmap is upside down, but also that it will be stretched, and partially covering the previous images. The more images you add, the more clear that effect will be.
Personally I think it's way more efficient to modify the existing bitmap in this scenario, mainly because you would otherwise have to clean up your old bitmap all the time, plus that you have a function that magically creates bitmaps. You get the question of ownership of the bitmap objects, and with that, the risk of memory leaks, which is not good, especially when dealing with large bitmaps.
My suggested version:
So, I would just make it a procedure, where the first bitmap is modified by adding the second bitmap to it.
In the version below, I also used Canvas.ClipRect, which is for a bitmap essentially the bounding rectangle of the bitmap. And then I used OffsetRect to 'move' this rectangle(increasing its top Y and bottom Y).
By doing this in a separate variable, you can have a relatively clean version compared to the quick fix I presented above, because you can use the dimensions of MainBitmap before actually modifying it.
procedure AppendBitmap(const MainBitmap: TBitmap; const BitmapToAdd:
TBitmap);
var
TargetRect: TRect;
begin
// Widen the main bitmap if needed
if BitmapToAdd.Width > MainBitmap.Width then
MainBitmap.Width := BitmapToAdd.Width;
// Set TargetRect to the right size
TargetRect := BitmapToAdd.Canvas.ClipRect;
// And then to the right position
OffsetRect(TargetRect, 0, MainBitmap.Height);
// Make room for the bitmap to add
MainBitmap.Height := MainBitmap.Height + BitmapToAdd.Height;
// Draw it in the created space
MainBitmap.Canvas.CopyRect(
TargetRect,
BitmapToAdd.Canvas,
BitmapToAdd.Canvas.ClipRect
);
end;
And if you like, you can make a wrapper function with the signature of the original, that creates a copy of the main image and returns that. Note though, that MainBitmap and the result of this function are no longer the same bitmap, and you have to make sure to properly free both of them when you're done.
function ConcatenateBitmaps(const MainBitmap: TBitmap; const BitmapToAdd:
TBitmap): TBitmap;
begin
Result := TBitmap.Create;
Result.Assign(MainBitmap);
AppendBitmap(Result, BitmapToAdd);
end;
PS: I like questions like this from which I learn something. I never realized you could flip an image by flipping the rect passed to CopyRect. :D
I Put a TImageEnView on my form and put a Label on the TImageEnView.
I want to save this parent and child as one Png or Jpg on my hard drive.
I write this code :
CharLbl.Font.Size := I;
CharLbl.Top:=22;
ImageEnIO1.SaveToFile('D:\output2.png'); // Save in thread 2
ImageEnIO1.WaitThreads(false);
ShowMessage(inttoStr(I));
But the output is only background with out Label. How can I save the label as well?
Try the following:
var
paintbmp:tbitmap;
begin
paintbmp:=tbitmap.Create;
try
paintbmp.Width:=ImageEnIO1.Width;
paintbmp.Height:=ImageEnIO1.Height;
paintbmp.Canvas.Draw(0,0,ImageEnIO1.Picture.Graphic);
paintbmp.Canvas.CopyRect(rect(0,0,ImageEnIO1.Width,ImageEnIO1.Height)
,CharLbl.Canvas
,rect(0,0,ImageEnIO1.Width,ImageEnIO1.Height));
paintbmp.SaveToFile('D:\output2.png');
finally
paintbmp.Free;
end;
end;
Just be careful in order for this to give you what you want the size of the label is to be the same as the image's and the top and left is the same as the image's.
Note: I would still recommend you to see the link I gave you in comments, because it will aid you to learn a valuable tool that would even enable you to write your own component in the future.
Note 2: The output image is not a valid PNG it is still a Bitmap so you still need to convert it.(thanks to Kobik)
I have some data stored inside my TImage Component. I want to save the image as an bitmap. Executing this code the TImage content gets white and only a bmp file with 0 bytes has been created on the harddisk. What is wrong with my code ?
MainStatusbar.SimpleText := 'save the image .... ';
if SaveDialog.Execute then
begin
Image1.picture.Bitmap.SaveToFile(SaveDialog.filename);
end;
MainStatusbar.SimpleText := 'done ';
TPicture is a container for multiple different types of images. If the current image is not a TBitmap, then Picture.Bitmap will not contain your image. When you refer to Picture.Bitmap your original image will be destroyed, and an empty TBitmap created. So, the obvious explanation in that Picture.Bitmap is empty when SaveToFile() is called.
You should be saving your image by calling SaveToFile on the Picture object instead:
Image1.Picture.SaveToFile(...);
If the original image is not a bitmap then calling Picture.Bitmap will erase the content and create an empty bitmap. Depending on the original picture format an automatic conversion (e.g. from an icon) may be possible, but has to be done outside of TImage.
If the TImage.Picture.Graphic property does not currently hold a TBitmap then accessing the TImage.Picture.Bitmap property will free the current Graphic and replace it with a blank TBitmap. This is documented behavior.
Since you want to save a bitmap, check if the current Graphic is already a TBitmap. If so, save it as-is. Otherwise create a temp TBitmap, assign the current Graphic to it, and then save it.
MainStatusbar.SimpleText := 'save the image .... ';
if SaveDialog.Execute then
begin
if Image1.Picture.Graphic is TBitmap then
Image1.Picture.Bitmap.SaveToFile(SaveDialog.FileName)
else
begin
Tmp := TBitmap.Create;
try
Tmp.Assign(Image1.Picture.Graphic);
Tmp.SaveToFile(SaveDialog.FileName);
finally
Tmp.Free;
end;
end;
end;
MainStatusbar.SimpleText := 'done ';