vb.net 如何在Word文档中插入Image对象作为图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16538601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert Image object as picture in Word document
提问by Syspect
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.AddPicturebut it only takes a stringargument, 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 Imageparameter.
所以我有这个函数来生成和返回我的图像(.bmp 格式)。我想把它放到一个word文档中。我看了看,InlineShapes.AddPicture但它只需要一个string参数,这需要我物理保存图片,然后将图片的路径作为参数提供给我不想要的 AddPicture。我想生成图片并直接存储它,而我需要一个带Image参数的方法。
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.
PS 创建 Word 文档、表格、决定将图片放入哪个单元格以及所有这些都完成了,我只需要插入图片。
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.
这是生成图片的代码,所以你可以看到我只将它作为一个对象,但没有将它物理存储在任何地方。这是用C#写的,但是我想操作Word文档的地方,我是用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;
}
采纳答案by Milkncookiez
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:
如果您设置了 Word 文档的创建和打开,并根据您提供的功能,我想您唯一要做的就是:
Dim rng As Word.Range = oDoc.Range(int1, int2)
Dim img As Image = qrGen.generateQRcodeImage("desiredInfoToEncloseInQRcode")
Clipboard.SetImage(img)
rng.Paste()
where qrGenis 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.)
在哪里qrGen当然是实现该generateQRcodeImage()功能的类的对象。而且您还必须将此代码放在要在 Word 文档(表格/单元格/等)中排列的位置。
回答by gumuruh
This code help you to insert picture into the ms word via vb.net:
此代码可帮助您通过 vb.net 将图片插入到 ms word 中:
Dim word_app As Word._Application = New _
Word.ApplicationClass()
' Create the Word document.
Dim word_doc As Word._Document = _
word_app.Documents.Add()
Dim para As Word.Paragraph = word_doc.Paragraphs.Add()
para.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
para.Range.InlineShapes.AddPicture(YOURPATHPICTURE)
para.Range.InsertParagraphAfter()
and dont forget to import the libraries.
并且不要忘记导入库。
Imports Microsoft.Office.Interop
good luck!
祝你好运!
回答by Rodrigo Ponce
I use the following variables:
我使用以下变量:
Public oDoct As Microsoft.Office.Interop.Word.Document
Public oTable As Microsoft.Office.Interop.Word.Table
What I did was this:
我所做的是这样的:
1) I have the picture/image that I want in a PictureBox (pict1) on Form1
1) 我在 Form1 上的 PictureBox (pict1) 中有我想要的图片/图像
2) Since I want to put it in a table, I create the table
2) 既然要放在表中,就创建表
oDoct.Sections(1).Headers(1).Range.Bookmarks.Add("mHeader", )
oTable = oDoct.Tables.Add(oDoct.Sections(1).Headers(1).Range.Bookmarks.Item("mheader").Range, 2, 3)
Please note that the table will be included in the header, and I've added a bookmark ("mHeader"), but this is not necessary. I did it like this because I want my image as a header.
请注意,表格将包含在标题中,并且我添加了一个书签(“mHeader”),但这不是必需的。我这样做是因为我想要我的图像作为标题。
3) I added a bookmark, within the table, for the picture
3)我在表格中为图片添加了一个书签
oTable.Cell(1, 1).Range.Bookmarks.Add("hPicture_c11")
4) The picture is then copied in the Clipboard
4)然后将图片复制到剪贴板中
Clipboard.SetImage(Form1.pict1.Image)
5) Finally, the picture is pasted within the table
5)最后将图片粘贴到表格内
oTable.Cell(1, 1).Range.Bookmarks.Item("hPicture_c11").Range.Paste()
The "hPicture_c11" Bookmark is not mandatory. If you want to simply insert the picture use the following code:
“hPicture_c11”书签不是强制性的。如果您只想插入图片,请使用以下代码:
oDoct.Range.Bookmarks.Item("\endofdoc").Range.Paste()
One last thing: check the dimensions of your image. Even though once it is inserted into the document it can be treated as any image, if it's too big you might have to resize it in Word
最后一件事:检查图像的尺寸。即使一旦插入到文档中,它就可以被视为任何图像,如果它太大,您可能需要在 Word 中调整它的大小

