Forum > Graphics

Trapping non-available resource

<< < (2/2)

Handoko:
Try-except and try-finally blocks can be confusing. I even saw experienced programmers use them not fully correct, at least that was what I think.

The simple rule is:
Try-finally should be used if you create object in memory, you put the .Free command in the finally section.

Alternatively, you don't need to use try-finally block if you're sure or already make sure nothing bad may happen (like: all data already validated before running a command).

Try-finally is to make sure the object will be free even exception happens. Without try-finally, the .Free command may be skipped because the lines below the the exception happens, will not be executed.

You can consider them as: try-except is a panic exit and try-finally will make sure it always executed even the panic happens.


--- Quote from: J-G on June 10, 2022, 08:30:22 pm ---
--- Quote from: Handoko on June 10, 2022, 07:19:41 pm ---Don't give up easily, friend.
--- End quote ---
:D :D  That I very seldom do!  - I'm known for my stubborn determination to get my way  ::)

--- End quote ---

That is a requirement for programmer.  :)

Josh:
or this may suit

hopefully The name variable is the name of the resource, in the resource file; hense it does not have .extension, and should be ALL CAPS, curios as to why you taking off the last 4 chars when displaying message?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Rotate(Pic:TImage;Name:string;Angle:single;X,Y:word;T:byte);Var  tmp, dest : TBGRABitmap;begin  // will need lcltype in the uses clause to get the RT_RCDATA, RT_BITMAP etc,  IF FindResource(hInstance, PChar(Name), RT_RCDATA)<>0 THEN  // resource exists  begin    tmp:=tbGRABitmap.Create;    tmp.LoadFromResource(Name);    dest := TBGRAbitmap.create;    BGRAReplace(dest, tmp.FilterRotate(PointF(X,Y),angle,true));    dest.ReplaceTransparent(clRed);    Pic.Picture.Clear;    Pic.Picture.Bitmap.SetSize(tmp.Width,tmp.Height);      Pic.Picture.Bitmap.TransparentColor:=clRed;      Pic.Transparent:=true;     dest.Draw(Pic.Picture.Bitmap.Canvas,0,0);    tmp.Free;    dest.free;  end  else  begin    Form1.MissingImage.Caption := Copy(Name,1,Length(name)-4)+' Image not available';      Form1.MissingImage.Show;  end;end;

J-G:

--- Quote from: Josh on June 10, 2022, 11:07:52 pm ---or this may suit

hopefully The name variable is the name of the resource, in the resource file; hense it does not have .extension, and should be ALL CAPS, curios as to why you taking off the last 4 chars when displaying message?

  IF FindResource(hInstance, PChar(Name), RT_RCDATA)<>0 THEN  // resource exists

--- End quote ---

That suits fine Josh - thanks very much  -  the 'name' is the full filename and therefore does include the extension (.png) and of course that is the reason for trimming the last four characters - I only need to report the number of teeth on the gear, not the name of the file

Naturally my first attempt failed since I hadn't seen your comments (was that an [Edit]? ) about the extension but as soon as I created a 'ShortName' - using  ' ShortName := Copy(Name,1,Length(Name)-4); ' and used that in the 'FindResorce' call, all was hunky-dory.  The fact that the filename consists of a number and an uppercase 'T' means that I wasn't afflicted by the 'All Caps' issue.

So  -  that is the code I'll be using for now, though if I can solve the issues with @Winni's suggestion in the BGRA... forum that would be an improvement.

Navigation

[0] Message Index

[*] Previous page

Go to full version