'System.Drawing.Bitmap.Bitmap()' Error

F

Fir5tSight

Hi All,

I used the following code in C#:

using System.Drawing;


//blah blah blah

Bitmap bmp = new Bitmap();
bmp = (Bitmap)image.GetImage();

I got the compiling error:

"'System.Drawing.Bitmap.Bitmap()' is inaccessible due to its protection
level"

Any advice on how to get rid of this error? Thanks!

-Emily
 
K

Kevin Spencer

//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.
 
F

Fir5tSight

Hi Kevin,

Thanks for the clear explanation!

My code now looked like follows:

Bitmap bmp = (Bitmap)image.GetImage();
bmp.Save("C:\\Temp\\" + numOfImageObjects.ToString() + ".bmp");

Now it throws an error on the line containing the "Save" method:

"An unhandled exception of type 'System.NullReferenceException'
occurred in TextExt.exe

Additional information: Object reference not set to an instance of an
object."

I believe that this is caused by bmp being null. How come bmp is null?
Any advice?

Thanks!
-Emily
 
K

Kevin Spencer

Hi Emily,

It's hard to know for sure, because you sill haven't told me what type the
"image" variable holds. But I would guess that the GetImage() method of this
object is not returning an image. It's always a good practice to check for
null values when an object has a dependency on another object in order to
have a value. "There's many a slip 'twixt the cup and the lip" as they say.
BTW, don't you have a debugger? You can set breakpoints and do watches, and
that sort of thing in Visual Studio, to diagnose this sort of thing.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.
 
F

Fir5tSight

Hi Kevin,

The definition of image is:

PDFPARSERLib.Image image;

I use the API library downloaded from pdf-tools.com.

I tried the following before:

byte[] data = new byte[32768];
data = (byte[])image.GetImage();

and the got the same "invalid data" for data. It seems that the
"GetImage" method doesn't return valid data. Do you have any advice on
how to get good data for image?

Thanks,
-Emily
 
K

Kevin Spencer

Okay, you're using a COM object in a .Net application, right? That would
imply that you're using an Interop assembly. The Interop assembly should
tell you (in the Class View in Visual Studio, assuming you have VS) what
data type this and other methods of this class returns.

The following section of the MSDN2 Library should help you. And BTW, it's
best to avoid COM Interop whenever you possibly can!

http://msdn2.microsoft.com/en-us/library/z6tx9dw3.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.
 
F

Fir5tSight

Hi Kevin,

Thanks for the instructions! I've found the return type of the
"GetImage" method. It's defined as follows:

public virtual new System.Object GetImage ( )
Member of PDFPARSERLib.ImageClass

It seems that the return type is Object. What does this mean? I assume
it should return either a byte array or a bitmap. Could you tell me
what I should do to get meaning image data from this method? Thanks
again!

-Emily
 
K

Kevin Spencer

Hi Emily,

That's not very helpful, I'm afraid. From what I saw of the documentation
for your 3rd-party COM object, the return type is a variant, which means
that it can be anything. The .Net equivalent of a variant is Object, since
all classes are inherited from Object, and it can therefore be any type of
class.

It might be possible to do some watching in the debugger to ascertain what
the actual return type is, but rather than waste your time doing that, I
would ask the manufacturer of the COM object. It is highly likely that it
returns some type of image format, but which format I could not say. The
vendor would know, though.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top