| View previous topic :: View next topic |
| Author |
Message |
xa Guest
|
Posted: Mon May 16, 2005 12:58 pm Post subject: Picture LoadFromFile |
|
|
I am working with some websites that stores both .gif and .jpg image files
as the .img extension and when i try to use the loadfromfile function i get
an exception...
Is there anyway to use the loadfromfile into the image component or will i
have to use a try and catch into a gifImage or jpegimage first and then
assign the bitmap to the image..
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon May 16, 2005 5:33 pm Post subject: Re: Picture LoadFromFile |
|
|
"xa" <pageme (AT) ntlworld (DOT) com> wrote
| Quote: | I am working with some websites that stores both .gif and .jpg
image files as the .img extension and when i try to use the loadfromfile
function i get an exception...
|
As well you should be. TPicture does not recognize .img files at all, and
it also does not analyze the actual file contents to determine which format
to actually load (I already reported QC # 12434 asking for this feature).
| Quote: | Is there anyway to use the loadfromfile into the image component
|
The only ways to make LoadFromFile() work with .img files is to either:
1) save the data to the hard drive, then analyze the file data to decide
which format is present, then change the file's extension accordingly before
then calling LoadFromFile().
2) write your own TGraphic descendant class to handle .img files and then
register it via TPicture::RegisterFileFormat(). Inside the class, you can
open and analyze the file data, then instantiate the appropriate TGraphic
class for it and delegate all of your own class's virtual methods to call
into the instantiated TGraphic class to perform the actual work.
| Quote: | or will i have to use a try and catch into a gifImage or jpegimage first
and then assign the bitmap to the image..
|
A better way would be to look at the actual file contents yourself to
determine which format is actually present, and then you can instantiate and
load the appropriate TGraphic class with the data and then finally assign
that to the TImage directly.
Gambit
|
|
| Back to top |
|
 |
xa Guest
|
Posted: Tue May 17, 2005 2:25 am Post subject: Re: Picture LoadFromFile |
|
|
Given that i know the site only allows gifs or jpgs i went with this
afterwards which seems to have worked fine..
int __fastcall TMainForm::RetImageType(AnsiString image)
{
int res = 3;
std::auto_ptr <TJPEGImage> Jpeg(new TJPEGImage);
TGIFImage *Gif = new TGIFImage;
if(FileExists(image)){
try{
Jpeg->LoadFromFile(image);
res = 1;
}
catch(EInvalidGraphic &E){
try{
Gif->LoadFromFile(image);
res = 2;
}
catch(EInvalidGraphic &E){
res = 3;
}
}
}
delete Gif;
return res;
}
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue May 17, 2005 7:36 am Post subject: Re: Picture LoadFromFile |
|
|
"xa" <pageme (AT) ntlworld (DOT) com> wrote
| Quote: | Given that i know the site only allows gifs or jpgs i went with
this afterwards which seems to have worked fine..
|
As I mentioned earlier, it is better to analyze the actual file data instead
of trying to load every format until one eventually succeeds. For example:
int __fastcall TMainForm::RetImageType(const AnsiString &image)
{
int iFile = FileOpen(image, fmOpenRead | fmShareDenyWrite);
if( iFile != -1 )
{
BYTE buffer[3];
int iRead = FileRead(iFile, buffer, 3);
FileClose(iFile);
if( iRead > 2 )
{
// JPG Start-of-Image marker?
if( (buffer[0] == 0xFF) && (buffer[1] == 0xD8) )
return 1;
// first half of GIF signature?
if( (buffer[0] == 'G') && (buffer[1] == 'I') && (buffer[2]
== 'F') )
return 2;
}
}
// unknown format
return 3;
}
Gambit
|
|
| Back to top |
|
 |
|