stdole.IPictureDisp to Bitmap/icon conversion in C#  
Author Message
Callavin





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

I am having a 3rd part DLL which contains Icons.

I have to use this DLL to get icons in C# application. That DLL returns the icon wrapped as an object of stdole.IPictureDisp.

Now how to convert this to get the bitmap or icon.

I am trying for image option but runtime exception comes as cant cast it.

any Help.



Visual C#14  
 
 
Craig Main





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

Hi,

You cannot cast it. As luck would have it, I wrote a little class to do this for a fingerprint scanner I was using. Here is my little class which overrides the correct methods.

/// <summary>
/// Conversion thunk.
/// </summary>
internal sealed class IPictureDispHost : AxHost {
/// <summary>
/// Default Constructor, required by the framework.
/// </summary>
private IPictureDispHost(): base(string.Empty) {}
/// <summary>
/// Convert the image to an ipicturedisp.
/// </summary>
/// <param name="image">The image instance</param>
/// <returns>The picture dispatch object.</returns>
public new static object GetIPictureDispFromPicture(Image image) {
return AxHost.GetIPictureDispFromPicture(image);
}
/// <summary>
/// Convert the dispatch interface into an image object.
/// </summary>
/// <param name="picture">The picture interface</param>
/// <returns>An image instance.</returns>
public new static Image GetPictureFromIPicture(object picture) {
return AxHost.GetPictureFromIPicture(picture);
}
}

Here is a short sample of how I used it in context.

I hope this helps.

System.Drawing.Image handle = null;
stdole.IPictureDisp disp = (stdole.IPictureDisp)IPictureDispHost.GetIPictureDispFromPicture(handle);
result = FingerDevice.BiometricDisplay(
ref template.Bytes,
ref rawImage,
width,
height,
res,
graphics.GetHdc().ToInt32(),
ref disp,
(int)GRConstants.GR_NO_CONTEXT);
if (result < 0)
throw new FingerprintTechnicalException(result);

handle = IPictureDispHost.GetPictureFromIPicture(disp);


 
 
Callavin





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

i was also trying the same. but there is one problem in this.

The object is wrapped in the stdole.IPictureDisp is basically an Icon or bitmap. So with this approach
i get this exception:

System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Drawing.Icon' to type 'System.Drawing.Image'."
Source="System.Windows.Forms"

Not getting any idea now what to do

Regards,



 
 
Craig Main





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

Hi,

An icon is not an image. You need to use the correct class in the correct context. You cannot merely cast an Icon to an Image when they are not in the same object heirarchy. You will need to construct a new Bitmap with the Icon data to convert an icon to an image. (Bitmap does in fact inherit Image). You need to understand how the object heirarchy is laid out.

You are able to use the ToBitmap method of the icon class to covert the icon to a bitmap.

Regards

Craig


 
 
Callavin





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

this is correct.

But my problem is first I have to convert the stdole.ipiscuredisp object returned by the mehtod to some object which C# can understand.
Now to convert i am using the protected function

System.Windows.Forms.AxHost.GetPictureFromIPicture(stdole.ipicturedisp obj)

which returns the Image object(I dont know why ).
And that stdole.ipiscuredisp object contains Icon inside it.
so when i call this function exception arises.

I can't change the 3rd party DLL. So i have to typecast or convert somehow if possible!!
So for this scenario which way i should go.


 
 
Craig Main





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

Hi,

Ok, I see your problem now.

I would use an Icon rather than an image as the 'handle' and try to convert that way. Have you tried this I would then start looking at all the fromHandle methods, and writing a wrapper in C++. Sorry but without more specific information I am going to struggle. The handle types for icons and images are very different and not interchangable.

Regards

Craig


 
 
Craig Main





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

Hi,

Me again.

The above suggestion will not work.

If you attempt to use a bitmap instead of an icon, does it work

My only other suggestion is to look at the source code for GetIPicture, (Posted Below) and look at the state in a de**** to try and work it all out.

[EditorBrowsable(EditorBrowsableState.Advanced)]
protected static Image GetPictureFromIPicture(object picture)
{
   if (picture == null)
   {
      return null;
   }
   IntPtr ptr1 = IntPtr.Zero;
   UnsafeNativeMethods.IPicture picture1 = (UnsafeNativeMethods.IPicture) picture;
   int num1 = picture1.GetPictureType();
   if (num1 == 1)
   {
      try
      {
         ptr1 = picture1.GetHPal();
      }
      catch (COMException)
      {
      }
   }
   return AxHost.GetPictureFromParams(picture1, picture1.GetHandle(), num1, ptr1, picture1.GetWidth(), picture1.GetHeight());
}

Sorry I cannot be more help.

Regards

Craig


 
 
MichaelKruz





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

Could i inquire as to how or if you were able to capture an image from a finger p[rint scanner please.

There are hunders of us out here tring to figure out how to capture images from a scanner without having to buy third party SDKs which cost a fortune.

If you could help it would be appreiciated.



 
 
Figo Fei - MSFT





PostPosted: Visual C# General, stdole.IPictureDisp to Bitmap/icon conversion in C# Top

Hi, 3rd party stuff is not suported here.

Thank you for understanding.