GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / AddImageFromGdPictureImage Method / AddImageFromGdPictureImage(Int32,Boolean,Boolean) Method
The unique image identifier of the GdPictureImage object representing the required image. You need to add the image resource into the currently loaded PDF document, for example, using methods of the GdPictureImaging class, whose returned value is the unique image identifier.
Applicable only for 1 bit per pixel images. Indicates, whether the inserted image shall be treated as an image mask (or stencil mask). The recommended default value is false.
Set this parameter to true if you want to draw an image directly, otherwise set it to false.

If positive, then the new page is added into the current document as the last page and the image is drawn on its whole surface.

Example





In This Topic
AddImageFromGdPictureImage(Int32,Boolean,Boolean) Method
In This Topic
Adds an image resource, specified by its unique image identifier, to the currently loaded PDF document according to what you have specified. You can obtain this identifier, for example, using methods of the GdPictureImaging class when you create the image resource using an object of the type GdPictureImage. Then you can either draw the image directly using this method or you can draw it later using the returned image resource name.
Syntax
'Declaration
 
Public Overloads Function AddImageFromGdPictureImage( _
   ByVal ImageID As Integer, _
   ByVal ImageMask As Boolean, _
   ByVal DrawImage As Boolean _
) As String
public string AddImageFromGdPictureImage( 
   int ImageID,
   bool ImageMask,
   bool DrawImage
)
public function AddImageFromGdPictureImage( 
    ImageID: Integer;
    ImageMask: Boolean;
    DrawImage: Boolean
): String; 
public function AddImageFromGdPictureImage( 
   ImageID : int,
   ImageMask : boolean,
   DrawImage : boolean
) : String;
public: string* AddImageFromGdPictureImage( 
   int ImageID,
   bool ImageMask,
   bool DrawImage
) 
public:
String^ AddImageFromGdPictureImage( 
   int ImageID,
   bool ImageMask,
   bool DrawImage
) 

Parameters

ImageID
The unique image identifier of the GdPictureImage object representing the required image. You need to add the image resource into the currently loaded PDF document, for example, using methods of the GdPictureImaging class, whose returned value is the unique image identifier.
ImageMask
Applicable only for 1 bit per pixel images. Indicates, whether the inserted image shall be treated as an image mask (or stencil mask). The recommended default value is false.
DrawImage
Set this parameter to true if you want to draw an image directly, otherwise set it to false.

If positive, then the new page is added into the current document as the last page and the image is drawn on its whole surface.

Return Value

Returns a name of the newly inserted image resource strictly bounded to the currently loaded PDF document. The GetStat method can be subsequently used to determine if this method has been successful.

This name can be subsequently used with the DrawImage method to draw an image onto the page.

Remarks
This method is only allowed for use with non-encrypted documents.

It is recommend to use the GetStat method to identify the specific reason for the method's failure, if any.

Just to remind you, that the newly created page is inserted as the last page in the loaded document if you draw the image immediately.

Example
How to add and draw an image from a tif file to a PDF document.
Dim caption As String = "Example: AddImageFromGdPictureImage"
Dim gdpictureImaging As New GdPictureImaging()
Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("image.tif")
If gdpictureImaging.GetStat() = GdPictureStatus.OK Then
    Dim width As Double = gdpictureImaging.GetWidthInches(imageID)
    Dim draw As Boolean = (gdpictureImaging.GetStat() <> GdPictureStatus.OK)
    Dim height As Double = gdpictureImaging.GetHeightInches(imageID)
    draw = draw OrElse (gdpictureImaging.GetStat() <> GdPictureStatus.OK)
    Dim gdpicturePDF As New GdPicturePDF()
    If gdpicturePDF.LoadFromFile("test.pdf", True) = GdPictureStatus.OK Then
        Dim imageResName As String = ""
        If draw Then
            'The original width or the height of the image can't be found, so
            'the image is drawn directly on the whole surface of the newly added page and
            'the dimensions of the page are the same as the original dimensions of the image.
            imageResName = gdpicturePDF.AddImageFromGdPictureImage(imageID, False, True)
            If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
                MessageBox.Show("The AddImageFromGdPictureImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        Else
            'The original width or the height of the image has been found correctly, so
            'the image is added into the document, but it is not drawn directly.
            imageResName = gdpicturePDF.AddImageFromGdPictureImage(imageID, False, False)
            If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                'The image is drawn retaining its original size onto the newly added page according to your preferences.
                If (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) <> GdPictureStatus.OK) OrElse
                   (gdpicturePDF.DrawImage(imageResName, 0, 0, CSng(width), CSng(height)) <> GdPictureStatus.OK) Then
                    MessageBox.Show("The NewPage() or the DrawImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
                End If
            Else
                MessageBox.Show("The AddImageFromGdPictureImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        End If
        gdpictureImaging.ReleaseGdPictureImage(imageID)
        If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
            'The document is overwritten.
            If gdpicturePDF.SaveToFile("test.pdf", True) = GdPictureStatus.OK Then
                MessageBox.Show("The example has been followed successfully and the file has been saved.", caption)
            Else
                MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        End If
    Else
        MessageBox.Show("The LoadFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
    End If
    gdpicturePDF.Dispose()
Else
    MessageBox.Show("The CreateGdPictureImageFromFile() method has failed with the status: " + gdpictureImaging.GetStat().ToString(), caption)
End If
gdpictureImaging.Dispose()
string caption = "Example: AddImageFromGdPictureImage";
GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile("image.tif");
if (gdpictureImaging.GetStat() == GdPictureStatus.OK)
{
    double width = gdpictureImaging.GetWidthInches(imageID);
    Boolean draw = (gdpictureImaging.GetStat() != GdPictureStatus.OK);
    double height = gdpictureImaging.GetHeightInches(imageID);
    draw = draw || (gdpictureImaging.GetStat() != GdPictureStatus.OK);
    GdPicturePDF gdpicturePDF = new GdPicturePDF();
    if (gdpicturePDF.LoadFromFile("test.pdf", true) == GdPictureStatus.OK)
    {
        string imageResName = "";
        if (draw)
        //The original width or the height of the image can't be found.
        {
            //The image is drawn directly on the whole surface of the newly added page and
            //the dimensions of the page are the same as the original dimensions of the image.
            imageResName = gdpicturePDF.AddImageFromGdPictureImage(imageID, false, true);
            if (gdpicturePDF.GetStat() != GdPictureStatus.OK)
                MessageBox.Show("The AddImageFromGdPictureImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
        else
        //The original width or the height of the image has been found correctly.
        {
            //The image is added into the document, but it is not drawn directly.
            imageResName = gdpicturePDF.AddImageFromGdPictureImage(imageID, false, false);
            if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
            {
                //The image is drawn retaining its original size onto the newly added page according to your preferences.
                if ((gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) != GdPictureStatus.OK) ||
                    (gdpicturePDF.DrawImage(imageResName, 0, 0, (float)width, (float)height) != GdPictureStatus.OK))
                    MessageBox.Show("The NewPage() or the DrawImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
            }
            else
                MessageBox.Show("The AddImageFromGdPictureImage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
        gdpictureImaging.ReleaseGdPictureImage(imageID);
        if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
        {
            //The document is overwritten.
            if (gdpicturePDF.SaveToFile("test.pdf", true) == GdPictureStatus.OK)
                MessageBox.Show("The example has been followed successfully and the file has been saved.", caption);
            else
                MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
    }
    else
        MessageBox.Show("The LoadFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
    gdpicturePDF.Dispose();
}
else
    MessageBox.Show("The CreateGdPictureImageFromFile() method has failed with the status: " + gdpictureImaging.GetStat().ToString(), caption);
gdpictureImaging.Dispose();
See Also