/// <summary>
        /// Get the image stored at the specified path as an IPicture.
        /// The PictureHolder remains responsible to dispose or release the picture as needed,
        /// typically when the application exits, so the client may keep and use it indefinitely.
        /// </summary>
        public IPicture GetComPicture(string imagePath)
        {
            IPicture comPicture;

            if (m_previousPictures == null)
            {
                m_previousPictures = new Dictionary <string, IPicture>();
            }
            if (m_previousPictures.TryGetValue(imagePath, out comPicture))
            {
                return(comPicture);
            }
            try
            {
                string actualFilePath = FileUtils.ActualFilePath(imagePath);
                using (var image = Image.FromFile(actualFilePath))
                {
                    comPicture = (IPicture)OLECvt.ToOLE_IPictureDisp(image);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failed to create picture from path " + imagePath + " exception: " + e.Message);
                comPicture = null;                 // if we can't get the picture too bad.
            }
            m_previousPictures[imagePath] = comPicture;
            return(comPicture);
        }
Example #2
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Prepare an image by replacing the transparent color with the window color.
 /// </summary>
 /// <param name="img"></param>
 /// <returns></returns>
 /// ------------------------------------------------------------------------------------
 private static IPicture PrepareImage(Image img)
 {
     using (var bitmap = ResourceHelper.ReplaceTransparentColor(img, SystemColors.Window))
     {
         return((IPicture)OLECvt.ToOLE_IPictureDisp(bitmap));
     }
 }
Example #3
0
        private IPicture LoadPicture()
        {
            IPicture picture;
            Image    image = null;

            try
            {
                try
                {
                    image = Image.FromFile(FileUtils.ActualFilePath(m_sPath));
                }
                catch
                {
                    // unable to read image. set to default image that indicates an invalid image.
                    image = SimpleRootSite.ImageNotFoundX;
                }
                try
                {
                    picture = (IPicture)OLECvt.ToOLE_IPictureDisp(image);
                }
                catch
                {
                    // conversion to OLE format from current image format is not supported (e.g. WMF file)
                    // try to convert it to a bitmap and convert it to OLE format again.
                    // TODO: deal with transparency
                    // We could just do the following line (creating a new bitmap) instead of going
                    // through a memory stream, but then we end up with an image that is too big.
                    //image = new Bitmap(image, image.Size);
                    using (MemoryStream imageStream = new MemoryStream())
                    {
                        image.Save(imageStream, ImageFormat.Png);
                        image.Dispose();
                        // TODO-Linux: useEmbeddedColorManagement parameter is not supported
                        // on Mono
                        image = Image.FromStream(imageStream, true);
                    }
                    picture = (IPicture)OLECvt.ToOLE_IPictureDisp(image);
                }
                m_width           = picture.Width;
                m_height          = picture.Height;
                m_internalPicture = new WeakReference(picture);
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
            return(picture);
        }
        /// <summary>
        /// Get an IPicture corresponding to the specified key. If one is not known,
        /// obtain it from the source image, and save it for next time. The PictureHolder
        /// remains responsible for disposing or releasing the picture in either case.
        /// </summary>
        public IPicture GetPicture(string key, Image source)
        {
            IPicture comPicture;

            if (m_previousPictures == null)
            {
                m_previousPictures = new Dictionary <string, IPicture>();
            }
            if (!m_previousPictures.TryGetValue(key, out comPicture))
            {
                comPicture = (IPicture)OLECvt.ToOLE_IPictureDisp(source);
                m_previousPictures[key] = comPicture;
            }
            return(comPicture);
        }
Example #5
0
 /// <summary>
 /// Converts the image to (an OLECvt) IPicture picture and wraps it with a disposable object.
 /// </summary>
 /// <param name="image">The image.</param>
 /// <returns></returns>
 static public ComPictureWrapper ConvertImageToComPicture(Image image)
 {
     return(new ComPictureWrapper((IPicture)OLECvt.ToOLE_IPictureDisp(image)));
 }
Example #6
0
 public ImageBox(AssembledStyles styles, Image image) : base(styles)
 {
     BaseImage = image;             // keep a reference to it so it doesn't become garbage
     Picture   = (IPicture)OLECvt.ToOLE_IPictureDisp(image);
 }
Example #7
0
        /// -----------------------------------------------------------------------------------
        /// <summary>
        /// Display the specified object (from an ORC embedded in a string). The default
        /// here knows how to display IPictures.
        /// </summary>
        /// <param name="vwenv"></param>
        /// <param name="hvo"></param>
        /// -----------------------------------------------------------------------------------
        public virtual void DisplayEmbeddedObject(IVwEnv vwenv, int hvo)
        {
            CheckDisposed();
            // See if it is a CmPicture.
            ISilDataAccess sda   = vwenv.DataAccess;
            int            clsid = sda.get_IntProp(hvo, (int)CmObjectFields.kflidCmObject_Class);

            if (clsid != CmPicture.kclsidCmPicture)
            {
                return;                 // don't know how to deal with it.
            }
            int hvoFile = sda.get_ObjectProp(hvo, (int)CmPicture.CmPictureTags.kflidPictureFile);

            if (hvoFile == 0)
            {
                return;
            }
            string path;
            string fileName = sda.get_UnicodeProp(hvoFile, (int)CmFile.CmFileTags.kflidInternalPath);

            if (Path.IsPathRooted(fileName))
            {
                path = fileName;
            }
            else
            {
                if (m_hvoLangProject == 0)
                {
                    TryToSetLangProjectHvo(sda, hvo);
                }
                string externalRoot = sda.get_UnicodeProp(m_hvoLangProject, (int)LangProject.LangProjectTags.kflidExtLinkRootDir);
                if (String.IsNullOrEmpty(externalRoot))
                {
                    path = Path.Combine(DirectoryFinder.FWDataDirectory, fileName);
                }
                else
                {
                    path = Path.Combine(externalRoot, fileName);
                }
            }
            vwenv.set_IntProperty((int)FwTextPropType.ktptAlign,
                                  (int)FwTextPropVar.ktpvEnum, (int)FwTextAlign.ktalCenter);
            Image image;

            try
            {
                image = Image.FromFile(FileUtils.ActualFilePath(path));
            }
            catch
            {
                // unable to read image. set to default image that indicates an invalid image.
                image = ResourceHelper.ImageNotFoundX;
            }
            stdole.IPicture picture;
            try
            {
                picture = (stdole.IPicture)OLECvt.ToOLE_IPictureDisp(image);
            }
            catch
            {
                // conversion to OLE format from current image format is not supported (e.g. WMF file)
                // try to convert it to a bitmap and convert it to OLE format again.
                // TODO: deal with transparency
                // We could just do the following line (creating a new bitmap) instead of going
                // through a memory stream, but then we end up with an image that is too big.
                //image = new Bitmap(image, image.Size);
                using (MemoryStream imageStream = new MemoryStream())
                {
                    image.Save(imageStream, ImageFormat.Png);
                    image = Image.FromStream(imageStream, true);
                }
                picture = (stdole.IPicture)OLECvt.ToOLE_IPictureDisp(image);
            }
            // -1 is ktagNotAnAttr. 0 width & height mean use natural width/height.
            vwenv.AddPictureWithCaption(picture, -1, CaptionProps, hvoFile, m_wsDefault, 0, 0, this);
            image.Dispose();
        }
Example #8
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Prepare an image by replacing the transparent color with the window color.
 /// </summary>
 /// <param name="img"></param>
 /// <returns></returns>
 /// ------------------------------------------------------------------------------------
 private stdole.IPicture PrepareImage(Image img)
 {
     return((stdole.IPicture)OLECvt.ToOLE_IPictureDisp(
                ResourceHelper.ReplaceTransparentColor(img, SystemColors.Window)));
 }