Board index » cppbuilder » Bitmap first time load problem

Bitmap first time load problem

Hey,

I am having (what I think is) a rather strange problem using the TBitmap
object with BCB.  I am trying to load a bitmap into memory and then
stretch it into another canvas.  No big deal here.  My problem is that
the following code doesn;t work on the first time through.  If I try
to load the same image (or any image) a second time, everything is cool
from that point on.

Does anyone have any idea what I am doing wrong?? I have a feeling that
I am forgeting to init SOMETHING somewhere, but I can't find it.

The code :

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
        {
        Graphics::TBitmap *MyBitmap;
        MyBitmap = new Graphics::TBitmap();
        MyBitmap->LoadFromFile(OpenDialog1->FileName);

        TRect MyRect;
        MyRect.Top = 0;
        MyRect.Left = 0;
        MyRect.Right = Image1->Picture->Width;
        MyRect.Bottom = Image1->Picture->Height;

        Image1->Canvas->StretchDraw(MyRect, MyBitmap);
        }

Quote
}

// end code.

Thanx for your time,

dv

 

Re:Bitmap first time load problem


Quote
David E. Vandewalle wrote:

> Hey,

> I am having (what I think is) a rather strange problem using the TBitmap
> object with BCB.  I am trying to load a bitmap into memory and then
> stretch it into another canvas.  No big deal here.  My problem is that
> the following code doesn;t work on the first time through.  If I try
> to load the same image (or any image) a second time, everything is cool
> from that point on.

> Does anyone have any idea what I am doing wrong?? I have a feeling that
> I am forgeting to init SOMETHING somewhere, but I can't find it.

> The code :

> void __fastcall TForm1::BitBtn1Click(TObject *Sender)
> {
> if(OpenDialog1->Execute())
>         {
>         Graphics::TBitmap *MyBitmap;
>         MyBitmap = new Graphics::TBitmap();
>         MyBitmap->LoadFromFile(OpenDialog1->FileName);

>         TRect MyRect;
>         MyRect.Top = 0;
>         MyRect.Left = 0;
>         MyRect.Right = Image1->Picture->Width;
>         MyRect.Bottom = Image1->Picture->Height;

>         Image1->Canvas->StretchDraw(MyRect, MyBitmap);
>         }

> }

> // end code.

> Thanx for your time,

> dv

Hi David. In your code I don't see anywhere this statement

delete MyBitmap;

Because what I believe is happening is that your creating many instances
of a TBitmap in memory with the same name and not deleting it when
you're done. I don't know if this might actually be affecting your code,
but I remember writing a program once with almost the exact statements
as yours and had no problem but, I did delete all the instances I
created once I was done.
                                                regards Emmanuel

Re:Bitmap first time load problem


Quote
Emmanuel Vlamakis wrote:

> Hi David. In your code I don't see anywhere this statement

> delete MyBitmap;

> Because what I believe is happening is that your creating many instances
> of a TBitmap in memory with the same name and not deleting it when
> you're done. I don't know if this might actually be affecting your code,
> but I remember writing a program once with almost the exact statements
> as yours and had no problem but, I did delete all the instances I
> created once I was done.
>                                                 regards Emmanuel

Whoops.  I actually did have the delete code in my main program and just
forgot to cut/paste
it over when I simplified it for posting.  With the deletes in there, it
still behaves exactly the same.  Argh!  

Thanx

dv

Re:Bitmap first time load problem


Quote
> still behaves exactly the same.  Argh!

> Thanx

> dv

Hey David,
  Here's a solution to your problem. In order for this to work properly
set the Image1 property AutoSize to false and the Stretch property to
true first. Then write this code in the function where you wanted to
load your bitmap.

  if(OpenDialog1->Execute())
  {
        Graphics::TBitmap *MyBitmap;
        MyBitmap = new Graphics::TBitmap();
        MyBitmap->LoadFromFile(OpenDialog1->FileName);

//        TRect MyRect;
//        MyRect.Top = 0;
//        MyRect.Left = 0;
//        MyRect.Right = Image1->Picture->Width;
//        MyRect.Bottom = Image1->Picture->Height;
        Image1->Picture->Bitmap = MyBitmap;
  }

Re:Bitmap first time load problem


Quote
> still behaves exactly the same.  Argh!

> Thanx

> dv

Hey David,
  Here's a solution to your problem. In order for this to work properly
set the Image1 property AutoSize to false and the Stretch property to
true first. Then write this code in the function where you wanted to
load your bitmap.

  if(OpenDialog1->Execute())
  {
        Graphics::TBitmap *MyBitmap;
        MyBitmap = new Graphics::TBitmap();
        MyBitmap->LoadFromFile(OpenDialog1->FileName);

//        TRect MyRect;
//        MyRect.Top = 0;
//        MyRect.Left = 0;
//        MyRect.Right = Image1->Picture->Width;
//        MyRect.Bottom = Image1->Picture->Height;
        Image1->Picture->Bitmap = MyBitmap;
  }

I hope this helps.
                                                regards Emmanuel

Other Threads