移动位图像素 [英] Moving bitmap pixels
问题描述
如果我想移动/移动位图的像素,我该怎么做?
procedure MovePixels(Bitmap: TBitmap; Horizontal, Vertical: Integer);
begin
{ move the Bitmap pixels to new position }
end;
示例:
通过调用 MovePixels(Image1.Picture.Bitmap,20,20)
例如将输出如下:
还可以指定/更改移动像素后剩下的画布的颜色。所以在这个例子中,灰色/棕色可以是蓝色等。
It would be useful to also specify / change the color of the canvas that is left showing after moving the pixels. So in this example that gray / brown color could be blue etc.
我注意到有 Bitmap.Canvas.Pixels
和 Bitmap.Canvas.MoveTo
属性,这是我需要做的吗?
I noticed there is Bitmap.Canvas.Pixels
and Bitmap.Canvas.MoveTo
properties, is this what I would need to do this?
我真的很不知道,我敢打赌它很简单..
I really don't know and I bet it is so simple..
推荐答案
你不能轻易移动像素,复制。
You can't easily move pixels, but you can make a copy.
var
Source, Dest: TRect;
....
Source := Rect(0, 0, Bitmap.Width, Bitmap.Height);
Dest := Source;
Dest.Offset(X, Y);
Bitmap.Canvas.CopyRect(Dest, Bitmap.Canvas, Source);
剩下的是填充你所选择的颜色,我相信你可以做的很容易,几次调用 FillRect
。
What remains is to fill in the space with the colour of your choice which I am sure you can do easily enough with a couple of calls to FillRect
.
但是,我认为不会尝试更简单这个就位。相反,我会创建一个新的位图。也许这样:
However, I think that it would be simpler not to attempt this in-place. Instead I would create a new bitmap. Perhaps like this:
function CreateMovedImage(Bitmap: TBitmap; X, Y: Integer; BackColor: TColor): TBitmap;
var
Source, Dest: TRect;
begin
Source := Rect(0, 0, Bitmap.Width, Bitmap.Height);
Dest := Source;
Dest.Offset(X, Y);
Result := TBitmap.Create;
Try
Result.SetSize(Bitmap.Width, Bitmap.Height);
Result.Canvas.Brush.Style := bsSolid;
Result.Canvas.Brush.Color := BackColor;
Result.Canvas.FillRect(Source);
Result.Canvas.CopyRect(Dest, Bitmap.Canvas, Source);
Except
Result.Free;
raise;
End;
end;
这篇关于移动位图像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!