我将jpg图片转换成一个每个象素点由RGB值组成的二维数组,代码如下:
type
tagRGBTRIPLE = packed record
rgbtBlue: Byte;
rgbtGreen: Byte;
rgbtRed: Byte;
end;
TRGBTriple = tagRGBTRIPLE;
TRGBArray = ARRAY[0..65534] OF TRGBTriple;
pRGBArray = ^TRGBArray;
procedure TForm1.rgbchange(ImgRGB:TRGBArray);
Var
Row:pRGBArray;
i,j,n:Integer;
begin
n:=0;
for i:=0 to image1.Picture.Height-1 do
begin
Row:=Image1.Picture.Bitmap.ScanLine[i];
for j:=0 to image1.Picture.Width-1 do
begin
ImgRGB[n].rgbtRed:=Row^[j].rgbtRed;
ImgRGB[n].rgbtGreen:=Row^[j].rgbtGreen;
ImgRGB[n].rgbtBlue:=Row^[j].rgbtBlue;
inc(n);
end;
end;
end;
但是运行后提示"scan line index out of range",不知为什么提示扫描越界?请高手帮助
type
tagRGBTRIPLE = packed record
rgbtBlue: Byte;
rgbtGreen: Byte;
rgbtRed: Byte;
end;
TRGBTriple = tagRGBTRIPLE;
TRGBArray = ARRAY[0..65534] OF TRGBTriple;
pRGBArray = ^TRGBArray;
procedure TForm1.rgbchange(ImgRGB:TRGBArray);
Var
Row:pRGBArray;
i,j,n:Integer;
begin
n:=0;
for i:=0 to image1.Picture.Height-1 do
begin
Row:=Image1.Picture.Bitmap.ScanLine[i];
for j:=0 to image1.Picture.Width-1 do
begin
ImgRGB[n].rgbtRed:=Row^[j].rgbtRed;
ImgRGB[n].rgbtGreen:=Row^[j].rgbtGreen;
ImgRGB[n].rgbtBlue:=Row^[j].rgbtBlue;
inc(n);
end;
end;
end;
但是运行后提示"scan line index out of range",不知为什么提示扫描越界?请高手帮助
3个字节对应一个象素,如果你不吧内存中的位图格式化成24位图,除非恰巧原来的就是24位图,否则当然会报错了.
应该是Image1.Piture.Bitmap.Height
image1.Picture.Width-1 应该是Image1.Picture.Bitmap.Width -1不要以为Bitmap和JPEG是一样的,如果你用JPEG,那么Bitmap没有用。
用Canvas,或者用TJPEGImage来转换成Bitmap再处理。
//将jpeg文件加载并付给Bitmap对象
procedure Tform1.GetRGBBitmap(Filename:string);
var
Jpegmap:TJPEGImage;
begin
Jpegmap:=TjpegImage.Create;
try
Jpegmap.LoadFromFile(FileName); //加载jpeg文件
image1.Picture.Bitmap.PixelFormat:=Pf24Bit;
image1.Picture.bitmap.Assign (JpegMap);
finally
Jpegmap.free;
end;
end;
//象素点转换成RGB值
procedure TForm1.rgbchange(ImgRGB:TRGBArray);
Var
Row:pRGBArray;
i,j,n:Integer;
begin
n:=0;
for i:=0 to image1.Picture.bitmap.Height-1 do
begin
Image1.Picture.BItmap.PixelFormat:=pf24bit;
Row:=Image1.Picture.bitmap.ScanLine[i];
for j:=0 to image1.Picture.bitmap.Width-1 do
begin
ImgRGB[n].rgbtRed:=Row^[j].rgbtRed;
ImgRGB[n].rgbtGreen:=Row^[j].rgbtGreen;
ImgRGB[n].rgbtBlue:=Row^[j].rgbtBlue;
inc(n);
end;
end;
end;
我的代码!看有什么错误?
tagRGBTRIPLE = packed record
rgbtBlue: Byte;
rgbtGreen: Byte;
rgbtRed: Byte;
end;
TRGBTriple = tagRGBTRIPLE;
TRGBArray = ARRAY[0..65534] OF TRGBTriple;
pRGBArray = ^TRGBArray;
在98下报错居然是说"该程序执行了非法操作,即将关闭"然后直接就推出了,根本无法运行下去,而且没有其他任何提示,让我不知何去何从!@
好像的确是内存读取时有问题!那怎么解决呢?多谢高手ehom多次帮助!不甚感激!在上面初始化时我加了一条new(row)来初始化指针变量,但是没有好转!难道要把new加到for循环里面?每次都声请一个?我觉得不会把!那用数组怎么表达呢?给点代码我看看吧!
这里已经将指针指向了一个地址,不用分配内存看看也知道,0..65534这个范围还是很小的,长乘宽的结果很容易超过范围