Exemple #1
0
        public Image Ole2Image(int width, int height)
        {
            Bitmap result = null;

            IRichEditOle richEditInterface = this.GetRichEditOleInterface();

            if (richEditInterface.GetObjectCount() == 0)
            {
                return(result);
            }

            REOBJECT reObject = new REOBJECT();

            // S_OK == 0, so 0 == success.
            if (0 == richEditInterface.GetObject(0, reObject, GetObjectOptions.REO_GETOBJ_POLEOBJ))
            {
                try
                {
                    IntPtr pViewObject = IntPtr.Zero;
                    Guid   guid        = new Guid("{0000010d-0000-0000-C000-000000000046}");
                    Marshal.QueryInterface(reObject.poleobj, ref guid, out pViewObject);

                    IViewObject viewobject = (IViewObject)Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(IViewObject));

                    int imgwidth  = width;
                    int imgheight = System.Convert.ToInt32(reObject.sizel.y * 1.0 / reObject.sizel.x * imgwidth);
                    result = new Bitmap(imgwidth, imgheight);
                    Rectangle imgRectangle = new Rectangle(0, 0, imgwidth, imgheight);
                    using (Graphics g = Graphics.FromImage(result))
                    {
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                        IntPtr hdc = g.GetHdc();
                        viewobject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero,
                                        IntPtr.Zero, hdc, ref imgRectangle,
                                        ref imgRectangle, IntPtr.Zero, 0);
                        g.ReleaseHdc(hdc);
                        g.DrawRectangle(Pens.White, imgRectangle);
                    }

                    //Release the pViewObject
                    Marshal.Release(pViewObject);
                }
                finally
                {
                    Marshal.Release(reObject.poleobj);
                }
            }

            return(result);
        }
Exemple #2
0
        public Size GetObjectSize(Size defaultSize)
        {
            Size size = defaultSize;

            IRichEditOle richEditInterface = this.GetRichEditOleInterface();

            if (richEditInterface.GetObjectCount() == 0)
            {
                return(size);
            }

            REOBJECT reObject = new REOBJECT();

            // S_OK == 0, so 0 == success.
            if (0 == richEditInterface.GetObject(0, reObject, GetObjectOptions.REO_GETOBJ_POLEOBJ))
            {
                try
                {
                    IntPtr pViewObject = IntPtr.Zero;
                    Guid   guid        = new Guid("{0000010d-0000-0000-C000-000000000046}");
                    Marshal.QueryInterface(reObject.poleobj, ref guid, out pViewObject);

                    IViewObject viewobject = (IViewObject)Marshal.GetTypedObjectForIUnknown(pViewObject, typeof(IViewObject));

                    size = new Size(reObject.sizel.x, reObject.sizel.y);

                    //Release the pViewObject
                    Marshal.Release(pViewObject);
                }
                finally
                {
                    Marshal.Release(reObject.poleobj);
                }
            }

            return(size);
        }