Adding Image file supports into .net

  • Thread starter Thread starter Dave Quigley
  • Start date Start date
D

Dave Quigley

Hello everyone.... Im currently starting my targa support project again for
..NET and I am wondering if there are any particular interfaces or classes
that Im supposed to extend in order to do this in a propper manner. I could
try to replicate Bitmap however I believe the problem I ran into was that
the base classes for images are sealed in .net so I cant inherit from
them... Any suggestions would be greatly apprectiated. I may consider adding
things like psd support also later down the line so I want to come up with a
system now.

Dave Quigley
 
I realized that while looking through the documentation so I must be missing
something then... This is what I have for the class declaration

public class Targa : Image
{
public Targa()
{
}
}
Very simple but its giving me this error.


System.Drawing.Image.Image() is inaccessable due to its protection level. Im
not quite sure why its looking for this constructor since the base class is
abstract so it shouldent exist.

This does however pose a problem. If I inherit from Image which seems to be
the best Idea how do I go about getting the information to the graphic
object properly... I was thinking a bitmap data object internally which I
can probably just pass to be drawn.. That makes the most sense to me... Its
either do it this way or write Targa as a wrapper around Bitmap for encoding
and decoding targas. Im not quite sure the best way to do this.
 
Hi,

The constructor of the Image class is internal, that way any classes within
the same Assembly such as Bitmap can happily derive from image. What might
be an option for you (And I have not tried this!), is to write your own
class Targa, then to provide a implicit cast operator for Image or Bitmap.
That way you can pass your class into functions that require a Image. I will
do a test to see if something like this will work and post my results here.

Hope this helps
 
Hi,

I did the following little test to confirm my previous post.

class MyImage
{
private string _fileName;
private Bitmap _bitmap;

public MyImage( string fileName )
{
_fileName = fileName;
}

public static implicit operator Image( MyImage o )
{
if ( o._bitmap == null )
o._bitmap = new Bitmap( o._fileName );

return o._bitmap;
}
}

Then in my Paint event handler

MyImage img = new MyImage("c:\\winnt\\Soap Bubbles.bmp");
e.Graphics.DrawImage( img, 0, 0 );

Hope this helps
 
Thats kewl,

Could you explain the implicit operator a bit more Ive never seen it used
and I want to make sure I fully understand it before I go ahead and use it.
 
Hi Dave,

When providing an cast operator it can either be as explicit or
implicit. An explicit cast operator requires that the user explicitly
cast. So had I declared the cast operator as explicit then the call to
drawing would have been as follows.

e.Graphics.DrawImage( (Image)img, 0, 0 );

The above is still valid for the implicit cast operator, so there is no
need to declare both (in fact you get a compile error if you do).

<IMHO>
The question of course is if this is really a good design practice,
personally I try to avoid operator overloads unless they really make
sense. For me the risk of the above is that I get the false impression
that the MyImage class is derived from Image. On the other hand if it
can be pulled of seamlessly and the user of your library can reuse his
knowlege of Image and Bitmap classes it could be worth while. One way to
do this might be to provide the same interface to your class as what
Image has, if you can do that I think it would be great (Again, just my
opinion).

I assume that your implementation of the Targa loader will actually
build a bitmap and convert from the Targa file format to the in memory
Bitmap. If so then you can provide the same interface as Image and
delegate the calls to the underlying Bitmap created by the loader. I
think that would take you a very long way in accurately emulating the
Microsoft Image/Bitmap classes. You can hopefully also provide a
non-specific base class so that other image file formats can be
developed from your framework.
</IMHO>

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
Thanks for your input. After thinking about this for a good portion of today
I decided that since images such as png's jpeg's and gif's get turned into
bitmaps internally for the purposes of .net why just not write a set of
encoders and decoders instead. This will allow me to just write encoders and
decoders for targa and do all my filtering and such on the bitmap objects
directly which makes more sense to me.

Dave
 
Hi,

It is a pleasure!

For the benefit of this thread and group, I just want to highlight that the
documentation is incorrect or rather misleading in that it is implied that
the System.Drawing.Imaging.Encoder class enables one to develop custom
CODECS for the GDI+ imaging system. This is unfortunately not supported in
the current version of GDI+, not for .NET v1.0 or .NET v1.1. So your
encoder/decoder will have to be a component that returns a Bitmap. Which I
am sure is what you intended, I just wanted to clarify this point.

Hopefully Microsoft will provide this extensibility in the future.

Regards
 
Back
Top