Skip to content

Commit e065f50

Browse files
Merge pull request #500 from telerik/dess-image-utils
Update create-custom-jpeg-image-converter-net-standard.md
2 parents 2cbc9aa + 847f7d8 commit e065f50

File tree

1 file changed

+68
-34
lines changed

1 file changed

+68
-34
lines changed

knowledge-base/create-custom-jpeg-image-converter-net-standard.md

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ res_type: kb
1919
</thead>
2020
<tbody>
2121
<tr>
22-
<td>2023.1.315</td>
22+
<td>up to 2023.1.410</td>
2323
<td>RadPdfProcessing</td>
2424
<td rowspan="2" ><a href="https://www.telerik.com/blogs/author/martin-velikov">Martin Velikov</a></td>
2525
</tr>
@@ -32,66 +32,100 @@ res_type: kb
3232

3333
## Description
3434

35-
**.NET Standard** specification does not define APIs for converting images or scaling their quality. That is why to export to PDF format a document containing images different than Jpeg and Jpeg2000 or ImageQuality different than High, you will need to provide an implementation of the **JpegImageConverterBase** abstract class. This implementation should be passed to the **JpegImageConverter** property of the **FixedExtensibilityManager**.
35+
**.NET Standard** specification does not define APIs for converting images or scaling their quality. That is why, when inserting images in a PDF document different than Jpeg and Jpeg2000 or ImageQuality different than High, you will need to implement the **JpegImageConverterBase** abstract class. This implementation should be passed to the **JpegImageConverter** property of the **FixedExtensibilityManager**.
36+
37+
>important With the [R2 2023 changes](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/changes-and-backward-compatibility/backward-compatibility#whats-different-in-2023-r2) SkiaSharp replaced ImageSharp as the required dependency.
3638
3739
## Solution
3840

39-
The following code snippets demonstrate how to create a custom implementation of the JpegImageConverterBase abstract class using the [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp) library and set it to the JpegImageConverter property of the FixedExtensibilityManager. We are using the ImageSharp library to convert the images from one of the library's supported formats to Jpeg and to change their quality if it is set.
41+
The following code snippets demonstrate how to create a custom implementation of the JpegImageConverterBase abstract class using the [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp) library and set it to the JpegImageConverter property of the FixedExtensibilityManager. We are using the ImageSharp library to convert the images from one of the library's supported formats to Jpeg and to change their quality if it is set. Note that this approach is valid up to version 2023.1.410 of RadPdfProcessing.
4042

4143
#### __[C#] Create a custom implementation inheriting the JpegImageConverterBase abstract class__
4244

4345
{{region kb-create-custom-jpeg-image-converter1}}
4446

45-
internal class JpegImageConverter : JpegImageConverterBase
46-
{
47-
public override bool TryConvertToJpegImageData(byte[] imageData, ImageQuality imageQuality, out byte[] jpegImageData)
47+
public class CustomJpegImageConverter : JpegImageConverterBase
4848
{
49-
try
49+
public override bool TryConvertToJpegImageData(byte[] imageData, ImageQuality imageQuality, out byte[] jpegImageData)
5050
{
51-
IImageFormat imageFormat;
52-
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData, out imageFormat))
51+
try
5352
{
54-
if (imageFormat.GetType() == typeof(PngFormat))
53+
IImageFormat imageFormat;
54+
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData, out imageFormat))
5555
{
56-
image.Mutate(x => x.BackgroundColor(Color.White));
57-
}
56+
if (imageFormat.GetType() == typeof(PngFormat))
57+
{
58+
image.Mutate(x => x.BackgroundColor(Color.White));
59+
}
5860

59-
JpegEncoder options = new JpegEncoder
60-
{
61-
Quality = (int)imageQuality,
62-
};
61+
JpegEncoder options = new JpegEncoder
62+
{
63+
Quality = (int)imageQuality,
64+
};
6365

64-
using (MemoryStream ms = new MemoryStream())
65-
{
66-
image.SaveAsJpeg(ms, options);
66+
using (MemoryStream ms = new MemoryStream())
67+
{
68+
image.SaveAsJpeg(ms, options);
6769

68-
jpegImageData = ms.ToArray();
70+
jpegImageData = ms.ToArray();
71+
}
72+
6973
}
70-
}
7174

72-
return true;
73-
}
74-
catch (Exception ex)
75-
{
76-
if (ex is UnknownImageFormatException || ex is ImageProcessingException)
77-
{
78-
jpegImageData = null;
79-
return false;
75+
return true;
8076
}
81-
else
77+
catch (Exception ex)
8278
{
83-
throw ex;
79+
if (ex is UnknownImageFormatException || ex is ImageProcessingException)
80+
{
81+
jpegImageData = null;
82+
return false;
83+
}
84+
else
85+
{
86+
throw ex;
87+
}
8488
}
8589
}
8690
}
87-
}
8891

8992
{{endregion}}
9093

9194
#### __[C#] Set the custom implementation to the JpegImageConverter property of the FixedExtensibilityManager__
9295

9396
{{region kb-create-custom-jpeg-image-converter2}}
9497

95-
JpegImageConverterBase customJpegImageConverter = new CustomJpegImageConverter();
96-
FixedExtensibilityManager.JpegImageConverter = customJpegImageConverter;
98+
JpegImageConverterBase customJpegImageConverter = new CustomJpegImageConverter();
99+
FixedExtensibilityManager.JpegImageConverter = customJpegImageConverter;
100+
101+
// RadPdfProcessing version up to 2023.1.410
102+
PdfFormatProvider provider = new PdfFormatProvider();
103+
string imageFolderPath = @"..\..\..\images";
104+
string[] imageFiles = Directory.GetFiles(imageFolderPath);
105+
RadFixedDocument fixedDocument = new RadFixedDocument();
106+
RadFixedDocumentEditor documentEditor = new RadFixedDocumentEditor(fixedDocument);
107+
108+
foreach (string imageFilePath in imageFiles)
109+
{
110+
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open);
111+
Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource _imageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(fileStream);
112+
documentEditor.InsertImageInline(_imageSource);
113+
documentEditor.InsertLineBreak();
114+
}
115+
116+
documentEditor.Dispose();
117+
string outputFilePath = @"output.pdf";
118+
File.Delete(outputFilePath);
119+
using (Stream output = File.OpenWrite(outputFilePath))
120+
{
121+
provider.Export(fixedDocument, output);
122+
}
123+
124+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
125+
97126
{{endregion}}
127+
128+
129+
# See Also
130+
131+
- [Cross platform >> Images]({%slug radpdfprocessing-cross-platform-images%})

0 commit comments

Comments
 (0)