delphi - Why do I get an access violation when assigning a JPEG to an array of TJPEGImages? -
formulae: array [1..6] of tjpegimage;
i have array in want assign images can display them onto form. i've used similar code jpeg data-stream timage question access violation error message @ if statement
procedure tfrm_calc2.changedisplay(imgno: integer; newimage: boolean); var tempimg: tjpegimage; begin tempimg:= tjpegimage.create; tempimg.loadfromfile('c2f'+inttostr(imgno)+'.jpg'); img_formulae.picture.assign(tempimg); // assigning each picture element in array if first time. used save pictures later on if newimage = true formulae[imgno].assign(tempimg); tempimg.free; imgdisplayed:= imgno; lbl_formuladisplay.caption:= 'formula ' + inttostr(imgno); //user can see formula can seen end;
thanks.
did populate array allocated objects before calling assign
on them? not. try more instead:
procedure tfrm_calc2.changedisplay(imgno: integer); var tempimg: tjpegimage; begin tempimg := tjpegimage.create; try tempimg.loadfromfile('c2f'+inttostr(imgno)+'.jpg'); img_formulae.picture.assign(tempimg); if formulae[imgno] = nil begin formulae[imgno] := tempimg; tempimg := nil; end else formulae[imgno].assign(tempimg); tempimg.free; end; imgdisplayed := imgno; lbl_formuladisplay.caption := 'formula ' + inttostr(imgno); end;
alternatively:
procedure tfrm_calc2.changedisplay(imgno: integer); var tempimg: tjpegimage; begin tempimg := tjpegimage.create; try tempimg.loadfromfile('c2f'+inttostr(imgno)+'.jpg'); img_formulae.picture.assign(tempimg); freeandnil(formulae[imgno]); formulae[imgno] := tempimg; except tempimg.free; raise; end; imgdisplayed := imgno; lbl_formuladisplay.caption := 'formula ' + inttostr(imgno); end;
Comments
Post a Comment