How to using GDI+ in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm newer to GDI+ and now want to indentify the image format painted on form.
I searched msdn and find the GetRowFormat method in Image class, but I can't
find this method in my .NET environment.
How should I do? Thanks for anyone tell me.
 
Jeremy,

Once the image is painted on the form, there really isn't a format
anymore. The form (or rather, the windows) have a device context associated
with them, and that device context has capabilities which limit what can be
displayed, as well as how it is displayed.

If you want to see what the device capabilities for a device context
associated with a window are, I would get the handle for the window, through
the Handle property. Pass that to the static FromHwnd method on the
Graphics class to get an instance of the Graphics class that represents the
device context. Then call the GetHdc method on the Graphics instance, this
will give you the handle to the device context.

From there, you would then call the GetDeviceCaps API function, through
the P/Invoke layer. This will allow you to determine the bits per pixel and
other various information.

Of course, remember to clean up properly (call ReleaseHdc on the handle
returned from GetHdc, call IDisposable.Dispose on the Graphics instance).

Hope this helps.
 
Jeremy,

I think that the format that you save it in (jpg, bmp, etc, etc) doesn't
matter. Basically, when you create a new bitmap, it should default to a
PixelFormat of Format32bppArgb. If you need a higher format, then you can
use the Format64bppArgb format, but the 32 bit format should be enough.

When you choose which format to save, it's up to you, as the image will
be saved as well as can be expected given the limitations of the image
format (not the pixel format).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeremy said:
Nicholas Paldino said:
Jeremy,

Once the image is painted on the form, there really isn't a format
anymore. The form (or rather, the windows) have a device context
associated
with them, and that device context has capabilities which limit what can
be
displayed, as well as how it is displayed.

If you want to see what the device capabilities for a device context
associated with a window are, I would get the handle for the window,
through
the Handle property. Pass that to the static FromHwnd method on the
Graphics class to get an instance of the Graphics class that represents
the
device context. Then call the GetHdc method on the Graphics instance,
this
will give you the handle to the device context.

From there, you would then call the GetDeviceCaps API function,
through
the P/Invoke layer. This will allow you to determine the bits per pixel
and
other various information.

Of course, remember to clean up properly (call ReleaseHdc on the
handle
returned from GetHdc, call IDisposable.Dispose on the Graphics instance).

Hope this helps.
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi Nicholas,
Thanks for your detailed reply.
In my project,the image painted on the form comes from bitmap files which
are supported by GDI+ such as bmp,jpg,tif etc,and also from binary image
files such as some remote sensing image. As you said,the image on the form
comes from binary files are not have format.The issue is when I save the
image painted on the form I must identify if it has a format first,when it
is
a binary file image the save event should be linked to saveAs event in
order
to provid some other formats for save for users.I found the RowFormat
property in Image object but don't know how to use it for it returns an
imageFormat object which hasn't any readble imformation of image format in
its property and method except ToString method but I can't find its
documentation
Any solutions?
 
Back
Top