我按照以下答案裁剪图像: how do I crop a bitmap “in place”?
在delphi 7中,我有一个TImage Image_center。
Image := TPngObject.Create;
try
Image.LoadFromStream(Stream);
Image_center.Picture.Graphic := Image;
Image_center.width := Image.width;
Image_center.height := Image.height;
Image_center.Left := ( form1.clientWidth div 2 ) - (Image_center.width div 2);
CropBitmap(Image_center.Picture.Bitmap, 1, 45, Image.width, Image.height-45);
finally
Image.Free;
end;
但是结果是TImage包含白色位图。如果我跳过/注释掉CropBitmap函数,那么我可以看到图像。因此,加载没有问题。为什么我看到白色区域而不是图像?
procedure CropBitmap(InBitmap : TBitmap; X, Y, W, H :Integer);
begin
BitBlt(InBitmap.Canvas.Handle, 0, 0, W, H, InBitmap.Canvas.Handle, X, Y, SRCCOPY);
InBitmap.Width :=W;
InBitmap.Height:=H;
end;
Delphi 7位图方法:
In TBitmap
~TBitmap
Assign
Create
Destroy
Dormant
FreeImage
HandleAllocated
LoadFromClipboardFormat
LoadFromResourceID
LoadFromResourceName
LoadFromStream
Mask
ReleaseHandle
ReleaseMaskHandle
ReleasePalette
SaveToClipboardFormat
SaveToStream
TBitmap
Derived from TGraphic
LoadFromFile
SaveToFile
Derived from TInterfacedPersistent
AfterConstruction
QueryInterface
Derived from TPersistent
GetNamePath
Derived from TObject
BeforeDestruction
ClassInfo
ClassName
ClassNameIs
ClassParent
ClassType
CleanupInstance
DefaultHandler
Dispatch
FieldAddress
Free
FreeInstance
GetInterface
GetInterfaceEntry
GetInterfaceTable
InheritsFrom
InitInstance
InstanceSize
MethodAddress
MethodName
NewInstance
SafeCallException
答案 0 :(得分:3)
您(错误地)假设Image_center.Picture.Bitmap
将为您提供图片作为位图。仅当它是位图时才如此,否则它将用空位图覆盖图形。
相反,您可以在位图上绘制PNG图像,例如:
with Image_center.Picture.Bitmap do
begin
Width := Image.Width;
Height := Image.Height;
Canvas.Draw(0,0, Image);
end;
之后,您可以使用Image_center.Picture.Graphic
或Image_center.Picture.Bitmap
中的位图来执行所需的任何操作。
请记住,这样一来,您将失去PNG图像中的任何透明度,并且即使您读过How to get bitmap transparancy without the need to paint first?也可以将其取回,这并非易事。