A small volume of operations on IPicture and IPictureDisp in. NET

Source: Internet
Author: User

[Digress]

Recently, I am working on a program to call an experimental instrument. This instrument provides the COM interface on Windows. When calling the instrument, you need to transmit images. The IPicture interface is used in the provided interface. Because you have never been in touch with it before, I found some information and sorted it out. NET System. drawing. image conversion method.

 

[Article Index]

 

[1. What is IPicture and IPictureDisp]

According to the description of IPicture and IPictureDisp on MSDN, IPicture and IPictureDisp provide language-independent interfaces that provide Bitmap and Icon) metafile abstraction. The latter also implements the IDispatch interface to implement the automated interface of COM. In short, if you transmit images through the COM interface, you may be exposed to these two interfaces.

 

[2. Use AxHost to implement mutual conversion with System. Drawing. Image]

. NET provides an AxHost class in System. Windows. Forms to implement access to ActiveX controls. However, we only use the protected static method in AxHost. Because it is a protected method, there is no way to directly call it. Fortunately, AxHost is not a sealed class, so we can also implement the call by integrating AxHost, such as the following code:

             IPictureConverter() : (       IPictureDisp                                               IPicture                                         }

[3. use VB6 compatibility library for mutual conversion]

In addition to the AxHost, Microsoft also provides another library for mutual conversion of hosted images and IPicture, that is, Microsoft. visualBasic. compatibility. dll. One of the classes named Support provides many backward compatible methods. For IPicture or IPictureDisp conversion, we can write the following code:

             IPictureDisp                                               IPicture                                         }

It seems that the two are almost the same, but interestingly, although many methods of the two, such as GetPICTDESCFromPicture, are not the same method, even interfaces such as IPicture are not defined in a library (AxHost is defined in System. windows. forms. unsafeNativeMethods, while VB6 compatibility library is defined in a separate stdole. dll), but the execution content in the called method is basically the same, IPicture and Other interfaces are also the same Guid of ComImport, and the source of the implementation of the two methods, it is also oleaut32.dll of DllImport, which calls the "OleCreatePictureIndirect" method, so the above two methods are exactly the same.

However, Microsoft. visualBasic. compatibility. the dll version is 10.0.0.0, and the Support class and corresponding methods are marked as Obsolete (Obsolete ), therefore, the warning prompted during compilation is quite disgusting (the 8.0.0.0 dll provided in the CLR of 2.0 does not have this problem), so the first method may be used.

 

[IV. Alpha Channel problems]

If your image contains an Alpha channel, the above conversion may cause some color problems. Since IPicture cannot support the Alpha channel, the only way to compromise is to either not use the Alpha channel, either place a solid background under the image before converting to IPicture (for example, the background of the image to be displayed in the other program), such as How to Convert a System. drawing. image to an IPictureDisp with Alpha Transparency this article does.

 

[Related link]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.