Return a Bitmap as IPicture

Y

Yoavo

Hi,
I am writing a COM class in c# which needs to implement an interface which
one of its methods declared in the following way:
public stdole.IPicture GetBitmap()
{
}

I have a bitmap (MyBitmap.bmp) in my c# application and I want to load it,
and return it as a return value of the method GetBitmap.
I created a System.Drawing.Bitmap object and load it with MyBitmap.bmp file.
Now, I tried to convert my Bitmap object to stdole.IPicture, but I dont know
how to do it.

Can someone please help (or suggest a different approach) ?

thanks,
Yoav.
 
D

Dave Sexton

Hi Yoavo,

System.Drawing.Bitmap does not implement the OLE IPicture interface. You'll
have to create your own implementation of that interface to wrap the Bitmap
object.

Actually, just on a hunch I searched for IPicture in the VS.NET 2005 object
browser and I discovered the following method:

System.Windows.Forms.AxHost.GetIPictureFromPicture(System.Drawing.Image)

Take a look at some of the other AxHost methods that deal with IPicture as
well. They may come in handy.

- Dave Sexton
 
N

Nicholas Paldino [.NET/C# MVP]

Unfortunately, this won't work. It is protected.

Instead, set a reference to Microsoft.VisualBasic.Compatability, and
call either the static ImageToIPicture ir ImageToIPictureDisp methods on the
Support class in the Microsoft.VisualBasic.Compatability.VB6 namespace.

Hope this helps.
 
Y

Yoavo

Hi Nicholas,
I tried to follow your instructions but I cant create
"Microsoft.VisualBasic.Compatability":
The only member of "Microsoft.VisualBasic" is
"Microsoft.VisualBasic.VBCodeProvider"...


Nicholas Paldino said:
Unfortunately, this won't work. It is protected.

Instead, set a reference to Microsoft.VisualBasic.Compatability, and
call either the static ImageToIPicture ir ImageToIPictureDisp methods on
the Support class in the Microsoft.VisualBasic.Compatability.VB6
namespace.

Hope this helps.


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


Dave Sexton said:
Hi Yoavo,

System.Drawing.Bitmap does not implement the OLE IPicture interface.
You'll have to create your own implementation of that interface to wrap
the Bitmap object.

Actually, just on a hunch I searched for IPicture in the VS.NET 2005
object browser and I discovered the following method:

System.Windows.Forms.AxHost.GetIPictureFromPicture(System.Drawing.Image)

Take a look at some of the other AxHost methods that deal with IPicture
as well. They may come in handy.

- Dave Sexton
 
D

Dave Sexton

Hi Yoavo,

Nicholas is correct, the method is protected static however it's documented
on MSDN so I don't see why you can't use it. If his VB assembly suggestion
doesn't work for you here is some code that will wrap the
AxHost.GetIPictureFromPicture method for you, keeping the actual
implementation private since it's a bit ugly. Also, keep in mind that you
don't get intellisense for the call to "AxHost.GetIPictureFromPicture"
because it's marked with EditorBrowsable(false), but I tested the code and
it compiles and executes just fine:

public static class ImageUtilities
{
public static object ConvertToIPicture(Image image)
{
return ImageOLEConverter.Instance.ConvertToIPicture(image);
}

private class ImageOLEConverter : AxHost
{
public static readonly ImageOLEConverter Instance = new
ImageOLEConverter();

private ImageOLEConverter()
: base(Guid.Empty.ToString())
{
}

public object ConvertToIPicture(Image image)
{
return AxHost.GetIPictureFromPicture(image);
}
}
}

In your code, just call ImageUtilities.ConvertToIPicture(myImage).

HTH

Yoavo said:
Hi Nicholas,
I tried to follow your instructions but I cant create
"Microsoft.VisualBasic.Compatability":
The only member of "Microsoft.VisualBasic" is
"Microsoft.VisualBasic.VBCodeProvider"...


Nicholas Paldino said:
Unfortunately, this won't work. It is protected.

Instead, set a reference to Microsoft.VisualBasic.Compatability, and
call either the static ImageToIPicture ir ImageToIPictureDisp methods on
the Support class in the Microsoft.VisualBasic.Compatability.VB6
namespace.

Hope this helps.


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


Dave Sexton said:
Hi Yoavo,

System.Drawing.Bitmap does not implement the OLE IPicture interface.
You'll have to create your own implementation of that interface to wrap
the Bitmap object.

Actually, just on a hunch I searched for IPicture in the VS.NET 2005
object browser and I discovered the following method:

System.Windows.Forms.AxHost.GetIPictureFromPicture(System.Drawing.Image)

Take a look at some of the other AxHost methods that deal with IPicture
as well. They may come in handy.

- Dave Sexton

Hi,
I am writing a COM class in c# which needs to implement an interface
which one of its methods declared in the following way:
public stdole.IPicture GetBitmap()
{
}

I have a bitmap (MyBitmap.bmp) in my c# application and I want to load
it, and return it as a return value of the method GetBitmap.
I created a System.Drawing.Bitmap object and load it with MyBitmap.bmp
file.
Now, I tried to convert my Bitmap object to stdole.IPicture, but I dont
know how to do it.

Can someone please help (or suggest a different approach) ?

thanks,
Yoav.
 

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