Save images in C#

To Byte Array

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.

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);