/// <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); }
/// <summary> /// Constructor. /// </summary> /// <param name="distFilesPath">the path to the distfiles Directory</param> /// <param name="relativeImagePath">they path to the image, relative to the distfiles Directory</param> /// <remarks>Will throw an exception if the image is not found.</remarks> public ImageSlice(string distFilesPath, string relativeImagePath) : base(new PictureBox()) { string sPathname = System.IO.Path.Combine(distFilesPath, relativeImagePath); ((PictureBox)this.Control).Image = Image.FromFile(FileUtils.ActualFilePath(sPathname)); ((PictureBox)this.Control).Height = ((PictureBox)this.Control).Image.Height; }
/// <summary> /// Handle launching of the media player. /// </summary> protected override void HandleChooser() { var file = m_obj as ICmFile; // Open the file with Media Player or whatever the user has set up. try { string sPathname = FileUtils.ActualFilePath(file.AbsoluteInternalPath); if (IsWavFile(sPathname)) { using (System.Media.SoundPlayer simpleSound = new System.Media.SoundPlayer(sPathname)) { simpleSound.Play(); } } else { using (System.Diagnostics.Process.Start(sPathname)) { } } } catch (Exception ex) { MessageBox.Show(ex.Message, DetailControlsStrings.ksNoPlayMedia); } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Initialize the dialog (and let the user select a picture) /// </summary> /// <returns>True if initialization succeeded, false otherwise</returns> /// ------------------------------------------------------------------------------------ public bool Initialize() { CheckDisposed(); ILgWritingSystemFactory wsf = m_cache.LanguageWritingSystemFactoryAccessor; m_txtCaption.WritingSystemFactory = wsf; m_txtCaption.WritingSystemCode = m_captionWs; m_txtDestination.Text = s_sExternalLinkDestinationDir ?? m_cache.LangProject.ExternalLinkRootDir; if (m_initialPicture != null) { ITsString tss = m_initialPicture.Caption.GetAlternative(m_captionWs).UnderlyingTsString; m_txtCaption.Tss = tss.Length == 0 ? MakeEmptyCaptionString() : tss; if (m_initialPicture.PictureFileRA == null) { m_filePath = String.Empty; } else { m_filePath = m_initialPicture.PictureFileRA.AbsoluteInternalPath; if (m_filePath == CmFile.EmptyFileName) { m_filePath = String.Empty; } } m_filePath = FileUtils.ActualFilePath(m_filePath); if (File.Exists(m_filePath)) { m_currentImage = Image.FromFile(m_filePath); } else { // use an image that indicates the image file could not be opened. m_currentImage = ResourceHelper.ImageNotFoundX; } UpdatePicInformation(); m_rbLeave.Checked = true; return(true); } m_txtCaption.Tss = MakeEmptyCaptionString(); // if the user isn't editing an existing picture, then go ahead and bring up // the file chooser DialogResult result = ShowChoosePictureDlg(); if (result == DialogResult.Cancel) { // use an image that indicates the we don't have an image Debug.Assert(m_currentImage == null); m_currentImage = ResourceHelper.ImageNotFoundX; UpdatePicInformation(); } ApplyDefaultFileLocationChoice(); return(result == DialogResult.OK); }
/// ----------------------------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="ImageDialog"/> class. /// </summary> /// ----------------------------------------------------------------------------------- public ImageDialog(string path, string caption) { InitializeComponent(); this.pictureBox1.Image = Image.FromFile(FileUtils.ActualFilePath(path)); this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; this.Size = new Size(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height + 50); this.Text = caption; }
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> /// Show the file chooser dialog for opening an image file. /// </summary> /// ------------------------------------------------------------------------------------ private DialogResult ShowChoosePictureDlg() { DialogResult dialogResult = DialogResult.None; using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.InitialDirectory = (m_grpFileLocOptions.Visible) ? m_txtDestination.Text : m_cache.LangProject.ExternalLinkRootDir; dlg.Filter = ResourceHelper.BuildFileFilter(new FileFilterType[] { FileFilterType.AllImage, FileFilterType.AllFiles }); dlg.FilterIndex = 1; dlg.Title = FwCoreDlgs.kstidInsertPictureChooseFileCaption; dlg.RestoreDirectory = true; dlg.CheckFileExists = true; dlg.CheckPathExists = true; while (dialogResult != DialogResult.OK && dialogResult != DialogResult.Cancel) { dialogResult = dlg.ShowDialog(); if (dialogResult == DialogResult.OK) { string file = dlg.FileName; if (String.IsNullOrEmpty(file)) { return(DialogResult.Cancel); } Image image; try { image = Image.FromFile(FileUtils.ActualFilePath(file)); } catch (OutOfMemoryException) // unsupported image format { MessageBox.Show(FwCoreDlgs.kstidInsertPictureReadError, FwCoreDlgs.kstidInsertPictureReadErrorCaption); dialogResult = DialogResult.None; continue; } m_filePath = file; m_currentImage = image; UpdatePicInformation(); if (m_grpFileLocOptions.Visible) { ApplyDefaultFileLocationChoice(); } } } } return(dialogResult); }
private void InstallPicture(PictureBox pb) { try { pb.Image = Image.FromFile(FileUtils.ActualFilePath(m_picture.PictureFileRA.AbsoluteInternalPath)); m_aspectRatio = (float)pb.Image.Height / (float)pb.Image.Width; if (m_aspectRatio == 0.0) { m_aspectRatio = 0.0001F; // avoid divide by zero. } } catch { // If we can't get the image for some reason, just ignore? } }
//IxCoreColleague public void Init(Mediator mediator, XmlNode configurationParameters) { CheckDisposed(); base.m_configurationParameters = configurationParameters; // save for acc info string path = XmlUtils.GetManditoryAttributeValue(configurationParameters, "imagePath"); path = mediator.GetRealPath(path); if (System.IO.File.Exists(path)) { this.pictureBox1.Image = Image.FromFile(FileUtils.ActualFilePath(path)); } else { imagePath.Text = path; } //throw new ConfigurationException("Could not find this file", configurationParameters); }
//IxCoreColleague public void Init(Mediator mediator, PropertyTable propertyTable, XmlNode configurationParameters) { CheckDisposed(); m_configurationParameters = configurationParameters; // save for acc info string path = XmlUtils.GetMandatoryAttributeValue(configurationParameters, "imagePath"); path = mediator.GetRealPath(path); if (File.Exists(path)) { pictureBox1.Image = Image.FromFile(FileUtils.ActualFilePath(path)); } else { imagePath.Text = path; } }
/// ----------------------------------------------------------------------------------- /// <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(); }