Save Images in C#

To save a GdPicture image to a file, use one of the methods of the GdPictureImaging class starting with SaveAs. For example, the SaveAsPNG method saves a GdPicture image to a PNG file. Refer to the list of supported file types to learn which file formats are available when saving an image to a file.

All saving methods use the following parameters:

  • imageID — The ID of the GdPicture image.

  • FilePath — The file path where you want to save the image. If the specified file already exists, the method overwrites it.

  • A set of format-specific parameters — Parameters specific to the file format in which you want to save the image. In most cases, these parameters are optional.

Warning

When you no longer need an image resource, release it with the ReleaseGdPictureImage method.

The following example saves a previously loaded JPG image to PNG format:

using GdPictureImaging gdPictureImaging = new GdPictureImaging();
// Create a GdPicture image from a JPG file.
int imageID = gdPictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Save the GdPicture image as a PNG file.
gdPictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png");
gdPictureImaging.ReleaseGdPictureImage(imageID);
Using gdPictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Create a GdPicture image from a JPG file.
    Dim imageID As Integer = gdPictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg")
    ' Save the GdPicture image as a PNG file.
    gdPictureImaging.SaveAsPNG(imageID, "C:\temp\output.png")
    gdPictureImaging.ReleaseGdPictureImage(imageID)
End Using

To save a GdPicture image to a bitmap image, use the SaveAsBMP method of the GdPictureImaging class. It uses the following parameters:

  • imageID — The ID of the GdPicture image.

  • FilePath — The file path where you want to save the image. If the specified file already exists, the method overwrites it.

  • UseRLE — A Boolean value that specifies if the bitmap pixels are compressed with the RLE compression scheme. It’s only available for bitmaps with 8 bits per pixel.

Warning

When you no longer need an image resource, release it with the ReleaseGdPictureImage method.

The following example saves a previously loaded JPG image to bitmap format:

using GdPictureImaging gdPictureImaging = new GdPictureImaging();
// Create a GdPicture image from a JPG file.
int imageID = gdPictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Save the GdPicture image as a bitmap file.
gdPictureImaging.SaveAsBMP(imageID, @"C:\temp\output.bmp");
gdPictureImaging.ReleaseGdPictureImage(imageID);
Using gdPictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Create a GdPicture image from a JPG file.
    Dim imageID As Integer = gdPictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg")
    ' Save the GdPicture image as a bitmap file.
    gdPictureImaging.SaveAsBMP(imageID, "C:\temp\output.bmp")
    gdPictureImaging.ReleaseGdPictureImage(imageID)
End Using

A vector graphic file is an image that can be scaled to any size without losing quality. While standard images are constructed as a set of pixels, vector images consist of mathematically defined simple shapes. One of the most popular vector image formats is SVG.

To save a GdPicture image to an SVG image, use the SaveAsSVG method of the GdPictureDocumentConverter class. It requires the file path to the newly created SVG image.

Information

In the conversion process, the vector content is preserved as much as possible. If the source contains multiple pages (such as a PDF document or a TIFF image), only the first page is converted.

To save an image in SVG format, use the following example:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load a PDF document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.pdf",
    GdPicture14.DocumentFormat.DocumentFormatPDF);
// Save the PDF document as an SVG image.
gdpictureDocumentConverter.SaveAsSVG(@"C:\temp\output.svg");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter()
    ' Load a PDF document.
    gdpictureDocumentConverter.LoadFromFile("C:\temp\source.pdf", GdPicture14.DocumentFormat.DocumentFormatPDF)
    ' Save the PDF document as an SVG image.
    gdpictureDocumentConverter.SaveAsSVG("C:\temp\output.svg")
End Using

To save a GdPicture image to a byte array, use the SaveAsByteArray method of the GdPictureImaging class. It uses the following parameters:

  • imageID — The ID of the GdPicture image.

  • Data — The reference parameter of the byte array where the image data is stored.

  • Length — The reference parameter that returns the number of bytes written into the byte array if the save operation was successful.

  • ImageFormat— The image format represented as a member of the DocumentFormat enumeration.

  • EncoderParameter — The compression or encoding quality used. This parameter and its range depend on the image format:

    • JPEG format — Image quality between 1 (lowest) and 100 (highest).

    • PNG format — Compression level between 0 (no compression and fastest encoding) and 9 (maximum compression and slowest encoding).

    • TIFF format — Compression scheme specified with the TiffCompression enumeration.

    • JPEG2000 format — Compression rate between 1 (highest quality) and 512 (lowest quality).

    • WebP format — Image quality between 1 (lowest) and 100 (highest).

    • For other formats, set this parameter to 0.

Warning

When you no longer need an image resource, release it with the ReleaseGdPictureImage method.

The following example saves a previously loaded JPG image to a byte array:

using GdPictureImaging gdPictureImaging = new GdPictureImaging();
// Create a GdPicture image from a JPG file.
int imageID = gdPictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Initialize the `Data` and `Length` reference parameters.
byte[] data = new byte[0];
int length = 0;
// Save the GdPicture image as a byte array.
gdPictureImaging.SaveAsByteArray(imageID, ref data, ref length,
    GdPicture14.DocumentFormat.DocumentFormatPNG, 9);
// Save the byte array to a PNG image.
File.WriteAllBytes(@"C:\temp\output.png", data);
gdPictureImaging.ReleaseGdPictureImage(imageID);
Using gdPictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Create a GdPicture image from a JPG file.
    Dim imageID As Integer = gdPictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg")
    ' Initialize the `Data` and `Length` reference parameters.
    Dim data = New Byte(0) {}
    Dim length = 0
    ' Save the GdPicture image as a byte array.
    gdPictureImaging.SaveAsByteArray(imageID, data, length,
        GdPicture14.DocumentFormat.DocumentFormatPNG, 9)
    ' Write the `Length` parameter to the console.
    Console.WriteLine(length)
    ' Save the byte array to a PNG image.
    File.WriteAllBytes("C:\temp\output.png", data)
    gdPictureImaging.ReleaseGdPictureImage(imageID)
End Using

To save a GdPicture image to an FTP server, use the SaveToFTP method of the GdPictureImaging class. It uses the following parameters:

  • imageID — The ID of the GdPicture image.

  • ImageFormat — The image format represented as a member of the DocumentFormat enumeration.

  • EncoderParameter — The compression or encoding quality used. This parameter and its range depend on the image format:

    • JPEG format — Image quality between 1 (lowest) and 100 (highest).

    • PNG format — Compression level between 0 (no compression and fastest encoding) and 9 (maximum compression and slowest encoding).

    • TIFF format — Compression scheme specified with the TiffCompression enumeration.

    • JPEG2000 format — Compression rate between 1 (highest quality) and 512 (lowest quality).

    • WebP format — Image quality between 1 (lowest) and 100 (highest).

    • For other formats, set this parameter to 0.

  • Host — Name of the host server.

  • Path — Path to the image file on the FTP server.

  • Login — User’s login to authenticate on the server.

  • Password — User’s password.

  • FTPPort — FTP server’s port number.

Warning

When you no longer need an image resource, release it with the ReleaseGdPictureImage method.

Information

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.

The following example saves a previously loaded JPG image to a byte array:

using GdPictureImaging gdPictureImaging = new GdPictureImaging();
// Create a GdPicture image from a JPG file.
int imageID = gdPictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Set the package size to 2048.
gdpictureImaging.SetHttpTransferBufferSize(2048);
// Save the GdPicture image to the FTP server.
gdPictureImaging.SaveToFTP(imageID, GdPicture14.DocumentFormat.DocumentFormatJPEG,
    75, "ftp.pspdfkit.com", "/demo/source.jpg", "user", "passw0rd", 21);
gdPictureImaging.ReleaseGdPictureImage(imageID);
Using gdPictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Create a GdPicture image from a JPG file.
    Dim imageID As Integer = gdPictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg")
    ' Set the package size to 2048.
    gdpictureImaging.SetHttpTransferBufferSize(2048)
    ' Save the GdPicture image to the FTP server.
    gdPictureImaging.SaveToFTP(imageID, GdPicture14.DocumentFormat.DocumentFormatJPEG,
        75, "ftp.pspdfkit.com", "/demo/source.jpg", "user", "passw0rd", 21)
    gdPictureImaging.ReleaseGdPictureImage(imageID)
End Using

To save a GdPicture image to a remote URL, use the SaveToHTTP method of the GdPictureImaging class. This method uses the HTTP PUT protocol to upload the image. It also uses the following parameters:

  • imageID — The ID of the GdPicture image.

  • ImageFormat — The image format represented as a member of the DocumentFormat enumeration.

  • EncoderParameter — The compression or encoding quality used. This parameter and its range depend on the type of image formats:

    • JPEG format — Image quality between 1 (lowest) and 100 (highest).

    • PNG format — Compression level between 0 (no compression and fastest encoding) and 9 (maximum compression and slowest encoding).

    • TIFF format — Compression scheme specified with the TiffCompression enumeration.

    • JPEG2000 format — Compression rate between 1 (highest quality) and 512 (lowest quality).

    • WebP format — Image quality between 1 (lowest) and 100 (highest).

    • For other formats, set this parameter to 0.

  • Address — The address of the remote URL.

  • Login — Optional: User’s login to authenticate on the server.

  • Password — Optional: User’s password.

Warning

When you no longer need an image resource, release it with the ReleaseGdPictureImage method.

Information

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.

The following example saves a previously loaded JPG image to a byte array:

using GdPictureImaging gdPictureImaging = new GdPictureImaging();
// Create a GdPicture image from a JPG file.
int imageID = gdPictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Set the package size to 2048.
gdpictureImaging.SetHttpTransferBufferSize(2048);
// Save the GdPicture image to the FTP server.
gdPictureImaging.SaveToFTP(imageID, GdPicture14.DocumentFormat.DocumentFormatJPEG,
    75, "ftp.pspdfkit.com", "/demo/source.jpg", "user", "passw0rd", 21);
gdPictureImaging.ReleaseGdPictureImage(imageID);
Using gdPictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Create a GdPicture image from a JPG file.
    Dim imageID As Integer = gdPictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg")
    ' Set the package size to 2048.
    gdpictureImaging.SetHttpTransferBufferSize(2048)
    ' Save the GdPicture image to the FTP server.
    gdPictureImaging.SaveToFTP(imageID, GdPicture14.DocumentFormat.DocumentFormatJPEG,
        75, "ftp.pspdfkit.com", "/demo/source.jpg", "user", "passw0rd", 21)
    gdPictureImaging.ReleaseGdPictureImage(imageID)
End Using