• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1217
  • Last Modified:

Putting a VB net image into a Word document.

Hello, Experts,

I am creating a report in Microsoft Word (2K) from information held in a VB.Net application.  I would like to include graphic images that I have available in that application.  

In the VB.Net application, I typically display the images using the paint event (of a PictureBox, etc.) through calls of the form:

    MyObject.RenderImage(e.Graphics)

Any suggestions about how I can place a copy of the resulting image into an object in my Word report will be gratefully received.  

Thanks,
Randy

0
omegaomega
Asked:
omegaomega
  • 2
  • 2
  • 2
2 Solutions
 
regevhaCommented:
You should create a Bitmap, create a Graphics object based on the Bitmap, draw on the Graphics (or copy from another Graphics object) and save the Bitmap to an image file which you can embed in your Word document
'Specify the Height and Width of the Image
Dim width As Integer = 500
Dim height As Integer = 100 

'Create a Bitmap instance of size 500x100
Dim objBitmap = New Bitmap(width, height)
Dim objGraphics = Graphics.FromImage(objBitmap) 

'Create a black background for the border
objGraphics.FillRectangle(New SolidBrush(Color.Black), 0, 0, width, height) 

'Draw some text in the image
objGraphics.DrawString("Sample Image generated on the Fly", New Font("Arial", 16, FontStyle.Bold, FontStyle.Italic), New SolidBrush(Color.Black), New Rectangle(0, 0, width, height), strFormat) 

'Save this image in the File system
objBitmap.Save("C:\Inetpub\wwwroot\WebApp\TestImage.jpg")

'Don’t forget for clean up
objGraphics.Dispose()
objBitmap.Dispose()

Open in new window

Code snippet based on http://www.etechplanet.com/blog/creating-graphics-and-charts-programmatically-using-vbnet.aspx

If you wish to copy Graphics from one object to another, you should get the hDC (Device Context handle) of each Graphics object and use BitBlt, as described in http://www.codeproject.com/KB/GDI-plus/Bitblt_wrapper_class.aspx
0
 
CodeCruiserCommented:
I am assuming that you have you image in a Image or Bitmap object. Once you have a reference to the Document object, you can use the Document.InlineShapes.Add() method to insert the image to word document.
0
 
omegaomegaDeveloperAuthor Commented:
Hi, regevha and CodeCruiser,

Thanks very much for your suggestions.   I'm sorry that my question wasn't as clear as it could have been.  CodeCruiser's assumption is basically correct.  My problems are more on the Word side than on the VB side.  I have already got the image available on the VB side, and regevha's reply shows clearly how to get that into a file that can be loaded into Word.  And CodeCruiser's reply shows how that file can be loaded on the Word side.

But I had really hoped to be able to do this without having to use the file system as an intermediary.  There can potentially be a large number of these images, and I am concerned about the performance cost of using disk files to make the transfers.

Also, the report is being generated from a Word template file.  In my initial approach I had embedded a "place-holder" picture in a section of this template.  (That section then gets duplicated as many times as required to accommodate the number of pictures to be included.)  The picture in the template does not reference any file.  This technique allows me to position the picture appropriately within the other report information without having to use any code.

I had naively assumed there would be a way to pass the bitmaps directly between the applications and associate them with the pre-existing pictures in Word.  But after digging further it now seems that not only can I not get a Device Context handle for the Word picture (possibly allowing me to use BitBlt), I can't even load an image from a file into a pre-existing picture.   It seems that the association with a file can only be made when the picture is first created/added.  

Is there really no other way than to use the file system for the transfer of the image?

Is there really no way to load an image into a pre-existing Word picture?

(I'm really afraid that the answers to both questions may be "yes".  Sigh...)

Thanks again for your input, and for any additional insights you may be able to offer.

Cheers,
Randy
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
CodeCruiserCommented:
I am inclined to say yes as well. You can delete files afterwards.
0
 
regevhaCommented:
According to Microsoft.Office.Interop,Word documentation, Shapes.AddPicture method requires a filename as input - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shapes.addpicture.aspx
Like CodeCruiser expert comment, you cannot avoid saving the image to file (temporarily).
0
 
omegaomegaDeveloperAuthor Commented:
Hello, regevha and CodeCruiser,

Thanks very much for your input.  I have used the approach that you have recommended.  

I was able to reposition the pictures relatively easily by copying properties from the template pictures to the new pictures, and then deleting the templates.  I'm a bit disappointed to have to use the file system for the transfer, but the implementation does work, so thanks again.

Cheers,
Randy


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now