AV in the depths of Img32 modules.
A 2D graphics library written in Delphi Pascal
Brought to you by:
angusj
Hello, Is this a bug or is there something wrong in my code?
procedure TFMain.CreatePriceHistoryChart;
type
TPHRec = record
prices: array of integer;
end;
var
ph: TPHRec;
img: TImage32;
path: TPathD;
FontReader: TFontReader;
FontCache: TFontCache;
begin
SetLength(ph.prices, 24);
ph.prices := [511, 569, 567, 546, 518, 504, 469, 464, 475, 416, 413, 470, 515, 541, 589, 587, 598, 619, 618, 622, 622, 622, 634, 654, 654];
FontReader := nil;
FontCache := nil;
img := TImage32.Create(600, 200); // canvas for chart
try
FontReader := TFontReader.Create;
FontReader.Load('Courier');
FontCache := TFontCache.Create(FontReader, 12);
img.Clear(clWhite32);
img.BeginUpdate;
try
var maxY := MaxIntValue(ph.prices);
var scaleY := 160 / (maxY - MinIntValue(ph.prices));
var scaleX := 600 / (Length(ph.prices));
SetLength(path, Length(ph.prices));
for var i := 0 to Length(ph.prices) do begin
path[i].X := i * scaleX;
path[i].Y := (maxY - ph.prices[i] + 20) * scaleY;
DrawPoint(img, PointD(i * scaleX, (maxY - ph.prices[i] + 20) * scaleY) , 4, clLime32); // point - green circle
// Img32.Text.DrawText(img, path[i].X, path[i].Y, ph.prices[i].ToString, FontCache);
// Img32.Text.DrawVerticalText(img, path[i].X, path[i].Y, 2.0, ph.prices[i].ToString, FontCache);
end;
DrawLine(img, path, 3, clOrange32, esButt);
finally
img.EndUpdate;
end;
img.SaveToFile('chart.png');
finally
FontCache.Free;
FontReader.Free;
img.Free;
end;
end;
Call Stack in the attachment.
Also, if you uncomment the text drawing code, it is not drawn.
Best regards
Delphi 10.4.2 Pro + Win 10 x64 + latest Img32 snapshot
Ooops... my bad, sorry! :( I forgot about the -1 in the code:
for var i := 0 to Length(ph.prices) - 1 do begin
Night is the mother of counsels :)
But still there is one remark. If you draw the text in clBlack color, then it is not visible.
See attachment.
Hi Vad.
This is your problem ....
FontReader.Load('Courier');
This isn't a TrueType font and Image32 only reads TrueType fonts.
Change the line to ...
FontReader.Load('Courier New');
and your chart will look just fine.
Last edit: Angus Johnson 2022-03-18