Creating a TImage dynamically

Anybody knows how to dynamically create a TImage (type TBitmap)? I have been trying this for a long time, but failed every time. I want a workable TImage without using a TImage put on a TForm (which I always do as a workaround). Anybody an idea?

[code]
//Own scope for creating a TImage dynamically
{
const int width = 256;
const int height = 256;
std::auto_ptr image(new TImage(0));
image->Picture->Bitmap = (Graphics::TBitmap*) CreateCompatibleBitmap(Handle,width,height);
//image->Picture->Bitmap = (Graphics::TBitmap*) CreateBitmap(width,height,3,8,(void*)new char[width*height*3]);

assert(image->Picture != 0);
assert(image->Picture->Bitmap != 0);
assert(image->Picture->Graphic != 0);
assert(image->Picture->Graphic->Palette == 0); //Would this be it?

image->Picture->Bitmap->Width = width;
image->Picture->Bitmap->Height = height;
image->Picture->Graphic->Width = width;
image->Picture->Graphic->Height = height;
image->Width = width;
image->Height = height;
image->AutoSize = true;
image->Refresh();
for (int y=0; y!=height; ++y)
{
unsigned char * line=static_cast(image->Picture->Bitmap->ScanLine[y]);
for (int x=0; x!=width; ++x)
{
line[x*3+0]= x; //Blue
line[x*3+1]= y; //Green
line[x*3+2]= x+y; //Red
}
}
//Save the image
image->Picture->SaveToFile("temp.bmp");
}

//Load the image
Image1->Picture->LoadFromFile("temp.bmp");

[/code]

Hope anyone knows the trick!
See ya,
bilderbikkel

Comments

  • Im not sure if i understood you, but can't you do like this?

    TImage *MyImage;
    MyImage = new TImage(this);
    MyImage->Parent = Form1;
    MyImage->Left = 0;
    MyImage->Top = 0;
    MyImage->Width = 100;
    MyImage->Height = 50;
    // etc etc...

    P.S. Or was that the way you were doing as a workaround..
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion