Board index » cppbuilder » LoadFromStream AV

LoadFromStream AV


2004-05-05 05:29:49 PM
cppbuilder76
What would cause the following code to cause an AV when
trying to load the picture into the TImage?
SHFILEINFO FileInfo = {0};
SHGetFileInfo("c:\\HTTPSRV.pas", 0, &FileInfo, sizeof(FileInfo),
SHGFI_SMALLICON | SHGFI_ICON | SHGFI_SYSICONINDEX);
try
{
// assign the icon
TIcon *ico = new TIcon;
ico->Handle = FileInfo.hIcon;
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Width = ico->Width;
bmp->Height = ico->Height;
bmp->Canvas->Draw(0, 0, ico);
TJPEGImage *jpg = new TJPEGImage;
jpg->Assign(bmp);
delete bmp;
delete ico;
// store the bitmap to the stream
TMemoryStream *strm = new TMemoryStream();
jpg->SaveToStream(strm);
delete jpg;
strm->Position = 0;
// crashes on this line with an AV
Image1->Picture->Graphic->LoadFromStream(strm);
delete strm;
}
catch(...)
{ }
I can save the pic to a file, but i can't seem to load it at all.
Thanks
Greg
 
 

Re:LoadFromStream AV

Greg Stantin wrote:
Quote
// crashes on this line with an AV
Image1->Picture->Graphic->LoadFromStream(strm);
delete strm;
}
catch(...)
{ }

Don' know why it AV's.
Quote
I can save the pic to a file, but i can't seem to load it at all.
You could simply draw the Icon on another TCanvas too like:
Image->Picture->Bitmap->Width = Icon->Width;
Image->Picture->Bitmap->Height = Icon->Height;
Image->Picture->Bitmap->Canvas->Draw( 0, 0, Icon );
Hans.
 

Re:LoadFromStream AV

Greg Stantin wrote:
Quote
I can save the pic to a file, but i can't seem to load it at all.
Did you try:
Image->Picture->LoadFromFile ( FileName );
Hans.
 

{smallsort}

Re:LoadFromStream AV

"Greg Stantin" < XXXX@XXXXX.COM >wrote:
Quote
What would cause the following code to cause an AV when
trying to load the picture into the TImage?

SHFILEINFO FileInfo = {0};

SHGetFileInfo("c:\\HTTPSRV.pas", 0, &FileInfo, sizeof(FileInfo),
SHGFI_SMALLICON | SHGFI_ICON | SHGFI_SYSICONINDEX);
SHGetFileInfo( "c:/HTTPSRV.pas", 0, &FileInfo, sizeof(SHFILEINFO), SHGFI_SMALLICON | SHGFI_ICON | SHGFI_SYSICONINDEX);
~ JD
 

Re:LoadFromStream AV

Quote
SHGetFileInfo( "c:/HTTPSRV.pas", 0, &FileInfo, sizeof(SHFILEINFO),
SHGFI_SMALLICON | SHGFI_ICON | SHGFI_SYSICONINDEX);
Unfortuantely it didn't change anything, i get the read of address
00000000 and the av.
 

Re:LoadFromStream AV

Quote
Did you try:

Image->Picture->LoadFromFile ( FileName );
That seems to work, but its not what i am after.
This is still for the web browser, i am playing around
with little snippets of code until i get it to work.
I am trying to figure out how to load a pic from a stream,
so far its not working. It will load from the file, but never from
the stream.
Can you test this code out for me and tell me if it works
for you. Just utilize any file on your drive instead of the .pas
file i have built in.
SHFILEINFO FileInfo = {0};
SHGetFileInfo("c:\\HTTPSRV.pas", 0, &FileInfo, sizeof(SHFILEINFO),
SHGFI_SMALLICON | SHGFI_ICON | SHGFI_SYSICONINDEX);
try
{
// assign the icon
TIcon *ico = new TIcon;
ico->Handle = FileInfo.hIcon;
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Width = ico->Width;
bmp->Height = ico->Height;
bmp->Canvas->Draw(0, 0, ico);
TJPEGImage *jpg = new TJPEGImage;
jpg->Assign(bmp);
delete bmp;
delete ico;
// store the bitmap to the stream
TMemoryStream *strm = new TMemoryStream();
jpg->SaveToStream(strm);
// strm->SaveToFile("c:\\pic.jpg");
delete jpg;
strm->Position = 0;
Image1->Picture->Graphic->LoadFromStream(strm);
// Image1->Picture->LoadFromFile("c:\\pic.jpg");
delete strm;
}
catch(...)
{ }
 

Re:LoadFromStream AV

Hi Greg
"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
What would cause the following code to cause an AV when
trying to load the picture into the TImage?

strm->Position = 0;
I don't know if this makes a difference, but try using the Seek () method of
TMemoryStream rather
than the Position property.
strm->Seek (0, soFromBeginning);
Theoretically it should achieve the same thing but there may be some extra
stuff that it takes care of
that changing Position doesn't.
Quote


Ciao
Bill
 

Re:LoadFromStream AV

Quote
Theoretically it should achieve the same thing but there may be some extra
stuff that it takes care of
that changing Position doesn't.
Hi Bill, thanks for the snip, i tried it but still nothing. This makes no
sense,
nothing seems to work for me anymore, maybe i need a new pc :)
Greg
 

Re:LoadFromStream AV

Greg Stantin wrote:
Quote
Can you test this code out for me and tell me if it works
for you. Just utilize any file on your drive instead of the .pas
file i have built in.
Image1->Picture->Graphic->LoadFromStream(strm);
// Image1->Picture->LoadFromFile("c:\\pic.jpg");
The sequence:
Image1->Picture->Graphic->LoadFromStream(strm);
Image1->Picture->LoadFromFile("c:\\pic.jpg");
will produce an AV where if you change it then it will not AV.
Image1->Picture->LoadFromFile("c:\\pic.jpg");
Image1->Picture->Graphic->LoadFromStream(strm);
But then it will jot looad also.
If you save the bmp to a stream, then you also need a
LoadFromFile ( ...bmp ) first. But then it does load from
stream.
So what stays is preparing it for the jpg.
I will try.
Hans.
 

Re:LoadFromStream AV

Hello again.
I think I missed something the first time I looked at this.
"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
What would cause the following code to cause an AV when
trying to load the picture into the TImage?


// crashes on this line with an AV
Image1->Picture->Graphic->LoadFromStream(strm);
Graphic is probably undefined at this point. You likely need something
along the lines:
grph = new TGraphic ();
grph->LoadFromStream (strm);
Image1->Picture->Assign (grph);
delete grph;
This may not be exactly correct, but I think that it is a starting point.
Ciao
Bill
 

Re:LoadFromStream AV

Greg Stantin wrote:
Quote
I am trying to figure out how to load a pic from a stream,
so far its not working. It will load from the file, but never from
the stream.
There is a solution.
Quote
TJPEGImage *jpg = new TJPEGImage;
jpg->Assign(bmp);
delete bmp;
You should first realise that if you now excecuted the following
statement, that the jpg was assigned.
Image1->Picture->Bitmap->Assign ( jpg ); // OK.
But you want to load from stream ? First load the stream in
a new TJPEGImage and then use this Assign:
TJPEGImage *jpeg = new TJPEGImage;
jpeg->LoadFromStream ( strm );
Image1->Picture->Bitmap->Assign ( jpeg );
delete jpeg;
Maybe there is a more elegant way. But until you find one,
you can use this.
Hans.
 

Re:LoadFromStream AV

"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
// crashes on this line with an AV
Image1->Picture->Graphic->LoadFromStream(strm);
Did you verify that the Graphic is not NULL at the time?
Why not just assign the TJPEGImage to the TImage directly and not use the
stream at all?
TJPEGImage *jpg = new TJPEGImage;
try
{
jpg->Assign(bmp);
Image1->Picture->Graphic = jpg;
}
__finally {
delete jpg;
}
Gambit
 

Re:LoadFromStream AV

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message
Quote
grph = new TGraphic ();
TGraphic is an abstract base class, you can't instantiate it directly.
Gambit
 

Re:LoadFromStream AV

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message
news:4099140e$ XXXX@XXXXX.COM ...

>grph = new TGraphic ();

TGraphic is an abstract base class, you can't instantiate it directly.

Right. I fogot about that.
However, I think the problem is related to the fact that
Image1->Picture->Graphic
is probably NULL, so a descendant of TGraphic needs to be created somewhere
and
passed to Image1->Picture->Assign ().
Ciao
Bill
 

Re:LoadFromStream AV

Quote
Why not just assign the TJPEGImage to the TImage directly and not use the
stream at all?
I can't do it like that. This is to help me understand why the webserver
images
is not working. So i need to see how the pics are loaded when
LoadFromStream
is used.
What i need to do is grab the icon of the specific file
Convert the icon to bitmap
Assign the bitmap to a jpg
Then load the image from stream.