Board index » cppbuilder » Loading a picture from file

Loading a picture from file


2004-07-02 06:35:31 PM
cppbuilder98
Dear all,
I have a TImage control on my form and I would like to load
into it a jpg image at runtime. I have used the following code
to do this:
Form ->Logo ->Picture ->LoadFromFile (FileName)
where "Logo" is the name of my control. At runtime, when the
above code is executed, I get an "Access violation" exception.
Why ? Could you suggest me a way to load an image at runtime ?
Best Regards
/Alessandro Strazzero
 
 

Re:Loading a picture from file

Hi,
i use this code to do something similar:
Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
pBitmap->LoadFromFile(opt.WorkDir+"\\data\\logo.bmp");
pBitmap->Transparent = true;
pBitmap->TransparentColor = clWhite;
logo->Canvas->Draw(0,0,pBitmap);
pBitmap->TransparentMode = tmAuto;
logo->Canvas->Draw(0,0,pBitmap);
pBitmap->Free();
this code is placed in the OnResize EventHandler of a form, so it's
executed many times. It work very well...
hope to be helpful,
Daniele
Alessandro Strazzero ha scritto:
Quote
Dear all,

I have a TImage control on my form and I would like to load
into it a jpg image at runtime. I have used the following code
to do this:

Form ->Logo ->Picture ->LoadFromFile (FileName)

where "Logo" is the name of my control. At runtime, when the
above code is executed, I get an "Access violation" exception.
Why ? Could you suggest me a way to load an image at runtime ?

Best Regards

/Alessandro Strazzero
 

Re:Loading a picture from file

"Alessandro Strazzero" < XXXX@XXXXX.COM >wrote:
Quote
[...] Could you suggest me a way to load an image at runtime ?
The VCL has a TJPEGImage class for that purpose.
Simply #include "jpeg.hpp" and then create a TJPEGImage
instance when needed. Despite its name, TJPEGImage is not a
component. It is a standalone parser class, like TBitmap.
TJPEGImage integrates with TPicture so that TImage,
TOpenPictureDialog, and TSavePictureDialog can natively
display jpg images.
~ JD
 

{smallsort}

Re:Loading a picture from file

Quote
where "Logo" is the name of my control. At runtime, when the
above code is executed, I get an "Access violation" exception.
Why ? Could you suggest me a way to load an image at runtime ?
You can't load a JPG into a TImage directly. Try this instead:
..
#include <jpeg.hpp>
..
TJPEGImage *J = new TJPEGImage();
J->LoadFromFile(FileName);
Logo->AutoSize = true;
Logo->Picture->Assign(J);
delete J;
HTH,
Steve.
 

Re:Loading a picture from file

Alessandro Strazzero wrote:
Quote
I have a TImage control on my form and I would like to load
into it a jpg image at runtime. I have used the following code
to do this:

Form ->Logo ->Picture ->LoadFromFile (FileName)

where "Logo" is the name of my control. At runtime, when the
above code is executed, I get an "Access violation" exception.
Why ? Could you suggest me a way to load an image at runtime ?
The code is ok.
(Except for making it unreadable by changing the borland given name ).
But you probably forgot to include a headerfile.
#include <jpeg.hpp>
Form1->Image1->Picture->LoadFromFile ( FileName );
If you use bcb3 you need to do more.
Hans.
 

Re:Loading a picture from file

Quote
You can't load a JPG into a TImage directly.
Sorry, sure you can. Hans is right, only the header is missing. Anyway
the exception you get should be EInvalidGraphic, not "Access
violation" otherwise the problem could be somewhere else.
Steve.
 

Re:Loading a picture from file

"Alessandro Strazzero" < XXXX@XXXXX.COM >wrote in message
Quote
At runtime, when the above code is executed, I get
an "Access violation" exception.Why ?
Where exactly are you calling the code from? The location of your code can
make a difference, especially since you are accessing the form via a global
pointer. If you are calling the code in the form's constructor, for
instance, then that pointer has not been assigned yet so you ould be
accessing an invalid memory address (which is what an Access Violation
specifically warns you about).
Gambit
 

Re:Loading a picture from file

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
The VCL has a TJPEGImage class for that purpose.
That is not hte cause of the problem. Had Alessandro not already had
TJPEGImage enabled in his code, calling LoadFromFile() with a jpg file would
produce a very different error altogether.
Gambit
 

Re:Loading a picture from file

"Steve Aletto" < XXXX@XXXXX.COM >wrote in message
Quote
You can't load a JPG into a TImage directly.
Yes, you can. Simply including jpeg.hpp into your code automatically
enabled TJPEGImage support in TPicture, thus TPicture (and everything that
uses TPicture, such as TImage) can load jpg images directly.
Gambit