I'm converting PDF-pages in small bitmaps for a DataGridView. I convert each page into a bitmap and assign it to a newly created bitmap inside a using() {}, like can be read in the code.
I get an 'Invalid Parameter' as soon as I want to assign the thumbnail to a DataGridView cell within the using() {}. When I remove the using() {}, I don't get the error. But I don't want to remove the using() {} because I want the (each) Bitmap disposed of properly.
I've been searching all over the internet, mostly being referred to SO, and I've read many posts, but none of the problems seems to be mine and I've not found a solution so far.
I've removed the using() and replaced it with a thumbnail.Dispose() after I used it in the copy operation, which gives (naturally) exactly the same problem.
I don't have the problem if I don't dispose(), but then I'm leaving it up to the GB to clean up, which is not the right way (I also read on SO).
EDIT: added the for loop which the pieces of code are in. (Nevermind the numberOfThumbnails variable, I need that later for other purposes.)
EDIT2: I think I found the solution. It was always the second time I referred to the thumbnail variable that the exception occurred. So, somehow the same variable was used, regardless the 'Bitmap thumbnail = ...' statement within the using(...). When I changed the assignment to a 'new Bitmap(thumbnail);', the problem was gone, see variant five. I now remain in doubt if this change again creates a problem, because I cannot explicitly dispose of this new Bitmap... Can someone please explain why this works and if this is safe to use (without the disposal of the newly created Bitmap)?
// Variant one:
int numberOfThumbnails = 0;
for (int pageNumber = 1; pageNumber <= PageCount; pageNumber++)
{
using (Bitmap thumbnail = pdfDocument.PageToBitmap(pagenumber, width, height))
{
(row, col) = RowColFromLinearCellNumber(numberOfThumbnails++);
if (col == 0)
Rows.Add(thumbnail);
else
Rows[row].Cells[col].Value = thumbnail; // ERROR: Invalid Parameter
}
}
// Variant two:
int numberOfThumbnails = 0;
for (int pageNumber = 1; pageNumber <= PageCount; pageNumber++)
{
using (Bitmap thumbnail = new Bitmap(pdfDocument.PageToBitmap(pagenumber, width, height)))
{
(row, col) = RowColFromLinearCellNumber(numberOfThumbnails++);
if (col == 0)
Rows.Add(thumbnail);
else
Rows[row].Cells[col].Value = new Bitmap(thumbnail); // ERROR: Invalid Parameter
}
}
// Variant three:
int numberOfThumbnails = 0;
for (int pageNumber = 1; pageNumber <= PageCount; pageNumber++)
{
Bitmap thumbnail = new Bitmap(pdfDocument.PageToBitmap(pagenumber, width, height)))
(row, col) = RowColFromLinearCellNumber(numberOfThumbnails++);
if (col == 0)
Rows.Add(thumbnail);
else
Rows[row].Cells[col].Value = new Bitmap(thumbnail); // ERROR: Invalid Parameter
thumbnail.Dispose();
}
// Variant four:
int numberOfThumbnails = 0;
for (int pageNumber = 1; pageNumber <= PageCount; pageNumber++)
{
Bitmap thumbnail = new Bitmap(pdfDocument.PageToBitmap(pagenumber, width, height)))
(row, col) = RowColFromLinearCellNumber(numberOfThumbnails++);
if (col == 0)
Rows.Add(thumbnail);
else
Rows[row].Cells[col].Value = new Bitmap(thumbnail); // NO error, but no disposal of Bitmap as well...
}
// Variant five:
int numberOfThumbnails = 0;
for (int pageNumber = 1; pageNumber <= PageCount; pageNumber++)
{
using (Bitmap thumbnail = pdfDocument.PageToBitmap(pagenumber, width, height))
{
(row, col) = RowColFromLinearCellNumber(numberOfThumbnails++);
if (col == 0)
Rows.Add(new Bitmap(thumbnail));
else
Rows[row].Cells[col].Value = new Bitmap(thumbnail); // No error anymore
}
}
I expect all variants to work (functionally, ignoring lack of disposal), but in the first three variants I get the 'Invalid Parameter' error on the line where I want to put the thumbnail in the cell of the DataGridView.
I've spend many hours on this issue and I'm out of resources. I really hope somebody can point at some mistake I'm overlooking. Or maybe someone who ran into the same problem and found a good work-around.