Hi Team,
I am currently developing an application that renders a PDF to a display using JPEG format. However, we are encountering an exception when running the application.
Previously, the rendering function worked correctly when using a PNG device. To improve performance, we decided to switch to JPEG, but this change has resulted in the application failing.
Below, you will find the code snippet and a screenshot of the exception:
public void Aspose_RenderPagetoHDC(int pageID, IntPtr hdc, int width, int height)
{
Aspose.Pdf.Page page = pageDictionary[pageID].Item2;
Stream stream = pageDictionary[pageID].Item1;
Document pdfDocument = new Document(stream);
Aspose.Pdf.Page pdfPage;
try
{
pdfPage = pdfDocument.Pages[1];
}
catch (Exception ex)
{
return;
}
// Create a resolution object
Resolution resolution = new Resolution(72); // 300 DPI is a common resolution
// Create a MemoryStream to hold the rendered page
using (MemoryStream memStream = new MemoryStream())
{
// Create a PngDevice with the specified width, height, and resolution
//PngDevice pngDevice = new PngDevice(width, height, resolution);
JpegDevice jpegDevice = new JpegDevice(resolution);
try
{
// Render the page to the MemoryStream
//Note: This pngDevice.Process api works only with System.Text.Encoding dll version 5.0.0.0 (depricated)and not latest version
//pngDevice.Process(page, memStream);
jpegDevice.Process(pdfPage, memStream);
}
catch (Exception ex)
{
// Log any exceptions
return;
}
// Create a Bitmap from the MemoryStream
using (Bitmap gdiBitmap = new Bitmap(memStream))
{
Graphics graphics;
try
{
graphics = Graphics.FromHdc(hdc);
}
catch (Exception ex)
{
return;
}
// Create a Graphics object from the HDC handle
using (graphics)
{
// Draw the bitmap onto the HDC
graphics.DrawImage(gdiBitmap, 0, 0, width, height);
}
}
}
}
image.png (7.3 KB)
Any input would be a great help.
Regards,
Ramya.B