Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 03-04-2011, 10:46 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 217
Arrow Why would this produce an access violation

I have the following function that produces a thumbnail from the passed filename (fj), however its producing an access violation with a reference of 0000000000, can anybody shed some light on this for me?

Code:
function thumbfilenameI(fj:string):TBitmap;
var
  w,c:integer;
  masterBMP :TBitmap;
  filejpg,thumbjpg   :TJpegImage;
  x,y,h:integer;
BEGIN
  filejpg:=TJpegImage.Create;
  filejpg.LoadFromFile(fj);
  masterBMP:=TBitmap.create;
  masterBMP.Canvas.Pen.Color   :=clblack;
  masterBMP.Canvas.Brush.Color :=clwhite;
  masterBMP.width  :=150;
  masterBMP.height :=170;
  masterBMP.Canvas.Rectangle(0,0,150,170);
  masterBMP.Canvas.MoveTo(0,150);
  masterBMP.Canvas.LineTo(150,150);
  // determin aspect ratio and scale to fit
  if filejpg.Width > filejpg.Height then begin
  w:=148;
  h:=round(w*filejpg.Height / filejpg.Width);
  end else begin
  h:=148;
  w:=round(h*filejpg.Width / filejpg.Height);
  end;
  x:=(148 - w) div 2;
  y:=(148 - h) div 2;
  // draw the jpeg image into the bitmap
  masterBMP.Canvas.Rectangle(Rect(x+4,y+4,(x+w)-2,(y+h)-2));
  masterBMP.Canvas.StretchDraw(classes.Rect(x+5,y+5,(x+w)-3,(y+h)-3),filejpg);
  w:=masterBMP.Canvas.TextWidth(extractfilename(fj));
  c:=(150 div 2) - (w div 2);
  masterBMP.Canvas.TextOut(c,153,extractfilename(fj));
  RESULT:=masterBMP;
  masterBMP.Free;
  filejpg.Free;
END;
Reply With Quote
  #2  
Old 03-04-2011, 11:12 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,335
Default

You free masterbmp, but this is already assigned to Result...
Replace all masterBMP references with Result and remove the .Free()

Code:
function thumbfilenameI(fj:string):TBitmap;
var
  w,c:integer;
  filejpg   :TJpegImage;
  x,y,h:integer;
BEGIN
  filejpg:=TJpegImage.Create;
  filejpg.LoadFromFile(fj);
  Result:=TBitmap.create;
  Result.Canvas.Pen.Color   :=clblack;
  Result.Canvas.Brush.Color :=clwhite;
  Result.width  :=150;
  Result.height :=170;
  Result.Canvas.Rectangle(0,0,150,170);
  Result.Canvas.MoveTo(0,150);
  Result.Canvas.LineTo(150,150);
  // determin aspect ratio and scale to fit
  if filejpg.Width > filejpg.Height then begin
  w:=148;
  h:=round(w*filejpg.Height / filejpg.Width);
  end else begin
  h:=148;
  w:=round(h*filejpg.Width / filejpg.Height);
  end;
  x:=(148 - w) div 2;
  y:=(148 - h) div 2;
  // draw the jpeg image into the bitmap
  Result.Canvas.Rectangle(Rect(x+4,y+4,(x+w)-2,(y+h)-2));
  Result.Canvas.StretchDraw(classes.Rect(x+5,y+5,(x+w)-3,(y+h)-3),filejpg);
  w:=Result.Canvas.TextWidth(extractfilename(fj));
  c:=(150 div 2) - (w div 2);
  Result.Canvas.TextOut(c,153,extractfilename(fj));
  filejpg.Free;
END;
Reply With Quote
  #3  
Old 03-04-2011, 11:35 AM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 217
Thumbs up Thanks Norrit

Thanks for that Norrit, it now works a treat
Reply With Quote
Reply

Tags
error, thumbnail

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 06:11 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.