Load Images and PDF Files in C#

PDF creation and loading in GdPicture.NET is governed by the GdPicturePDF class, which is responsible for almost all the PDF features GdPicture.NET has to offer.

For example, here’s how to load a PDF file on disk:

GdPicturePDF oGdPicturePDF = new GdPicturePDF();
GdPictureStatus status = oGdPicturePDF.LoadFromFile("MyPDF.pdf", false);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The file can't be loaded. Error: " + status.ToString(), "Loading PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}
Dim oGdPicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = oGdPicturePDF.LoadFromFile("MyPDF.pdf", False)
If status <> GdPictureStatus.OK Then
   MessageBox.Show("The file can't be loaded. Error: " + status.ToString(), "Loading PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
   Return
End If

Now you can manipulate, draw on, merge, search, annotate, and save the file.

Viewing Images and PDF Documents from Locally Stored Files

The GdViewer class is designed to automate most viewing operations for you.

You can add a GdViewer object to your form and call one of the Display functions — for example, DisplayFromFile():

GdViewer GdViewer1 = new GdViewer();
GdViewer1.DisplayFromFile("");
Dim GdViewer1 As New GdViewer()
GdViewer1.DisplayFromFile("")

An empty string is passed as the file location parameter. This prompts the function to display an OpenFileDialog that’s linked to the GdViewer.

Viewing GdPictureImage Identifiers

Most GdPicture image processing occurs by using GdPictureImage identifiers. The following code shows how to create a GdPictureImage identifier from a file and how to view the resulting image in your GdViewer:

// We assume GdPicture has been correctly installed and unlocked.
// Initializing the `GdPictureImaging` object.
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
GdViewer GdViewer1 = new GdViewer();
// Loading the image from a file.
int imageID = oGdPictureImaging.CreateGdPictureImageFromFile("");
// Checking if the image has been loaded correctly.
if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
{
    // Displaying the image and checking the status.
    if (GdViewer1.DisplayFromGdPictureImage(imageID) != GdPictureStatus.OK)
    {
        MessageBox.Show("Error occurred when displaying the image. Error: " + GdViewer1.GetStat().ToString(), "Viewing Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    // Releasing the image resource.
    oGdPictureImaging.ReleaseGdPictureImage(imageID);
}
else
{
    // Displaying the error message.
    MessageBox.Show("The file can't be loaded. Error:" + oGdPictureImaging.GetStat().ToString(), "Viewing Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
oGdPictureImaging.Dispose();
'We assume GdPicture has been correctly installed and unlocked.
'Initializing the `GdPictureImaging` object.
Dim oGdPictureImaging As New GdPictureImaging()
Dim GdViewer1 As New GdViewer()
'Loading the image from a file.
Dim imageID As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("")
'Checking if the image has been loaded correctly.
If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
    'Displaying the image and checking the status.
    If GdViewer1.DisplayFromGdPictureImage(imageID) <> GdPictureStatus.OK Then
        MessageBox.Show("Error occurred when displaying the image. Error: " + GdViewer1.GetStat().ToString(), "Viewing Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Releasing the image resource.
    oGdPictureImaging.ReleaseGdPictureImage(imageID)
Else
    'Displaying the error message.
    MessageBox.Show("The file can't be loaded. Error:" + oGdPictureImaging.GetStat().ToString(), "Viewing Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
oGdPictureImaging.Dispose()

Viewing Documents from Another Source

From the following list, use the function that matches the file format of your image or PDF: