Add image in acomment (note)

How do I add an image to a comment?

I want to show an image when the user hover the cell, something like this:

Image in comment

Thank you in advance for the answer


J-D Gasser

Well, to be 100% honest, I wonder how to insert an image into a comment myself too :)  There is no place in the xls/xlsx file format to insert images into comments, so I imagine what you are really doing in the example you show is setting the "fill color" of the comment to be an image.

If that is the case, you can use some code like this to set the fill color of the comment:

            using (TUIImage Img = TUIImage.FromFile("imagename.png"))
                using (var ImgStream = new FileStream("imagename.png", FileMode.Open))
                    byte[] ImgData = new byte[ImgStream.Length];
                    ImgStream.Read(ImgData, 0, ImgData.Length);
                    var sourceRect = new TDrawingRelativeRect(0, 0, 100, 100);
                    var commentProperties = TCommentProperties.CreateStandard(5, 3, xls);
                    commentProperties.ShapeFill = new TShapeFill(
                        new TBlipFill(
                            192, false,
                            new TBlip(TBlipCompression.None, ImgData, "", StandardMimeType.Png),
                            null, new TBlipFillStretch(sourceRect)

                    commentProperties.Anchor = new TClientAnchor(
                        (int)(Img.HeightInPoints * 96.0 / 72.0),
                        (int)(Img.Width * 96.0 / 72.0),

                    xls.SetComment(5, 3, " ", "", commentProperties);

I've tried it here, and it creates a comment similar to the screenshot you sent. Once again, this is *not* adding an image to a comment, but just setting the comment background to be an image, but I believe this is what you are doing in that screenshot since as far as I know, you just can't add images to comments.

A final note: Sadly because (very) old design decisions (going back literally decades ago), SetComment will just remove the comment if you set it to an empty/null string. So you can't add a comment which is completely empty, and for this example, I just add a space (" ") as the comment.
Hello Adrian,
That is EXACTLY what I wanted to do, thank you for the very accurate answer.

You are right : this is not exactly adding an image to a comment, but setting an image as background.


J-D Gasser