Request for beginners guidance

Hi. I am looking for language reference guides (or books) that will explain to me how and why to solve one of my “hello world” problems.

I was trying to load a bitmap into a picturebox.

method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
var
tb: System.drawing.Image;
begin

end;

My specific lack of understanding here are

  • How do i create the image as in delphi’s tb:=TBitmap.create;? When is it created? Would i need to free it afterwards?
  • How would I load an image from disk? tbitmap.loadfromfile;
  • Assign it to the paintbox?

As you can see I am drowning in my ignorance here, so any helpful links to documents that will explain what i need to know for this would be very appreciated.

Can’t help you on the Paintbox/Picturebox aspect other than to suggest that Image property (of the PictureBox class) would appear to be of some help here ? :slight_smile:

The method for loading an image from a file is simply the Image.FromFile() method (on .NET).

I haven’t tested this (or ever actually done it myself, yet), but I think in theory this should do the trick:

myPicturebox.Image := Image.FromFile( sFilePath );
1 Like

So i had the code
Var
tb:Image
begin
tb.
end;

But no FromFile Appeared in the code-complete toolbox.

However…

I just wrote

tb:=Image.FromFile(“e:\penelope.bmp”);

which worked.

I’m unsure of the implications of this yet. In Delphi the variable TB would have all of the properties of it’s class but here
something else is happening. I need a reference that can explain why the variable, tb of class image does not exist with all of the methods of the Image class. (is that a completely wrong assessment?)

thanks for your help

FromFile() is a static method of the Image class (a class function, in Delphi terms).

That is, it is called with a class reference, not an instance. This is why it did not appear in the code completion for the tb variable. tb is a reference to an instance of Image. You should have found that FromFile() was in the code completion suggestions when you had the insertion point here:

tb := Image.|

This is also why you assign the result of the function to a variable: it returns a new Image instance, it does not operate on an existing one.

1 Like

Also a static method that returns a new instance is called a [Factory Method] (https://en.wikipedia.org/wiki/Factory_method_pattern) (in this case .FromFile())

To be honest in some cases it is very hard to understand why a factory method system is used and why it is not an overloaded constructor. But mostly factory methods can do neat stuff like limiting the number of created objects or getting objects from a pool of created objects to speed up things…

1 Like

Thanks for your help everyone. Factory Methods. I’ve found a good portion of programming troubles is just in knowing the correct words for the problem.