DrawImage Method (GdPicturePDF)
In This Topic
Draws an image, specified by its resource name, onto the exactly defined area of the currently selected page of the loaded PDF document. The image already needs to be included in the current document as a resource using one of the AddImage... methods().
The coordinates and the dimensions of the destination area need to be set in the current units defined in the PDF document, related to the actual page, where the required image is to be drawn. You can use the GdPicturePDF.GetMeasurementUnit method to determine the currently defined units and you can use the GdPicturePDF.SetMeasurementUnit method to reset the units according to your preference.
Syntax
'Declaration
Public Function DrawImage( _
ByVal As String, _
ByVal As Single, _
ByVal As Single, _
ByVal As Single, _
ByVal As Single _
) As GdPictureStatus
public GdPictureStatus DrawImage(
string ,
float ,
float ,
float ,
float
)
public function DrawImage(
: String;
: Single;
: Single;
: Single;
: Single
): GdPictureStatus;
public function DrawImage(
: String,
: float,
: float,
: float,
: float
) : GdPictureStatus;
public: GdPictureStatus DrawImage(
string* ,
float ,
float ,
float ,
float
)
public:
GdPictureStatus DrawImage(
String^ ,
float ,
float ,
float ,
float
)
Parameters
- ImageResName
- The image resource name of the required image as a string.
You can obtain this name using one of these methods: %AddImageFromBitmap(Bitmap, bool, bool)% , GdPicturePDF.AddImageFromGdPictureImage, GdPicturePDF.GetPageImageResName or any of the AddJpegImageFrom...() methods. For further assistance, please see the Images section of the GdPicturePDF class in the Reference Guide.
- DstX
- The horizontal (X) coordinate of the bottom left point of the destination area, where the image is to be drawn,
expressed in the current units specified by the SetMeasurementUnit method, related to the currently selected page.
- DstY
- The vertical (Y) coordinate of the bottom left point of the destination area, where the image is to be drawn,
expressed in the current units specified by the SetMeasurementUnit method, related to the currently selected page.
- Width
- The width of the destination area, expressed in the current units specified by the SetMeasurementUnit method.
- Height
- The height of the destination area, expressed in the current units specified by the SetMeasurementUnit method.
Return Value
A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.
We strongly recommend always checking this status first.
Example
These examples show you how to draw an image loaded from different resources using different methods
- the first image is loaded from the external file and the second image is loaded from the current PDF document.
How to draw a jpeg image, loaded from the external file, onto the first page of the newly created PDF document twice, side by side, within the two same-sized areas.
Dim caption As String = "Example: DrawImage"
Dim gdpicturePDF As New GdPicturePDF()
If (gdpicturePDF.NewPDF() = GdPictureStatus.OK) AndAlso
(gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) Then
Dim image_name As String = gdpicturePDF.AddJpegImageFromFile("image.jpg")
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
Dim width As Single = gdpicturePDF.GetPageWidth()
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
Dim height As Single = gdpicturePDF.GetPageHeight()
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
'the new height of the destination area
height = height / 2
If (gdpicturePDF.SelectPage(1) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawImage(image_name, 0, 0, width, height) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawImage(image_name, 0, height, width, height) = GdPictureStatus.OK) Then
Dim status As GdPictureStatus = gdpicturePDF.SaveToFile("test_DrawImage.pdf")
If status = 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: " + status.ToString(), caption)
End If
Else
MessageBox.Show("The example has not been followed successfully." + vbCrLf + "The last known status is " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The GetPageHeight() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The GetPageWidth() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The NewPDF() method or the NewPage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: DrawImage";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if ((gdpicturePDF.NewPDF() == GdPictureStatus.OK) &&
(gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK))
{
string image_name = gdpicturePDF.AddJpegImageFromFile("image.jpg");
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
float width = gdpicturePDF.GetPageWidth();
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
float height = gdpicturePDF.GetPageHeight();
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
//the new height of the destination area
height = height / 2;
if ((gdpicturePDF.SelectPage(1) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawImage(image_name, 0, 0, width, height) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawImage(image_name, 0, height, width, height) == GdPictureStatus.OK))
{
GdPictureStatus status = gdpicturePDF.SaveToFile("test_DrawImage.pdf");
if (status == 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: " + status.ToString(), caption);
}
else
MessageBox.Show("The example has not been followed successfully.\nThe last known status is " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The GetPageHeight() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The GetPageWidth() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The NewPDF() method or the NewPage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
gdpicturePDF.Dispose();
How to draw an image, loaded from the first page of the current PDF document, onto the newly inserted page, which has the same size as the first page, within the same PDF document.
Dim caption As String = "Example: DrawImage"
Dim message As String = ""
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.LoadFromFile("test_images.pdf", True)
If status = GdPictureStatus.OK Then
Dim pageCount As Integer = gdpicturePDF.GetPageCount()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
If gdpicturePDF.SelectPage(1) <> GdPictureStatus.OK Then
GoTo [error]
End If
Dim imageCount As Integer = gdpicturePDF.GetPageImageCount()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
If imageCount > 0 Then
Dim imageName As String = gdpicturePDF.GetPageImageResName(0)
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
Dim width As Single = gdpicturePDF.GetPageWidth()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
Dim height As Single = gdpicturePDF.GetPageHeight()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
If (gdpicturePDF.InsertPage(width, height, pageCount + 1) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawImage(imageName, 0, 0, width, height) = GdPictureStatus.OK) Then
status = gdpicturePDF.SaveToFileInc("test_images.pdf")
If status = GdPictureStatus.OK Then
message = "The example has been followed successfully and the file has been saved."
Else
message = "The example has been followed successfully, but the SaveToFileInc() method has failed with the status: " + status.ToString()
End If
End If
Else
message = "The GetPageImageResName() method has failed with the status: " + gdpicturePDF.GetStat().ToString()
End If
Else
message = "The first page of this document doesn't contain any image."
End If
Else
message = "The LoadFromFile() method has failed with the status: " + status.ToString()
End If
[error]:
If message.Equals("") Then
message = "The example has not been followed successfully." + vbCrLf + "The last known status is " + gdpicturePDF.GetStat().ToString()
End If
MessageBox.Show(message, caption)
gdpicturePDF.Dispose()
string caption = "Example: DrawImage";
string message = "";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.LoadFromFile("test_images.pdf", true);
if (status == GdPictureStatus.OK)
{
int pageCount = gdpicturePDF.GetPageCount();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
if (gdpicturePDF.SelectPage(1) != GdPictureStatus.OK) goto error;
int imageCount = gdpicturePDF.GetPageImageCount();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
if (imageCount > 0)
{
string imageName = gdpicturePDF.GetPageImageResName(0);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
float width = gdpicturePDF.GetPageWidth();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
float height = gdpicturePDF.GetPageHeight();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
if ((gdpicturePDF.InsertPage(width, height, pageCount + 1) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawImage(imageName, 0, 0, width, height) == GdPictureStatus.OK))
{
status = gdpicturePDF.SaveToFileInc("test_images.pdf");
if (status == GdPictureStatus.OK)
message = "The example has been followed successfully and the file has been saved.";
else
message = "The example has been followed successfully, but the SaveToFileInc() method has failed with the status: " + status.ToString();
}
}
else
message = "The GetPageImageResName() method has failed with the status: " + gdpicturePDF.GetStat().ToString();
}
else
message = "The first page of this document doesn't contain any image.";
}
else
message = "The LoadFromFile() method has failed with the status: " + status.ToString();
error:
if (message.Equals(""))
message = "The example has not been followed successfully.\nThe last known status is " + gdpicturePDF.GetStat().ToString();
MessageBox.Show(message, caption);
gdpicturePDF.Dispose();
Example
These examples show you how to draw an image loaded from different resources using different methods
- the first image is loaded from the external file and the second image is loaded from the current PDF document.
Dim caption As String = "Example: DrawImage"
Dim gdpicturePDF As New GdPicturePDF()
If (gdpicturePDF.NewPDF() = GdPictureStatus.OK) AndAlso
(gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) Then
Dim image_name As String = gdpicturePDF.AddJpegImageFromFile("image.jpg")
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
Dim width As Single = gdpicturePDF.GetPageWidth()
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
Dim height As Single = gdpicturePDF.GetPageHeight()
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
'the new height of the destination area
height = height / 2
If (gdpicturePDF.SelectPage(1) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawImage(image_name, 0, 0, width, height) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawImage(image_name, 0, height, width, height) = GdPictureStatus.OK) Then
Dim status As GdPictureStatus = gdpicturePDF.SaveToFile("test_DrawImage.pdf")
If status = 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: " + status.ToString(), caption)
End If
Else
MessageBox.Show("The example has not been followed successfully." + vbCrLf + "The last known status is " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The GetPageHeight() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The GetPageWidth() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
Else
MessageBox.Show("The NewPDF() method or the NewPage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: DrawImage";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if ((gdpicturePDF.NewPDF() == GdPictureStatus.OK) &&
(gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK))
{
string image_name = gdpicturePDF.AddJpegImageFromFile("image.jpg");
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
float width = gdpicturePDF.GetPageWidth();
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
float height = gdpicturePDF.GetPageHeight();
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
//the new height of the destination area
height = height / 2;
if ((gdpicturePDF.SelectPage(1) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawImage(image_name, 0, 0, width, height) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawImage(image_name, 0, height, width, height) == GdPictureStatus.OK))
{
GdPictureStatus status = gdpicturePDF.SaveToFile("test_DrawImage.pdf");
if (status == 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: " + status.ToString(), caption);
}
else
MessageBox.Show("The example has not been followed successfully.\nThe last known status is " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The GetPageHeight() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The GetPageWidth() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
MessageBox.Show("The NewPDF() method or the NewPage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
gdpicturePDF.Dispose();
Dim caption As String = "Example: DrawImage"
Dim message As String = ""
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.LoadFromFile("test_images.pdf", True)
If status = GdPictureStatus.OK Then
Dim pageCount As Integer = gdpicturePDF.GetPageCount()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
If gdpicturePDF.SelectPage(1) <> GdPictureStatus.OK Then
GoTo [error]
End If
Dim imageCount As Integer = gdpicturePDF.GetPageImageCount()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
If imageCount > 0 Then
Dim imageName As String = gdpicturePDF.GetPageImageResName(0)
If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
Dim width As Single = gdpicturePDF.GetPageWidth()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
Dim height As Single = gdpicturePDF.GetPageHeight()
If gdpicturePDF.GetStat() <> GdPictureStatus.OK Then
GoTo [error]
End If
If (gdpicturePDF.InsertPage(width, height, pageCount + 1) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawImage(imageName, 0, 0, width, height) = GdPictureStatus.OK) Then
status = gdpicturePDF.SaveToFileInc("test_images.pdf")
If status = GdPictureStatus.OK Then
message = "The example has been followed successfully and the file has been saved."
Else
message = "The example has been followed successfully, but the SaveToFileInc() method has failed with the status: " + status.ToString()
End If
End If
Else
message = "The GetPageImageResName() method has failed with the status: " + gdpicturePDF.GetStat().ToString()
End If
Else
message = "The first page of this document doesn't contain any image."
End If
Else
message = "The LoadFromFile() method has failed with the status: " + status.ToString()
End If
[error]:
If message.Equals("") Then
message = "The example has not been followed successfully." + vbCrLf + "The last known status is " + gdpicturePDF.GetStat().ToString()
End If
MessageBox.Show(message, caption)
gdpicturePDF.Dispose()
string caption = "Example: DrawImage";
string message = "";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.LoadFromFile("test_images.pdf", true);
if (status == GdPictureStatus.OK)
{
int pageCount = gdpicturePDF.GetPageCount();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
if (gdpicturePDF.SelectPage(1) != GdPictureStatus.OK) goto error;
int imageCount = gdpicturePDF.GetPageImageCount();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
if (imageCount > 0)
{
string imageName = gdpicturePDF.GetPageImageResName(0);
if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
{
float width = gdpicturePDF.GetPageWidth();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
float height = gdpicturePDF.GetPageHeight();
if (gdpicturePDF.GetStat() != GdPictureStatus.OK) goto error;
if ((gdpicturePDF.InsertPage(width, height, pageCount + 1) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawImage(imageName, 0, 0, width, height) == GdPictureStatus.OK))
{
status = gdpicturePDF.SaveToFileInc("test_images.pdf");
if (status == GdPictureStatus.OK)
message = "The example has been followed successfully and the file has been saved.";
else
message = "The example has been followed successfully, but the SaveToFileInc() method has failed with the status: " + status.ToString();
}
}
else
message = "The GetPageImageResName() method has failed with the status: " + gdpicturePDF.GetStat().ToString();
}
else
message = "The first page of this document doesn't contain any image.";
}
else
message = "The LoadFromFile() method has failed with the status: " + status.ToString();
error:
if (message.Equals(""))
message = "The example has not been followed successfully.\nThe last known status is " + gdpicturePDF.GetStat().ToString();
MessageBox.Show(message, caption);
gdpicturePDF.Dispose();
See Also