Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi everyone, I want to p/invoke OleCreatePictureIndirect() function in C#, I searched it in http://www.pinvoke.net/[^] but I didnt find it, please help me solve this problem, thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Oct-12 12:29pm    
Why doing it, ever? Isn't GDI+ or, better, DirectX-based WPF could be more then enough for you?
--SA
Andrewpeter 12-Oct-12 22:24pm    
Thanks SA, I want to get images from dll file, this is VB6 article:

http://www.codeproject.com/Articles/6953/Loading-Bitmap-pictures-from-external-DLL-on-VB-6?

In this article I see him use OleCreatePictureIndirect() API, i want to convert that code to C# code; i cant find any information about this API with C#. Could you help me? Thank you very much.

Hello,

Here is the brief implementation of what you need, I didn't test it - so you may need make some corrections:
C#
[StructLayout(LayoutKind.Explicit)]
class PICTDESC 
{
    [FieldOffset(0)]
    uint cbSizeofstruct; 
    [FieldOffset(4)]
    uint picType;
    [FieldOffset(8)]
    IntPtr hbitmap;
    [FieldOffset(8)]
    IntPtr hmeta;
    [FieldOffset(8)]
    IntPtr hicon;
    [FieldOffset(8)]
    IntPtr hemf;
    [FieldOffset(12)]
    HPALETTE hpal;
    [FieldOffset(12)]
    HPALETTE xExt;
    [FieldOffset(16)]
    HPALETTE yExt;
}
[StructLayout(LayoutKind.Sequential)]
class RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}
[ComImport, System.Security.SuppressUnmanagedCodeSecurity]
[Guid("7BF80980-BF32-101A-8BBB-00AA00300CAB")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IPicture
{
    [PreserveSig]
    int get_Handle([Out] out IntPtr pHandle);
    
    [PreserveSig]
    int get_hPal([Out] out IntPtr phPal);
    
    [PreserveSig]
    int get_Type( [Out] out short pType);
    
    [PreserveSig]
    int get_Width( [Out] out int pWidth);
    
    [PreserveSig]
    int get_Height( [Out] out int pHeight);
    
    [PreserveSig]
    int Render( 
        [In] IntPtr hDC,
        [In] int x,
        [In] int y,
        [In] int cx,
        [In] int cy,
        [In] int xSrc,
        [In] int ySrc,
        [In] int cxSrc,
        [In] int cySrc,
        [In,MarshalAs(UnmanagedType.LPStruct)] RECT pRcWBounds);
    
    [PreserveSig]
    int set_hPal( [In] IntPtr hPal);
    
    [PreserveSig]
    int get_CurDC( [Out] out IntPtr phDC);
    
    [PreserveSig]
    int SelectPicture( IntPtr hDCIn,[Out] out IntPtr phDCOut,[Out] out IntPtr phBmpOut);
    
    [PreserveSig]
    int get_KeepOriginalFormat( [Out,MarshalAs(UnmanagedType.Bool)] out bool pKeep);
    
    [PreserveSig]
    int put_KeepOriginalFormat([In,MarshalAs(UnmanagedType.Bool)] bool keep);
    
    [PreserveSig]
    int PictureChanged( );
    
    [PreserveSig]
    int SaveAsFile([In] System.Runtime.InteropServices.ComTypes.IStream pStream,
        [In,MarshalAs(UnmanagedType.Bool)] bool fSaveMemCopy,[Out] out int pCbSize);
    
    [PreserveSig]
    int get_Attributes([Out] out uint pDwAttr);
    
}
[DllImport("olepro32.dll")]
static extern int OleCreatePictureIndirect(
    [In, MarshalAs(UnmanagedType.LPStruct)] PICTDESC pPictDesc,
    [In] ref Guid riid,
    [In, MarshalAs(UnmanagedType.Bool)] bool fOwn,
    [Out] out object ppvObj);
PICTDESC _desc = new PICTDESC();
// initalize picture desc
object _object;
Guid _guid = typeof(IPicture).GUID;
if (0 == OleCreatePictureIndirect(_desc, ref _guid, true, out _object))
{
    IPicture _picture = (IPicture)_object;
    // Working with _picture
    Marshal.ReleaseComObject(_picture);
}


Regards,
Maxim.
 
Share this answer
 
 
Share this answer
 
Comments
Andrewpeter 13-Oct-12 3:01am    
Thank you, SA. I read it, but i dont get images from C# dll (class library of C#), my dll is WIN32 dll (e.g: Shell32.dll,....). That topic (in your link) uses C# dll. I can convert all VB6 code above to C# code but the third parameter of OleCreatePictureIndirect() (it is "IPic As IPicture") - my main question is "What type of C# does equivalent with IPicture in VB6?". Thanks. My vote is 5 for your answer, hope you help me more.
Sergey Alexandrovich Kryukov 14-Oct-12 1:32am    
This is not a question of "equivalent". "IPicture", as well as most of the types (all except language aliases) do not belong in language. A type is the type. Wrong question.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900