Solved

loading bmp from file to TImage

Posted on 2002-04-20
2
604 Views
Last Modified: 2010-05-18
I am using the code below to try and load a filepath to a bmp file from a txt file. It compiles fine but when I get to the LoadFromFile stmt I get a runtime error "EFOpenError Cannot open file" and then it displays the path it should open so it is reading it correctly. I am using Borland C++ Builder.  

Can anyone tell me where I have gone wrong please


ifstream in ("Test.txt");    
char s[_MAX_PATH];

if( ! in )
{
  Application->MessageBox("Cannot open file.",
   "MessageBox",MB_OK + MB_DEFBUTTON1);
}
else
{
  Graphics::TBitmap *Bitmap1 = new Graphics::TBitmap();
  in.getline(s,_MAX_PATH);
  Bitmap1->LoadFromFile(s);
  Image1->Picture->Bitmap = Bitmap1;
}
in.close();
0
Comment
Question by:sylvrynne
2 Comments
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 100 total points
Comment Utility
Maybe, s contains some spaces before or (after)image file. For example,  s = "   c:\x.bmp" is wrong filename

For sure, the filename is correct, you can use access(...) . The following is demo code

    ifstream in ("Test.txt");
    char s[_MAX_PATH];

    if( ! in )
    {
        Application->MessageBox("Cannot open file.",
            "MessageBox",MB_OK + MB_DEFBUTTON1);
    }
    else
    {
        Graphics::TBitmap *Bitmap1 = new Graphics::TBitmap();
        in.getline(s,_MAX_PATH);
        if( access(s,0) != 0 )
        {
            String msg = String("\"") + s + "\"";
            Application->MessageBox("Cannot open image.",
                msg.c_str(),MB_OK );
            return;
        }

        Bitmap1->LoadFromFile(s);
        Image1->Picture->Bitmap = Bitmap1;
    }
    in.close();

0
 

Author Comment

by:sylvrynne
Comment Utility
You will never believe what it was ... the path had " " marks around it .... just too late last night to see it in the text file  thanx for your help
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now