-
August 20th, 2016, 09:45 AM
#1
Copy Save Bitmap Image to file C++
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:
Code:
Bitmap^ imageTest;
String^ path = "C:\\ImageScan.bmp";
imageTest = gcnew Bitmap(path, true); // Actual image to be copied.
Bitmap^ imageTestCopy;
String^ path2 = "C:\\ImageScan2.bmp"; //'blank canvas' identical size 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:\\ImageScanCopy3.bmp"); //save image to new file
-
August 20th, 2016, 10:08 AM
#2
Re: Copy Save Bitmap Image to file C++
[moved from Visual C++ forum]
Victor Nijegorodov
-
August 20th, 2016, 10:13 AM
#3
Re: Copy Save Bitmap Image to file C++
Originally Posted by jamesspears
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.
It would be helpful if you posted the exact Exceptions message.
Did you debug your code? Then you could find the line that caused the exception.
Besides, it is not a good idea to create/write to the file in the root of C: drive. In the most of the systems it is not allowed for the non-admin users.
Victor Nijegorodov
-
August 20th, 2016, 10:46 AM
#4
Re: Copy Save Bitmap Image to file C++
Originally Posted by VictorN
It would be helpful if you posted the exact Exceptions message.
Did you debug your code? Then you could find the line that caused the exception.
Besides, it is not a good idea to create/write to the file in the root of C: drive. In the most of the systems it is not allowed for the non-admin users.
Hello. The run-time error I was getting was:
"An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+."
And this error occurred at the line
Code:
imageTestCopy->Save("c:\\BrainScanCopy3.bmp"); //save image to new file
But I changed the path from "c:\\ImageScanCopy3.bmp" to another (folder is still located inside C:\\) and it works now. Thank you for your help.
-
August 20th, 2016, 11:07 AM
#5
Re: Copy Save Bitmap Image to file C++
You are welcome!
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|