CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2016
    Posts
    2

    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

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Copy Save Bitmap Image to file C++

    [moved from Visual C++ forum]
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Copy Save Bitmap Image to file C++

    Quote Originally Posted by jamesspears View Post
    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

  4. #4
    Join Date
    Aug 2016
    Posts
    2

    Re: Copy Save Bitmap Image to file C++

    Quote Originally Posted by VictorN View Post
    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.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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
  •  





Click Here to Expand Forum to Full Width

Featured