Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I am trying to copy and save the contents of a Bitmap class image to a *.bmp file but currently receive Unhandled Exceptions during run-time of the console program. I would be grateful to learn where the error lies and how to fix this program. Thank you.

Below is the main part of the code in the Main program:

C++
Bitmap^ imageTest;
String^ path = "C:\\ImageScan.bmp";
imageTest = gcnew Bitmap(path, true); // Actual image to be copied.

Bitmap^ imageTestCopy;
String^ path2 = "C:\\BrainScanCopy2.bmp"; //'blank canvas' identical to imageTest
imageTestCopy = gcnew Bitmap(path2, true);


int x = 0;
int y = 0;

for (x = 0; x < imageTest->Width; x++) // Loop through the image pixels
{
	for (y = 0; y < imageTest->Height; y++)
	{
	        Color pixelColor = imageTest->GetPixel(x, y);
		imageTestCopy->SetPixel(x, y, pixelColor);
	}
}

imageTestCopy->Save("c:\\BrainScanCopy3.bmp"); //save image to new file


What I have tried:

I have tried changing the path and using "imageTestCopy2.bmp" instead of "imageTestCopy3.bmp" when trying to save the image to a file.
Posted
Updated 20-Aug-16 7:29am
Comments
Richard MacCutchan 20-Aug-16 11:13am    
Where do you receive exceptions and what types are they?
binaryoffset 20-Aug-16 11:40am    
The exception was:


An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+.
binaryoffset 20-Aug-16 11:42am    
But I changed the path from "c:\\ImageScanCopy3.bmp" to another folder (which is located inside C:\\) and it works now.
Afzaal Ahmad Zeeshan 20-Aug-16 13:18pm    
What is the unexpected exception that you get?

You dont respect the file format of bmp files. At the beginning is written the bitmap header, than optional some palette (mapping) data and than the pixels. You easily can fetch some information in the wikipedia article about bmp file format.

The legendary article CxImage handles the bitmap and some other graphic formats in an outstanding quality.
 
Share this answer
 
This generic exception is the worst exception that one can get while working with graphics objects. It just simply doesn't provide anything at all.
Quote:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+.

Even if you tried to get the ErrorCode it won't provide anything at all. Secondly, the main thing to understand is that once you load the graphics (bitmap), a lock is put on that file which doesn't allow updating or altering the file. This solution of Hans Passant[^] gives a good overview of using the using block to ensure that the previous bitmaps are properly disposed; you can write a try...catch alternative of that C# program to perform this in your C++ application.

For more, ExternalException Class (System.Runtime.InteropServices)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900