如何在Word文档中插入Image对象作为图片 [英] How to insert Image object as picture in Word document

查看:1890
本文介绍了如何在Word文档中插入Image对象作为图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个功能,我正在生成并返回我的图像(.bmp格式)。我想把它写成word文档。我查看 InlineShapes.AddPicture 但它只需要一个字符串参数,这需要我在物理上保存图片然后将图片的路径作为参数添加到AddPicture中,这是我不想要的。我想生成pic并直接存储它,而我需要一个带 Image 参数的方法。

So I have this function where I am generating and returning my image (a .bmp format). I want to put it into a word document. I looked at InlineShapes.AddPicture but it only takes a string argument, which requires me to save the picture physically and then give the path of the picture as parameter to the AddPicture, which I don't want. I want to generate the pic and directly store it, whereas I need a method that takes Image parameter.

P.S。创建Word文档,表格,决定将图片放入哪个单元格并完成所有这些工作,我只需要插入图片。

P.S. the creation of Word document, tables, deciding which cell to put the pic into and all that stuff is done, I need only the insertion of the picture.

这是用于生成图片的代码,因此您可以看到我仅将其作为对象,但不要将其存储在物理上的任何位置。这是在C#中,但是我想用Word文档操作,我在VB.NET中编写。

And this is the code for generating the picture, so you can see that I have it only as an object, but don't store it anywhere physically. This is in C#, but where I want to operate with the Word document, I am writing in VB.NET.

Bitmap picture = new Bitmap(100, 100);

        // generates a QRcode image and returns it
        public Image generateQRcodeImage(string textValue)
        {
            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode;
            encoder.TryEncode(textValue, out qrCode);

            using (Graphics graph = Graphics.FromImage(picture))
            {
                new GraphicsRenderer(new FixedCodeSize(100, QuietZoneModules.Two)).Draw(graph, qrCode.Matrix);
            }

            return picture;
        }


推荐答案

如果你设置了你的Word文档创建和打开,根据您提供的功能,我想您唯一要做的就是:

If you have set your Word document creation and opening, and according to the function that you've provided, I suppose the only thing left for you to do will be:

    Dim rng As Word.Range = oDoc.Range(int1, int2)

    Dim img As Image = qrGen.generateQRcodeImage("desiredInfoToEncloseInQRcode")
    Clipboard.SetImage(img)
    rng.Paste()

其中 qrGen 当然是你的类的对象,它实现了 generateQRcodeImage()函数。
您还必须将此代码放在您想要在word文档(表格/单元格等)中进行排列的位置。

where qrGen is of course object of your class that implements the generateQRcodeImage() function. And you will also have to put this code somewhere where you want to arrange it in the word document (a table/cell/etc.)

这篇关于如何在Word文档中插入Image对象作为图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆