Bonjour,

J'essaie d'insérer une image dans un RTF. Plus exactement j'essaie de construire une chaine contenant la description de l'image et de la placer dans la propriété Rtf du RichTextBox.

J'ai utilisé la méthode qui ressort le plus dans mes recherches mais qui ne semble pas fonctionner.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const int HMM_PER_INCH = 2540;
        const int TWIPS_PER_INCH = 1440;
 
public RtfBuilder AddImage(Image image, int width, int height)
{
if (image != null)
            {
                String prefix = ImagePrefix(image, width, height);
//String data = ImageToHex(image);
                String footer = @"}";
Byte[] bytes = ImageToByte(image);
                _container.Append(prefix);
                for (int i = 0; i != bytes.Length; ++i)
                {
                    _container.AppendFormat("{0:x2}", bytes[i]); 
                } 
                //_container.Append(data);
                _container.Append(footer);
}
}
 
private String ImagePrefix(Image image, int width, int height)
        {
            float xDpi, yDpi;
            using (Graphics graphs = Graphics.FromImage(image))
            {
                xDpi = graphs.DpiX;
                yDpi = graphs.DpiY;
            }
 
            int picw = (int)Math.Round((image.Width / xDpi) * HMM_PER_INCH);
            int pich = (int)Math.Round((image.Height / yDpi) * HMM_PER_INCH);
 
            int picwgoal = (int)Math.Round((width / xDpi) * TWIPS_PER_INCH);
            int pichgoal = (int)Math.Round((height / yDpi) * TWIPS_PER_INCH);
 
            return @"{\pict\wmetafile8\picw" + picw.ToString() + @"\pich" + pich.ToString()
                + @"\picwgoal" + picwgoal.ToString() + @"\pichgoal" + pichgoal.ToString() + Environment.NewLine;
        }
 
        //private String ImageToHex(Image image)
        private Byte[] ImageToByte(Image image)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, image.RawFormat);
 
            return ms.ToArray();
 
            //String hex = BitConverter.ToString(bytes,0);
            //return hex.Replace("-", String.Empty);
        }
Comme vous pouvez le constater j'ai d'abord essayé de convertir le bytes[] en hexa puis j'ai lu quelque part qu'il fallai le formater avec AppendFormat("{02}", bytes[i]);

Mais rien n'y fait

Pour comprendre ce qui n'allait pas j'ai essayé d'insérer une image dans un document word que j'ai sauvé en rtf.

J'ai épuré le document pour ne garder que ce qu'il me fallait et j'ai remarqué que la seule chose qui diffenciait c'était la chaine en hexadécimal correspondant à mon image.

Si je place le code hexa du document rtf généré et épuré dans ma chaine cela fonctionne.

J'en déduit que la seule chose qui déconne c'est ma function ImageToByte (ou ImageToHex lors de mes premiers essais).

Qu'est-ce que je ne fait pas bien ?