IntPtr - what is this!?!?

  • Thread starter Thread starter Nuno Esculcas
  • Start date Start date
N

Nuno Esculcas

Hello,

I come from C++ and i now have to work with C#, and someone tell me that
bye bye pointers but i think this is not true, i must convert a DIB image in
something that i can use in C# (like Image or Bitmap) and the only thing i
can do is a DIB into a IntPtr (a pointer). I do this with the platform
invoke method of Win32:

[DllImport("gdiplus.dll", ExactSpelling=true)]
internal static extern int GdipCreateBitmapFromGdiDib( IntPtr bminfo,
IntPtr pixdat, ref IntPtr image );

Now my little impression of what i do of it it's that the IntPtr is a
pointer to a Image!?!? right??!

If is, then how i do the cast (like in old good C++):
Image* pImg = (Image*) IntPtr_variable;

Thanks
Nuno
 
Hi Nuno,

From MSDN:

A platform-specific type that is used to represent a pointer or a handle.
The IntPtr type is designed to be an integer whose size is
platform-specific. That is, an instance of this type is expected to be
32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit
hardware and operating systems.

The IntPtr type can be used by languages that support pointers, and as a
common means of referring to data between languages that do and do not
support pointers.

IntPtr objects can also be used to hold handles. For example, instances of
IntPtr are used extensively in the System.IO.FileStream class to hold file
handles.

The IntPtr type is CLS-compliant, while the UIntPtr type is not. Only the
IntPtr type is used in the common language runtime. The UIntPtr type is
provided mostly to maintain architectural symmetry with the IntPtr type.



It's normally used when you are calling to non managed code ( API, COM,
etc )

Hope this help,
 
Adding a little more details to this, GdipCreateBitmapFromGdiDib is going
to return a pointer to the native gdiplus Image object. You cannot cast
that to the managed Image object (the Common Language Runtime knows how to
handle managed objects, but doesn't understand native objects).

You should look at the System.Drawing namespace for methods that do what
you want. In particular, there is a Bitmap constructor that takes a
pointer to the bits as argument:

public Bitmap(
int width,
int height,
int stride,
PixelFormat format,
IntPtr scan0
);

If your bitmap is stored in a file or as a resource, there are even simpler
constructors you could use.

Good luck!

-- Ori.

--------------------
From: "Ignacio Machin" <ignacio.machin AT dot.state.fl.us>
References: <#[email protected]>
Subject: Re: IntPtr - what is this!?!?
Date: Mon, 8 Sep 2003 14:53:02 -0400
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 156.75.83.5
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:183261
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hi Nuno,

From MSDN:

A platform-specific type that is used to represent a pointer or a handle.
The IntPtr type is designed to be an integer whose size is
platform-specific. That is, an instance of this type is expected to be
32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit
hardware and operating systems.

The IntPtr type can be used by languages that support pointers, and as a
common means of referring to data between languages that do and do not
support pointers.

IntPtr objects can also be used to hold handles. For example, instances of
IntPtr are used extensively in the System.IO.FileStream class to hold file
handles.

The IntPtr type is CLS-compliant, while the UIntPtr type is not. Only the
IntPtr type is used in the common language runtime. The UIntPtr type is
provided mostly to maintain architectural symmetry with the IntPtr type.



It's normally used when you are calling to non managed code ( API, COM,
etc )

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nuno Esculcas said:
Hello,

I come from C++ and i now have to work with C#, and someone tell me that
bye bye pointers but i think this is not true, i must convert a DIB image in
something that i can use in C# (like Image or Bitmap) and the only thing i
can do is a DIB into a IntPtr (a pointer). I do this with the platform
invoke method of Win32:

[DllImport("gdiplus.dll", ExactSpelling=true)]
internal static extern int GdipCreateBitmapFromGdiDib( IntPtr bminfo,
IntPtr pixdat, ref IntPtr image );

Now my little impression of what i do of it it's that the IntPtr is a
pointer to a Image!?!? right??!

If is, then how i do the cast (like in old good C++):
Image* pImg = (Image*) IntPtr_variable;

Thanks
Nuno


--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top