Cannot able to insert image in Word Document

Hello,
I am trying to insert image in a word ducemnt, but i cannot see any image in word document. I am using following code.

Dim totalframes As Integer

Using myimage As System.Drawing.Image = System.Drawing.Image.FromFile(inputFileName, True)

    totalframes = myimage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)
    Dim ps As Aspose.Words.PageSetup
    Dim builder As Aspose.Words.DocumentBuilde

    For i As Integer = 0 To totalframes - 1

        myimage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i)

        ps.PageWidth = ConvertUtil.PixelToPoint(myimage.Width, myimage.HorizontalResolution)
        ps.PageHeight = ConvertUtil.PixelToPoint(myimage.Height, myimage.VerticalResolution)
        Using bmpimage As System.Drawing.Image = System.Drawing.Image.FromFile(bmpfilename, True)

            builder.InsertImage(bmpimage, ps.PageWidth, ps.PageHeight)

        End Using
    Next

End Using

Please help me

Hi there,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.1.0) from here and let us know how it goes on your side.

You can simply insert an image into document using DocumentBuilder.InsertImage method. I suggest you please read the overloaded methods of DocumentBuilder.InsertImage from here:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertimage/

Please check the following code example for your kind reference. If the problem still remains, please attach your input image here for testing. I will investigate the issue on my side and provide you more information.

// Create Aspose.Words.Document and DocumentBuilder.
// The builder makes it simple to add content to the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(MyDir + "in.tiff"))
{
    // Find which dimension the frames in this image represent. For example
    // the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
    FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
    // Get the number of frames in the image.
    int framesCount = image.GetFrameCount(dimension);
    // Loop through all frames.
    for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
    {
        // Insert a section break before each new page, in case of a multi-frame TIFF.
        if (frameIdx != 0)
            builder.InsertBreak(BreakType.SectionBreakNewPage);
        // Select active frame.
        image.SelectActiveFrame(dimension, frameIdx);
        // We want the size of the page to be the same as the size of the image.
        // Convert pixels to points to size the page to the actual image size.
        PageSetup ps = builder.PageSetup;
        ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
        ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
        // Insert the image into the document and position it at the top left corner of the page.
        builder.InsertImage(image,
            RelativeHorizontalPosition.Page,
            0,
            RelativeVerticalPosition.Page,
            0,
            ps.PageWidth,
            ps.PageHeight,
            WrapType.None);
    }
}
// Save the document to Docx.
doc.Save(MyDir + "Out.docx");

Hello ,
The problem is my image size 1725 * 3195.
In below line the ps.PageWidth is coming as 612.9 and ps.PageHeight is coming as 2347.35 while debugging.

PageSetup ps = builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

However if I revaluate the Pagewidth to 612 and Pageheight to 792, then it is inserting properly in Word.
Please help me with solution.
I am also attaching the image.

Hi there,

Thanks for sharing the detail. Please use the following code to achieve your requirements. I have attached the output Docx with this post for your kind reference.

// Create Aspose.Words.Document and DocumentBuilder.
// The builder makes it simple to add content to the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(MyDir + "abcd.jpg"))
{
    // Get the number of frames in the image.
    int framesCount = image.GetFrameCount(FrameDimension.Page);
    // Loop through all frames.
    for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
    {
        // Insert a section break before each new page, in case of a multi-frame TIFF.
        if (frameIdx != 0)
            builder.InsertBreak(BreakType.SectionBreakNewPage);
        // Select active frame.
        image.SelectActiveFrame(FrameDimension.Page, frameIdx);
        // Max page size
        const double maxPageHeight = 1584;
        const double maxPageWidth = 1584;
        double currentImageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
        double currentImageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
        if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight)
        {
            // Get max image size.
            CalculateImageSize(image, maxPageHeight, maxPageWidth, out currentImageHeight, out currentImageWidth);
        }
        // We want the size of the page to be the same as the size of the image.
        // Convert pixels to points to size the page to the actual image size.
        PageSetup ps = builder.PageSetup;
        ps.PageWidth = currentImageWidth;
        ps.PageHeight = currentImageHeight;
        builder.InsertImage(image,
            RelativeHorizontalPosition.Page,
            0,
            RelativeVerticalPosition.Page,
            0,
            ps.PageWidth,
            ps.PageHeight,
            WrapType.None);
    }
}
// Save the document to Docx.
doc.Save(MyDir + "Out.docx");

Thanks for your help but what does this function/method “CaculateImageSize” does and where is this function resides.

// Get max image size.
CalculateImageSize(image, maxPageHeight, maxPageWidth, outcurrentImageHeight, out currentImageWidth);

Where can I get this method from. Is it an inbuilt method of any Aspose class.
Can you please give me CalculateImageSize funtcion as its giving me error , method does not exist.
please help me out.

Hi there,

Please accept my apologies for your inconvenience. Please check the CalculateImageSize method.

/// Calculates size of Image
///
/// Original image
/// Height of container where image should be inserted.
/// Width of container where image should be inserted.
/// Height of the image
/// Width of the image
public static void CalculateImageSize(Image img, double containerHeight, double containerWidth, out double targetHeight, out double targetWidth)
{
    // Calculate width and height
    targetHeight = containerHeight;
    targetWidth = containerWidth;
    // Get size of an image
    double imgHeight = ConvertUtil.PixelToPoint(img.Height);
    double imgWidth = ConvertUtil.PixelToPoint(img.Width);
    if (imgHeight <targetHeight && imgWidth <targetWidth)
    {
        targetHeight = imgHeight;
        targetWidth = imgWidth;
    }
    else
    {
        // Calculate size of an image in the document
        double ratioWidth = imgWidth / targetWidth;
        double ratioHeight = imgHeight / targetHeight;
        if (ratioWidth> ratioHeight)
            targetHeight = (targetHeight * (ratioHeight / ratioWidth));
        else
            targetWidth = (targetWidth * (ratioWidth / ratioHeight));
    }
}

Hi, thanks a lot for your help…My problem solved now.
Thanks again

Hi there,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.