I'm trying to crop a PDF page using this code, but after 3 crops GdViewer don't show any image and sometimes shows me an OutOfMemory error message.
Code: Select all
private void executeCrop(object parameter)
{
if (_gdViewer.MouseMode == ViewerMouseMode.MouseModeAreaSelection)
{
double left = 0;
double top = 0;
double width = 0;
double height = 0;
using (GdPictureImaging _image = new GdPictureImaging())
{
int tempImage = _gdPicturePDF.RenderPageToGdPictureImage(TIFF_DPI, false);
_gdViewer.DisplayFromGdPictureImage(tempImage);
_gdViewer.GetRectCoordinatesOnDocumentPixel(ref left, ref top, ref width, ref height);
if (width > 0 && height > 0)
{
if (_image.Crop(tempImage, (int)left, (int)top, (int)width, (int)height) != GdPictureStatus.OK)
{
MessageBox.Show("Error: " + _image.GetStat());
return;
}
replacePDFPage((float)width, (float)height, tempImage);
_gdViewer.ClearRect();
}
_image.ReleaseGdPictureImage(tempImage);
}
}
}
private void replacePDFPage(float width, float height, int tempImage)
{
int pageNo = _gdPicturePDF.GetCurrentPage();
//remove the original page from the pdf file
_gdPicturePDF.DeletePage(pageNo);
//insert a new, blank page
GdPictureStatus status;
if (_gdPicturePDF.GetPageCount() == 0)
{
if (_gdPicturePDF.NewPDF() == GdPictureStatus.OK)
{
status = _gdPicturePDF.NewPage(width, height);
}
}
else
{
status = _gdPicturePDF.InsertPage(width, height, pageNo);
}
//add the cropped image as a resource to the pdf
string imageResx = _gdPicturePDF.AddImageFromGdPictureImage(tempImage, false, false);
//draw it on the page
if (_gdPicturePDF.GetStat() == GdPictureStatus.OK)
{
_gdPicturePDF.DrawImage(imageResx, 0, 0, width, height);
}
_image.ReleaseGdPictureImage(tempImage);
_gdViewer.DisplayFromGdPicturePDF(_gdPicturePDF);
}