The ImageExport object has a function called SaveToImageFile that allows you to export the image displayed by the component to a file in different file formats. This removes the need to create an ImageCodecInfo object just to specify a simple parameter such as quality in the case of a JPEG. The function has two versions:
VB.NET |
|
Public Sub SaveToImageFile(ByVal fileName As String, ByVal pixelFormat As PixelFormat, ByVal imageFormat As ImageFormat, ByVal quality As Integer, ByVal grayscale As Boolean) Public Sub SaveToImageFile(ByVal fileName As String, ByVal dimensions As Size, ByVal pixelFormat As PixelFormat, ByVal imageFormat As ImageFormat, ByVal quality As Integer, ByVal grayscale As Boolean)
|
C# |
|
public void SaveToImageFile(String fileName, PixelFormat pixelFormat, ImageFormat imageFormat, int quality, bool grayscale) public void SaveToImageFile(String fileName, Size dimensions, PixelFormat pixelFormat, ImageFormat imageFormat, int quality, bool grayscale)
|
The difference between the two versions of the function is that the fist one will generate an image whose dimensions are synchronized with the chart size in the form. The second version allows you to specify custom dimensions via the dimensions parameter. Let's look at the parameters of the function one by one:
-
fileName: String describing the full path to the image file on the disk.
-
dimensions: The dimensions of the generated image in pixels.
-
pixelFormat: The pixel format (color resolution) of the image. To achieve maximum quality of the image, you must use a 24 or 32 bits per pixel format.
-
imageFormat: The format of the image file. Note that the format is determined by this parameter and not by the extension of the passed file name. It is recommended that you pass an image format that corresponds to the extension in order to avoid any confusions. For example, if you export in ImageFormat.png and the file is called “c:\temp\checkpngexport.png”, it will be larger than a png file containing the same image because bmp does not use compression.
-
quality: This parameter is used only when you export JPEG images and controls the quality of the compression. The parameter ranges from 0 to 100, where 0 means lowest quality and 100 means maximum quality. Increasing the image quality will naturally increase the size of the jpeg file.
-
grayscale: When you pass true to this parameter, the component will generate a grayscale image. This can be very useful if you generate images for printers.
The following code snippet illustrates how to work with the SaveToImageFile function:
VB.NET |
|
Dim imageExport As ImageExport = chartControl.ImageExport ' exports a true color, 200x200, png image imageExport.SaveToImageFile("c:\temp\checkpng.png", New Size(200, 200), PixelFormat.Format32bppRgb, ImageFormat.Png, 100, False) ' exports a true color, 200x200, jpg image with small quality imageExport.SaveToImageFile("c:\temp\checkjpg.jpg", New Size(200, 200), PixelFormat.Format32bppRgb, ImageFormat.Png, 50, False) ' exports a grayscale, jpg image with small quality, synchronized with the control dimensions in the form imageExport.SaveToImageFile("c:\temp\checkjpg.jpg", New Size(200, 200), PixelFormat.Format32bppRgb, ImageFormat.Png, 50, False)
|
C# |
|
ImageExport imageExport = ChartControl.ImageExport; // exports a true color, 200x200, png image imageExport.SaveToImageFile("c:\\temp\\checkpng.png", new Size(200, 200), PixelFormat.Format32bppRgb, ImageFormat.Png, 100, false); // exports a true color, 200x200, jpg image with small quality imageExport.SaveToImageFile("c:\\temp\\checkjpg.jpg", new Size(200, 200), PixelFormat.Format32bppRgb, ImageFormat.Png, 50, false); // exports a grayscale, jpg image with small quality, synchronized with the control dimensions in the form imageExport.SaveToImageFile("c:\\temp\\checkjpg.jpg", new Size(200, 200), PixelFormat.Format32bppRgb, ImageFormat.Png, 50, false);
|
See also
ChartControl | ChartServerControl | ImageExport