Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to import HTML files containing images referenced using custom prefix

$
0
0

By default, RichEditControl can import HTML files containing embedded images or links to external images with the src attribute specified as an image URL. Occasionally, you may have a web file where images are referenced in a custom manner (e.g. using the prefix 'cid' in the img src attribute, as in email files). In this scenario, you should implement and register a custom IUriStreamProvider to import these files into a RichEditControl correctly. This example illustrates the technique used to get an image referenced with the "cid" prefix from an external file in "bmp" format.

See Also:
Building a mail application with the RichEditControl

Question Comments

Added By: Jaap van der Have at: 5/16/2017 11:38:27 PM    Thanks for the great example, i made the following changes to it so it detects errors and imageformat. Also a very important thing is to set the RicheditControl to async if you want to wait for the loading to finish before continueing (and have not all images loaded yet). 
RicheditControlName.Options.Import.Html.AsyncImageLoading = false;

Also see https://www.devexpress.com/Support/Center/Question/Details/T515172 (also for a bigger example based on html import and outlook embedded mail

[C#]
// HTMLImport (https://www.devexpress.com/Support/Center/Example/Details/E3123)privateclassCustomUriStreamProvider:IUriStreamProvider{privatestringbasePath;//private string imageExtension;publicstringBasePath{get{returnbasePath;}set{basePath=value;}}publicCustomUriStreamProvider(stringbasePath){BasePath=basePath;}privatestaticSystem.Drawing.Imaging.ImageFormatGetImageFormat(System.Drawing.Imageimg){if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))returnSystem.Drawing.Imaging.ImageFormat.Jpeg;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))returnSystem.Drawing.Imaging.ImageFormat.Bmp;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))returnSystem.Drawing.Imaging.ImageFormat.Png;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))returnSystem.Drawing.Imaging.ImageFormat.Emf;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))returnSystem.Drawing.Imaging.ImageFormat.Exif;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))returnSystem.Drawing.Imaging.ImageFormat.Gif;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))returnSystem.Drawing.Imaging.ImageFormat.Icon;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp))returnSystem.Drawing.Imaging.ImageFormat.MemoryBmp;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))returnSystem.Drawing.Imaging.ImageFormat.Tiff;else{System.Diagnostics.Debug.Print("Cannot find type use it as a WMF image");returnSystem.Drawing.Imaging.ImageFormat.Wmf;}}publicStreamGetStream(stringurl){//string fileName = string.Format("{0}", url.Replace("cid:", string.Empty));stringfileName=string.Format("{0}",url.Replace("cid:",string.Empty));try{if(File.Exists(BasePath+fileName)){MemoryStreammemoryStream=newMemoryStream();Imageimage=Image.FromFile(BasePath+fileName);System.Diagnostics.Debug.Print("Loading Image: "+BasePath+fileName);image.Save(memoryStream,GetImageFormat(image));memoryStream.Seek(0,SeekOrigin.Begin);returnmemoryStream;}else{thrownewFileNotFoundException(@"File "+BasePath+fileName+" is not found");}}catch(Exceptionex){XtraMessageBox.Show("Error loading image "+BasePath+fileName+" from html template possible corrupt file?\r\n\r\n"+ex);returnnull;}}}// ExportprivateclassRichEditMailMessageExporter:IUriProvider{readonlyRichEditControlcontrol;readonlyOutlook._MailItemmailItem;intimageId;stringtempFiles=Path.Combine(Directory.GetCurrentDirectory(),"TempFiles");publicRichEditMailMessageExporter(RichEditControlcontrol,Outlook._MailItemmailItem){Guard.ArgumentNotNull(control,"control");Guard.ArgumentNotNull(mailItem,"mailItem");this.control=control;this.mailItem=mailItem;}publicvirtualvoidExport(){if(!Directory.Exists(tempFiles))Directory.CreateDirectory(tempFiles);control.BeforeExport+=OnBeforeExport;stringhtmlBody=control.Document.GetHtmlText(control.Document.Range,this);control.BeforeExport-=OnBeforeExport;mailItem.BodyFormat=Outlook.OlBodyFormat.olFormatHTML;mailItem.HTMLBody=htmlBody;}privatevoidOnBeforeExport(objectsender,BeforeExportEventArgse){HtmlDocumentExporterOptionsoptions=e.OptionsasHtmlDocumentExporterOptions;if(options!=null){options.Encoding=Encoding.UTF8;}}#regionIUriProviderMemberspublicstringCreateCssUri(stringrootUri,stringstyleText,stringrelativeUri){returnString.Empty;}publicstringCreateImageUri(stringrootUri,OfficeImageimage,stringrelativeUri){stringimageName=String.Format("image{0}.png",imageId);imageId++;stringimagePath=Path.Combine(tempFiles,imageName);image.NativeImage.Save(imagePath,ImageFormat.Png);mailItem.Attachments.Add(imagePath,Outlook.OlAttachmentType.olByValue, 0,Type.Missing);return""+imageName;//using cid: breaks images in my webmail (squirrelmail)}#endregion}



Added By: Jaap van der Have at: 5/17/2017 5:31:36 AM    I changed it a bit so it is possible to have a type of error checking/messaging: 

This is in the normal method, i also unregister it after the import:
[C#]
// Custom IUriStreamProvider registrationIUriStreamServiceuriStreamService=rBox.GetService<IUriStreamService>();varHTMLimport=newCustomUriStreamProvider(imagePath);uriStreamService.RegisterProvider(HTMLimport);rBox.Options.Import.Html.AsyncImageLoading=false;rBox.HtmlText=html;if(HTMLimport.HasErrorMessage!=string.Empty){thrownewSystem.Exception(HTMLimport.HasErrorMessage);}uriStreamService.UnregisterProvider(HTMLimport);SetDefaultFont(rBox);

And this is the class:
[C#]
// HTMLImport (https://www.devexpress.com/Support/Center/Example/Details/E3123)#regionHTMLImportClassprivateclassCustomUriStreamProvider:IUriStreamProvider{privatestringbasePath;//private string imageExtension;publicstringBasePath{get{returnbasePath;}set{basePath=value;}}publicstringHasErrorMessage{get;privateset;}publicCustomUriStreamProvider(stringbasePath){HasErrorMessage=string.Empty;BasePath=basePath;}privatestaticSystem.Drawing.Imaging.ImageFormatGetImageFormat(System.Drawing.Imageimg){if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))returnSystem.Drawing.Imaging.ImageFormat.Jpeg;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))returnSystem.Drawing.Imaging.ImageFormat.Bmp;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))returnSystem.Drawing.Imaging.ImageFormat.Png;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Emf))returnSystem.Drawing.Imaging.ImageFormat.Emf;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Exif))returnSystem.Drawing.Imaging.ImageFormat.Exif;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))returnSystem.Drawing.Imaging.ImageFormat.Gif;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))returnSystem.Drawing.Imaging.ImageFormat.Icon;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.MemoryBmp))returnSystem.Drawing.Imaging.ImageFormat.MemoryBmp;if(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))returnSystem.Drawing.Imaging.ImageFormat.Tiff;else{System.Diagnostics.Debug.Print("Cannot find type use it as a WMF image");returnSystem.Drawing.Imaging.ImageFormat.Wmf;}}publicStreamGetStream(stringurl){stringfileName=string.Format("{0}",url.Replace("cid:",string.Empty));try{if(File.Exists(BasePath+fileName)){MemoryStreammemoryStream=newMemoryStream();Imageimage=Image.FromFile(BasePath+fileName);System.Diagnostics.Debug.Print("Loading Image: "+BasePath+fileName);image.Save(memoryStream,GetImageFormat(image));memoryStream.Seek(0,SeekOrigin.Begin);returnmemoryStream;}else{thrownewFileNotFoundException(@"File "+BasePath+fileName+" is not found");}}catch(Exceptionex){this.HasErrorMessage="Error loading image "+BasePath+fileName+" from html template possible corrupt file?\r\n\r\n"+ex;returnnull;}}}#endregion// Export#regionEmbeddedimageExportprivateclassRichEditMailMessageExporter:IUriProvider{readonlyRichEditControlcontrol;readonlyOutlook._MailItemmailItem;intimageId;stringtempFiles=Path.Combine(Directory.GetCurrentDirectory(),"TempFiles");publicRichEditMailMessageExporter(RichEditControlcontrol,Outlook._MailItemmailItem){Guard.ArgumentNotNull(control,"control");Guard.ArgumentNotNull(mailItem,"mailItem");this.control=control;this.mailItem=mailItem;}publicvirtualvoidExport(){if(!Directory.Exists(tempFiles))Directory.CreateDirectory(tempFiles);control.BeforeExport+=OnBeforeExport;stringhtmlBody=control.Document.GetHtmlText(control.Document.Range,this);control.BeforeExport-=OnBeforeExport;mailItem.BodyFormat=Outlook.OlBodyFormat.olFormatHTML;mailItem.HTMLBody=htmlBody;}privatevoidOnBeforeExport(objectsender,BeforeExportEventArgse){HtmlDocumentExporterOptionsoptions=e.OptionsasHtmlDocumentExporterOptions;if(options!=null){options.Encoding=Encoding.UTF8;}}#regionIUriProviderMemberspublicstringCreateCssUri(stringrootUri,stringstyleText,stringrelativeUri){returnString.Empty;}publicstringCreateImageUri(stringrootUri,OfficeImageimage,stringrelativeUri){stringimageName=String.Format("image{0}.png",imageId);imageId++;stringimagePath=Path.Combine(tempFiles,imageName);image.NativeImage.Save(imagePath,ImageFormat.Png);mailItem.Attachments.Add(imagePath,Outlook.OlAttachmentType.olByValue, 0,Type.Missing);return""+imageName;//using cid: breaks images in my webmail (squirrelmail)}#endregion}#endregion











Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>