//blah blah blah
Bitmap bmp = new Bitmap();
bmp = (Bitmap)image.GetImage();
First, there is no public parameterless constructor for the
System.Drawing.Bitmap class. The exception indicates that the method you've
called is not accessible, which means that (in this case) it is not public.
The modifiers public, private, protected, internal, etc., are called "Access
modifiers." These define what sort of code has access to a member of a
class, such as a property or a method. If a member is marked public, it is
accessible to any code in an application. If it is marked as private, it is
only accessible from within the same instance of the class. And so on. So,
you apparently got "lucky" in calling the parameterless constructor for a
Bitmap (as there is none documented), and got the "inaccessible" error. If
you need to use a constructor, use one of the overloads that is publicly
accessible.
HOWEVER, you don't even need to call a constructor in this case. A variable
is a "container" for a class. It is not a class, even though once you assign
a class to it, you can refer to the class by referring to the variable. It
is a "handle" that allows you to programmatically work with a class. So,
when you write:
Bitmap bmp;
You have created (or "declared") a variable "of type" Bitmap, which means
that it is a container for a Bitmap, and only for a (one) Bitmap. At this
point it is an "empty box." It has nothing in it. It's value is null.
When you write:
bmp = (Bitmap)image.GetImage();
You are assigning the return value from the "GetImage()" method of whatever
class "image" represents (not an Image class for sure, as the Image class
has no "GetImage()" method). In other words, the method returns a Bitmap (I
suppose), and you are assigning that Bitmap to the bmp variable, or, to put
it another way, putting the Bitmap into the bmp "box."
Now when you declare a variable, and instantiate it in the same line of
code, you are merely using a convenient method for doing 2 operations at the
same time: (1) Declaring the variable, and (2) Assigning a new instance of
the class to the variable. So, using a "legal" example:
Bitmap bmp = new Bitmap("C:\\mybitmap.bmp");
.... you are doing the same as:
Bitmap bmp; // declare the variable
bmp = new Bitmap("C:\\mybitmap.bmp");
Now, lets take a look at your code, using my "legal" constructor:
Bitmap bmp = new Bitmap("C:\\mybitmap.bmp");
bmp = (Bitmap)image.GetImage();
What's happening here? Well, you're declaring a variable of type Bitmap,
assigning a new instance of Bitmap to it (from a file in this case), and
then assigning a Bitmap returned from a method to it. What happens to the
first Bitmap? It is lost. The variable can only hold *one* instance of
Bitmap, so when you assign another instance to it, the first is "pushed
out." In technical terms, it is "dereferenced."
So, when you want to assign something to a new variable you create, it is
superfluous to assign a new instance to it first. It is simply a waste of
processor and memory. You create a new instance and then throw it away. What
you *should* be doing (again, assuming that your second line of code is
correct) would be:
Bitmap bmp;
bmp = (Bitmap)image.GetImage();
- or -
Bmp bmp = (Bitmap)image.GetImage();
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
I recycle.
I send everything back to the planet it came from.