如果我有一个TBitmap并且我想从这个位图获得一个裁剪图像,我可以"就地"执行裁剪操作吗?例如,如果我有一个800x600的位图,我怎么能减少(裁剪)它,使其包含600x400图像在中心,即得到的TBitmap是600x400,并包含由(100,100)和(? 700,500)在原始图像?
我是否需要通过另一个位图,或者可以在原始位图内完成此操作?
我想将一个给定的缓冲区与Mono8格式的位图(单色8位)分配给一个位图.然后,我将结果位图分配给TImage组件以显示它.图片是结果显示的屏幕截图.
以下代码有效但似乎有点浪费:
procedure CopyToBitmapMono824(_Buffer: PByte; _Bmp: TBitmap);
var
y: Integer;
x: Integer;
ScanLine: PdzRgbTripleArray;
begin
for y := 0 to _Bmp.Height - 1 do begin
ScanLine := _Bmp.ScanLine[y];
for x := 0 to _Bmp.Width - 1 do begin
// monochrome: all 3 colors set to the same value
ScanLine[x].Red := _Buffer^;
ScanLine[x].Green := _Buffer^;
ScanLine[x].Blue := _Buffer^;
Inc(_Buffer);
end;
end;
end;
// [...]
fBmp.PixelFormat := pf24Bit;
FBmp.Monochrome := False;
CopyToBitmap(Buffer, fBmp);
Run Code Online (Sandbox Code Playgroud)
我宁愿使用pf8Bit格式的位图,我试过:
procedure CopyToBitmapMono8(_Buffer: PByte; _Bmp: TBitmap);
var
y: Integer; …Run Code Online (Sandbox Code Playgroud) 我在具有256x256位图的TImage上使用Stretched = True.这会按比例缩小1,2,4或8.正如预期的那样,位图上的文字越多,我就越离"1".我注意到,虽然Windows 7资源管理器呈现缩小版本的位图"更柔和",更令人愉悦.是否有可能以这种方式"模糊"TBitmap?
我有一个TBitmap,其中包含带alpha通道的半透明图像(在本例中我是从TPngImage获得的).
var
SourceBitmap: TBitmap;
PngImage: TPngImage;
begin
PngImage := TPngImage.Create();
SourceBitmap := TBitmap.Create();
try
PngImage.LoadFromFile('ImgSmallTransparent.png');
SourceBitmap.Assign(PngImage);
SourceBitmap.SaveToFile('TestIn.bmp');
imgSource.Picture.Assign(SourceBitmap);
finally
PngImage.Free();
SourceBitmap.Free();
end;
Run Code Online (Sandbox Code Playgroud)
当我将此TBitmap保存到TestIn.bmp文件并使用任何图像查看器打开它时,我可以看到透明度.但是当我将它分配给TImage时,透明像素显示为黑色(TImage有Transparent = True).
如何在TImage上正确显示具有透明度的TBitmap?
我在VCL表单应用程序中有这个代码:
implementation
{$R *.dfm}
var
MyBitmap: TBitmap;
procedure TFormMain.FormCreate(Sender: TObject);
begin
MyBitmap := TBitmap.Create;
end;
procedure TFormMain.FormDestroy(Sender: TObject);
begin
if Assigned(MyBitmap) then
MyBitmap.Free;
end;
procedure TFormMain.Button1Click(Sender: TObject);
begin
if Assigned(MyBitmap) then
MyBitmap.Free;
end;
Run Code Online (Sandbox Code Playgroud)
当我第二次单击按钮时,我MyBitmap.Free;在按钮的单击处理程序中获得了Access违规.但是MyBitmap在点击第一个按钮后不应再分配.那么为什么条件if Assigned(MyBitmap) then在第二次按键单击时不起作用,显然它在第一次按键点击时有效?
Delphi 10.1柏林更新2
我想将像素从 BMP1 复制到 BMP2 但复制的图像是乱码。为什么?
注:输入图像为pf8bit;
TYPE
TPixArray = array[0..4095] of Byte;
PPixArray = ^TPixArray;
procedure Tfrm1.CopyImage;
VAR
BMP1, BMP2: TBitmap;
y, x: Integer;
LineI, LineO: PPixArray;
begin
BMP1:= TBitmap.Create;
BMP2:= TBitmap.Create;
TRY
BMP1.LoadFromFile('test.bmp');
BMP2.SetSize(BMP1.Width, BMP1.Height);
BMP2.PixelFormat:= BMP1.PixelFormat;
for y:= 0 to BMP1.Height -1 DO
begin
LineI := BMP1.ScanLine[y];
LineO := BMP2.ScanLine[y];
for x := 0 to BMP1.Width -1 DO
LineO[x]:= LineI[x];
end;
//BMP2.SaveToFile('out.bmp');
imgOut.Picture.Assign(BMP2); //TImage
FINALLY
FreeAndNil(BMP2);
FreeAndNil(BMP1);
END;
end;
Run Code Online (Sandbox Code Playgroud)
对于保存的图像,图形编辑器会显示“像素深度/颜色:索引,256 色板”。
当像素包含32位TBitmap的特定颜色时,我需要更改alpha分量的值,我知道要访问位图数据的ScanLine属性,但我无法弄清楚如何更改每个alpha分量像素.