Pour des besoins techniques il fallait que je puisse ajouter une image générée à la volée dans un Report (rdlc). Malheureusement il n’est pas possible de passer le contenu d’une image directement dans le report, celui-ci n’acceptant que les fichiers physiques ou des urls.
Cela etant dit une première solution serait de generer une image bien precise dans un reportoire et de faire pointer le chemin du fichier dans l’image, avec le risque de verrouillage de fichier que cette opération comporte, ceci n’est pas très solide
Une deuxieme solution “standard” serait de creer un dataset, ajouter une colonne de type tableau de byte et de binder sa valeur sur le parametre Value de l’image en definissant qu’il s’agit d’une source venant d’une base de donnée. (voir ci-dessous)
Cette technique fonctionne très bien mais il faut definir un dataset puis le passer comme source de donnée au report, je la trouve assez contraignante a mettre en place.
Il existe une solution “non documentée” dont le principe est d’injecter à la volée l’image en tant que ressource embarquée et de faire pointer son identifant en valeur de l’image en indiquant qu’il s’agit d’un embeded image. Voici comment j’ai pratiqué :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(@".\Report1.rdlc");
string image = @"iVBORw0KGgoAAAANSUhEUgAAAP4AAAA7CAIAAADU0aABAAAABGdBTUEAALGPC/xhBQAAEb5JREFUeF7tnYdXFUcXwPP9VemapolGE01M7DUGNWqMJXZFRETEhhUbRcCGiiKiqDRBAUEQG2JDFLAAiiIiapLvh5MzWXb37Vv2vQVM5p13PLhv5s6de+/cuW1m//fXX3+9oz6KAv9BCiD6vn+ampvDd8buOZn2+vVr36EpCIoCHUCBd/w1xtOmpnHLQ0Jit71oafEXTAVHUcA9CvhN9EHxaN7p1bvjw+JjlO53j2EKsr8o4E/Rb3n5cuHWjal5udsPH/QXfgqOooBLFPCn6IMi5v6Js/nr9+/JLjnnEsYKrKKAXyjgZ9F//PTppFXL+Hfmxoi7D+77BUUFRFHADQr4WfRBcVNS4r7Mk+V3KmdtjMAEcgNpBVNRwHcK+F/0K+/fGx0S2NDYuCf9+OaD+31HUUFQFHCDAv4XfbAMid0elXLw1evXcyPX5ZaW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yZFsN8
n00qN/6+igMfrZnnxrXGi9+vrRJJL+6Hmh1PnyLcFYXS207+KhGoybycFPIq+9siVdmoUJ+tmygU+1jUOtPd6zOXtpJ7C+i2mgPf79d/iySnUFQU8U0CJvpKO/ygF/g+P7voo/MsJ6gAAAABJRU5ErkJg";
string reportContent = SetReportImages(doc, "image1", "image/png", image);
using (System.IO.TextReader reader = new System.IO.StringReader(reportContent))
{
this.reportViewer1.LocalReport.LoadReportDefinition(reader);
}
this.reportViewer1.RefreshReport();
}
private string SetReportImages(XmlDocument doc, string imageName, string mimeType, string contentBase64)
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("nm", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");
nsmgr.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
XmlNode embededImageNode = doc.DocumentElement.SelectSingleNode("//nm:EmbeddedImages", nsmgr);
if (embededImageNode == null)
{
XmlElement embededImageElement = doc.CreateElement("EmbeddedImages");
embededImageNode = doc.DocumentElement.AppendChild(embededImageElement);
}
string imgref = string.Format("IMG{0}", Guid.NewGuid().ToString().Substring(0, 5));
XmlElement imageElement = doc.CreateElement("EmbeddedImage", null);
embededImageNode.AppendChild(imageElement);
XmlAttribute name = doc.CreateAttribute("Name");
name.InnerText = imgref;
imageElement.Attributes.Append(name);
XmlElement mimeTypeElement = doc.CreateElement("MIMEType");
mimeTypeElement.InnerText = mimeType;
imageElement.AppendChild(mimeTypeElement);
XmlElement imageDataElement = doc.CreateElement("ImageData");
imageDataElement.InnerText = contentBase64;
imageElement.AppendChild(imageDataElement);
string imagePath = string.Format("//nm:Image[@Name='{0}']", imageName);
XmlNode imageNode = doc.DocumentElement.SelectSingleNode(imagePath, nsmgr);
if (imageNode != null)
{
XmlNode value = imageNode.SelectSingleNode("nm:Value", nsmgr);
value.InnerText = imgref;
XmlNode mimeTypeNode = imageNode.SelectSingleNode("nm:MIMEType", nsmgr);
if (mimeTypeNode == null)
{
mimeTypeNode = doc.CreateElement("MIMEType");
imageNode.AppendChild(mimeTypeNode);
}
mimeTypeNode.InnerText = mimeType;
}
string result = doc.InnerXml.Replace("xmlns=\"\"", "");
return result;
}
}
Voici le contenu du fichier rdlc :
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<InteractiveHeight>29.7cm</InteractiveHeight>
<rd:DrawGrid>true</rd:DrawGrid>
<InteractiveWidth>21cm</InteractiveWidth>
<rd:GridSpacing>0.25cm</rd:GridSpacing>
<rd:SnapToGrid>true</rd:SnapToGrid>
<RightMargin>2.5cm</RightMargin>
<LeftMargin>2.5cm</LeftMargin>
<BottomMargin>2.5cm</BottomMargin>
<rd:ReportID>f78945fb-499e-4d74-89e2-b953af73c02c</rd:ReportID>
<PageWidth>21cm</PageWidth>
<Width>9.75cm</Width>
<Body>
<ColumnSpacing>1cm</ColumnSpacing>
<ReportItems>
<Image Name="image1">
<Sizing>AutoSize</Sizing>
<Top>1cm</Top>
<Width>0.7619cm</Width>
<Source>Embedded</Source>
<Style />
<Left>1.5cm</Left>
<Height>0.7619cm</Height>
<Value>=""</Value>
</Image>
</ReportItems>
<Height>5.75cm</Height>
</Body>
<Language>en-US</Language>
<TopMargin>2.5cm</TopMargin>
<PageHeight>29.7cm</PageHeight>
</Report>
Le principe est de charger le fichier rdlc (ici en tant que fichier) , l’ouvrir avec un objet xmldocument puis rechercher la section embededimage, si elle n’existe pas la creer , puis ajouter l’image et enfin renseigner la propriété Value du controle image avec le pointeur vers l’image nouvellement embarquée, voici le resultat :
Je ne resiste pas a me faire un peu de pub ;)
ci-dessous le code source du projet :
Aucun commentaire:
Enregistrer un commentaire