Load images using the API in C#
Creating an empty image
To create an empty GdPicture image, use the CreateNewGdPictureImage
method of the GdPictureImaging
class. This creates a new GdPicture representation of the image, and it returns the unique image identifier (imageID
) of the newly created GdPicture image. This method accepts the following parameters:
Width
— The image width in pixels.Height
— The image height in pixels.- Color depth represented by one of the following forms:
BitDepth
— The number of bits used to indicate the color of a single pixel.PixelFormat
— A member of thePixelFormat
enumeration that specifies the format color of the color data for each pixel in an image.
- The background color specified by one of the following options:
Color
object — An object from theSystem.Drawing
namespace.ARGB
method — A method from theGdPictureImaging
class.
When you no longer need an image resource, release it with the ReleaseGdPictureImage
method.
To create a new 200×200 GdPicture image with a gray background, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Create a new GdPicture image.int imageID = gdpictureImaging.CreateNewGdPictureImage(200, 200, BitDepth: 32, gdpictureImaging.ARGB(100, 100, 100, 100));// Save the GdPicture image to a file.gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdpictureImaging.Dispose();
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Create a new GdPicture image. Dim imageID As Integer = gdpictureImaging.CreateNewGdPictureImage(200, 200, BitDepth:=32, gdpictureImaging.ARGB(100, 100, 100, 100)) ' Save the GdPicture image to a file. gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdpictureImaging.Dispose()End Using
Loading an image from an FTP server
To load an image from an FTP server, use the CreateGdPictureImageFromFTP
method of the GdPictureImaging
class. This creates a new GdPicture representation of the image, and it returns the unique image identifier (imageID
) of the newly created GdPicture image. This method accepts the following parameters:
Host
— The name of the host server.Path
— The path to the image file on the FTP server.Login
— The user’s login to authenticate on the server.Password
— The user’s password.FTPPort
— The FTP server’s port number.
When you no longer need an image resource, release it with the ReleaseGdPictureImage
method.
When transferring data to or from remote servers, you can optionally use the SetHttpTransferBufferSize
method to specify the maximum package size of the transferred data. By default, the buffer size is 4096
.
To load an image from an FTP server, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Load an image from an FTP server.int imageID = gdpictureImaging.CreateGdPictureImageFromFTP("ftp.pspdfkit.com", "/demo/source.jpg", "user", "passw0rd", 21);gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");// Release the loaded GdPicture image.gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load an image from an FTP server. Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFTP("ftp.pspdfkit.com", "/demo/source.jpg", "user", "passw0rd", 21) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") ' Release the loaded GdPicture image. gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Loading an image from the clipboard
To load an image from the clipboard, use the CreateGdPictureImageFromClipboard
method of the GdPictureImaging
class. This creates a new GdPicture representation of the image, and it returns the unique image identifier (imageID
) of the newly created GdPicture image. This method doesn’t require any parameters.
When you no longer need an image resource, release it with the ReleaseGdPictureImage
method.
To load an image from an area saved to the clipboard, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Create a GdPicture image from the source image.int imageSourceID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Copy a 100-by-100-pixel region of the source image to clipboard.gdpictureImaging.CopyRegionToClipboard(imageSourceID, 0, 0, 100, 100);// Create a new GdPicture image from the copied region.int imageID = gdpictureImaging.CreateGdPictureImageFromClipboard();gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");// Release the loaded GdPicture image.gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Create a GdPicture image from the source image. Dim imageSourceID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Copy a 100-by-100-pixel region of the source image to clipboard. gdpictureImaging.CopyRegionToClipboard(imageSourceID, 0, 0, 100, 100) ' Create a new GdPicture image from the copied region. Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromClipboard() gdpictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") ' Release the loaded GdPicture image. gdpictureImaging.ReleaseGdPictureImage(imageID)End Using
Loading an image from a metafile
To load an image from a metafile, use the [CreateGdPictureImageFromMetaFile
method][] of the GdPictureImaging
class. This creates a new GdPicture representation of the image, and it returns the unique image identifier (imageID
) of the newly created GdPicture image. This method accepts the following parameters:
FilePath
— The path to the image file.ScaleBy
— Optional: Multiplies the image by the specified factor.
The supported formats are the following:
- EMF
- WMF
- SVG
If you load the same image to the GdPictureImaging
object multiple times, use the GetLastPath
method instead of the FilePath
parameter. This method returns the file path of the latest loaded or saved file used by the GdPictureImaging
object.
When you no longer need an image resource, release it with the ReleaseGdPictureImage
method.
To load a metafile image from local storage, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();// Load an image.int imageID = gdpictureImaging.CreateGdPictureImageFromMetaFile(@"C:\temp\source.emf", 5);gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");// Release the loaded GdPicture image.gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load an image. Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromMetaFile("C:\temp\source.emf", 5) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") ' Release the loaded GdPicture image. gdpictureImaging.ReleaseGdPictureImage(imageID)End Using