🔒 Locked
.GetBounds() error
Advertisement
Original Post
Hey folks, I'm having a little problem over here and I was wondering if maybe you all could help me out. First of all I'm fairly new to programming and just trying myself at some C#. :) Ok, here is the problem. I have a .gif image and I want to get its bounds. What I wrote so far is ... Image rectBall; rectBall = Image.FromFile("picture.gif"); RectangleF rect = rectBall.GetBounds(); Now, when I try to compile this, I always get the error "No overload for method 'GetBounds' takes '0' arguments". I tried searching the web a bit, looked here on gamedev.net and in the MSDN, but I just can't figure it out. :( Any help would be much appreciated. Thanks a lot in advance! mIkE_2oo0
The signature of a method is the description of the number (and types) of its arguments, as well as its return type. Before using a method, one needs to know its signature, otherwise you cannot provide the correct arguments in the correct order.
Where did you first encounter this method?
Where did you first encounter this method?
Ok, got ya I think. ;-)
For the arguments:
public RectangleF GetBounds(
ref GraphicsUnit pageUnit
);
It just has one argument - GraphicsUnit. I have however seen examples of this method used, where they didn't specify it (was on the MSDN site), hence I didn't specify it either. If it's not specified, it is automatically set to false I believe.
For the return type:
Return Value
A RectangleF structure that represents a bounding rectangle for this Image object,
That's why I defined "rect" as a RectanlgeF.
Again, I'm hoping I'm making sense here and I appologize if I'm not. I just have some basic C/C++ knowledge from my university and trying myself now at some C#. ;-)
mIkE_2oo0
For the arguments:
public RectangleF GetBounds(
ref GraphicsUnit pageUnit
);
It just has one argument - GraphicsUnit. I have however seen examples of this method used, where they didn't specify it (was on the MSDN site), hence I didn't specify it either. If it's not specified, it is automatically set to false I believe.
For the return type:
Return Value
A RectangleF structure that represents a bounding rectangle for this Image object,
That's why I defined "rect" as a RectanlgeF.
Again, I'm hoping I'm making sense here and I appologize if I'm not. I just have some basic C/C++ knowledge from my university and trying myself now at some C#. ;-)
mIkE_2oo0
Quote:
It just has one argument - GraphicsUnit. I have however seen examples of this method used, where they didn't specify it (was on the MSDN site), hence I didn't specify it either. If it's not specified, it is automatically set to false I believe.
That would make no sense, since false is not a GraphicsUnit. Your return type is correct. You simply did not specify what unit should be used to measure the dimensions, which is necessary. So, what unit do you want your rectangle dimensions to be expressed in? Inches, points, pixels?
MSDN page for your method.
Hmm ok. :)
Pixels preferably. But setting it to GraphicsUnit.Pixel results in 2 errors.
"The best overloaded method match for 'System.Drawing.Image.GetBounds(ref System.Drawing.GraphicsUnit)' has some invalid arguments"
and
"Argument '1': cannot convert from 'System.Drawing.GraphicsUnit' to 'ref System.Drawing.GraphicsUnit'"
Thanks for taking the time to help me Victor, sorry for causing this much trouble.
mIkE_2oo0
Pixels preferably. But setting it to GraphicsUnit.Pixel results in 2 errors.
"The best overloaded method match for 'System.Drawing.Image.GetBounds(ref System.Drawing.GraphicsUnit)' has some invalid arguments"
and
"Argument '1': cannot convert from 'System.Drawing.GraphicsUnit' to 'ref System.Drawing.GraphicsUnit'"
Thanks for taking the time to help me Victor, sorry for causing this much trouble.
mIkE_2oo0
The documentation is not very clear about this, it seems.
The unit type must be passed by reference, which means that you need to create a variable with the correct GraphicsUnit value, and pass that variable to the function (instead of using a temporary constant object).
Why would the function require this? I can see two reasons:
1° The function can change the value of the GraphicsUnit (for instance, to tell you what the actual unit of the graphics is).
2° To allow you to use a default value by passing null.
I believe that the second is true (since the first would have made the unit an out parameter). But I don't know what the default value is, so you can only experiment by passing null and see if the results are expressed in pixels. Otherwise, create a GraphicsUnit variable, set its value to Pixels, and pass it to the function.
EDIT: I'm not fluent enough in C# to know if there is an idiomatic way to create a temporary object that can be passed by reference. There might be one.
The unit type must be passed by reference, which means that you need to create a variable with the correct GraphicsUnit value, and pass that variable to the function (instead of using a temporary constant object).
Why would the function require this? I can see two reasons:
1° The function can change the value of the GraphicsUnit (for instance, to tell you what the actual unit of the graphics is).
2° To allow you to use a default value by passing null.
I believe that the second is true (since the first would have made the unit an out parameter). But I don't know what the default value is, so you can only experiment by passing null and see if the results are expressed in pixels. Otherwise, create a GraphicsUnit variable, set its value to Pixels, and pass it to the function.
EDIT: I'm not fluent enough in C# to know if there is an idiomatic way to create a temporary object that can be passed by reference. There might be one.
Quote:Just an FYI, C# doesn't support default parameters. In C++ you can have some function
Original post by mIkE_2oo0
It just has one argument - GraphicsUnit. I have however seen examples of this method used, where they didn't specify it (was on the MSDN site), hence I didn't specify it either. If it's not specified, it is automatically set to false I believe.
int foo(int param = 0){ return param;}and calling foo() will return 0. This isn't supported in C#. See here for some discussion on the pros and cons of this.As for seeing it used without parameters on MSDN somewhere, are you sure it was the .NET Framework's Image.GetBounds() function? Or maybe the C++ version of it? Like ToohrVyk said, the docs on that function aren't very comprehensive and it doesn't mention a default parameter for the C++ version so that's probably not it.
I also don't know much about C# so I'm not sure about ToohrVyk's edit either. Maybe there is something about it in that FAQ that I linked to above.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement