【发布时间】:2015-03-17 11:26:58
【问题描述】:
我想做一个程序,可以把我的 TImage 像素 rgb 颜色写入备忘录,程序不知道分辨率。
那么如何将每个像素颜色恢复为字符串变量 (R,G,B)?
代码
var
Image_width,Image_height,x,y,i,i2:integer;
Colors:TColor;
begin
Image_width:=Image1.Width;
Image_height:=Image1.Height;
memo1.text:='$image_width=*'+IntToStr(Image_Width)+
'$$image_height='+IntToStr(Image_Height)+'*$';
x:=1;
y:=1;
for i := 1 to Image_width do begin
for i2 := 1 to Image_height do begin
Colors:=Image1.Canvas.Pixels[x,y];
memo1.Text:=memo1.Text+ColorToString(Colors);
y:=y+1;
end;
x:=x+1
end;
end
|已编辑|
procedure TForm1.Button2Click(Sender: TObject);
var
Image_width,Image_height,x,y:integer;
Colors:TColor;
s:string;
begin
Image_width:=Image1.Width;
Image_height:=Image1.Height;
memo1.text:='$image_width=*'+IntToStr(Image_Width)
+'*$ $image_height=*'+IntToStr(Image_Height)+'*$';
x:=0;
y:=0;
memo1.Lines.BeginUpdate;
for x := 0 to Image_width do begin
for y := 0 to Image_height do begin
Colors:=Image1.Canvas.Pixels[x,y];
memo1.Text:=Memo1.Text+ColorToString(Colors);
end;
memo1.Lines.EndUpdate;
end;
end;
它完成非常慢,我认为 19 秒与 640x480 图片。 在程序准备好并放入 memo1 后,我在 memo1 中看到了一些东西,但程序再次“没有响应”..
我尝试使用 s 变量,程序也这样做。
【问题讨论】:
-
使用 TBitmap 的 Scanline 属性读取像素。或来自
TImage.Canvas.Pixels[]属性。后者会慢一些。 -
您可以编写一个
record helper for TRGBTriple并实现一个ToString方法。剩下的就是迭代ScanLined 列并调用该方法, -
@Close voters:我不认为寻求调试帮助的问题(“为什么这段代码不起作用?”) 是正确的选择。这个原因应该用于寻求调试帮助的问题,而不是用于为我编写代码帖子。
-
这个问题太深奥了:有多个解,还有多个子解。你为什么不把这个问题分解成小块?比如:如何获取像素的颜色,如何将颜色转换和格式化为字符串,如何将字符串添加到备忘录中,如何格式化/概述备忘录中的字符串等等......
-
对您编辑的代码的评论。
for循环应该分别转到Image_width-1和Image_height-1。memo1.Lines.EndUpdate;放错了位置。它应该在for x := 0 to Image_width do begin的end;之后。如果您使用了代码格式化 (Ctrl-D),它会很容易被发现。正如我所说,反复连接memo1.Text非常慢。使用建议的其他解决方案。但是你真的应该解释一下为什么你甚至想把这种大小的图像转换成一个很长的字符串?
标签: windows delphi delphi-xe7