Board index » cppbuilder » Drawing on bitmap

Drawing on bitmap


2004-05-01 03:22:26 PM
cppbuilder3
It's required to draw pixels on TBitmap.
I've put TImage component on the Form, created Picture:
Image->Picture = new TPicture();
But after the operation
Image->Picture->Bitmap->Canvas->Pixels[x][y] = clRed;
form remains empty.
I know that I do this operation simply using
Image->Canvas->Pixels[x][y] = clRed;
Why? And how to do it correctly?
Thank you in advance.
 
 

Re:Drawing on bitmap

XXXX@XXXXX.COM wrote:
Quote
It's required to draw pixels on TBitmap.

I've put TImage component on the Form, created Picture:
Image->Picture = new TPicture();
WHy did you create a new TPicture ? That is not needed.
Hans.
 

Re:Drawing on bitmap

Hi,
Quote
Image->Picture = new TPicture();
I think, this isn't neccessary! There is a TPicture in the Image
automatically. If You create a new one, You might get trouble with
memory-leaks.
But it is neccessary, to set the bitmaps width and hight. Those are zero
in a new bitmap - So You are drawing far beyond the borders of the bitmap.
Image1->Picture->Bitmap->Width=whateveryouneed;
Image1->Picture->Bitmap->Height=whateveryouneed;
and it is also a good idea, to set the pixelformat according to Your
needs - Otherwise, the results may depend on the actual settings of the
computer:
Image1->Picture->Bitmap->PixelFormat=pf24Bit;
After this
Quote
Image->Picture->Bitmap->Canvas->Pixels[x][y] = clRed;
Should work correktly.
By the way: Canvas->Pixels seems to be the slowest possible way to set a
pixel on the screen. If You want to change a lot of pixels have a look
at Bitmap->RowScan
Good Luck
Michael Kutscher
www.kutscher-software.de
 

{smallsort}

Re:Drawing on bitmap

OK, it works now, thank you very much.
Quote
By the way: Canvas->Pixels seems to be the slowest possible way to set a
pixel on the screen. If You want to change a lot of pixels have a look
at Bitmap->RowScan
I know about Bitmap->ScanLine, however, I had problems with it. The goal
is to copy pixels from Bitmap1 (with pf32bit) to another one - Bitmap2
(also with pf32bit).
So, the previous code was as follows:
Graphics::TBitmap *Bitmap2;
Bitmap2 = new Graphics::TBitmap();
Bitmap2->PixelFormat = pf24bit;
Byte* ptr1, ptr2;
for (int y = 0; y < pBitmap1->Height; y++)
{
ptr2 = (Byte *)pBitmap2->ScanLine[y];
ptr1 = (Byte *)pBitmap1->ScanLine[y];
memcpy(ptr1, ptr2, pBitmap1->Width*4);
}
At the first string in cycle
ptr2 = (Byte *)pBitmap2->ScanLine[y];
at y = 0 the following error appears:
EInvalidGraphicOperation with message 'Scan line index out of range'.
Where is the problem here?
Probably, there is another way to copy one bitmap to another?
And how to modify this code if I want copy bits of pf32bit bitmap into
pf24bit bitmap?
Thanks in advance,
TM
 

Re:Drawing on bitmap

Hi,
Quote
Graphics::TBitmap *Bitmap2;
Bitmap2 = new Graphics::TBitmap();
Bitmap2->PixelFormat = pf24bit;
First: Width and Hight of Bitmap2 must be set, before it can be used in
any way.
Quote
Byte* ptr1, ptr2;
for (int y = 0; y < pBitmap1->Height; y++)
{
ptr2 = (Byte *)pBitmap2->ScanLine[y];
ptr1 = (Byte *)pBitmap1->ScanLine[y];
memcpy(ptr1, ptr2, pBitmap1->Width*4);
as You are using 24-Bit per pixel, You are going too far here. It has to be
memcpy(ptr1, ptr2, pBitmap1->Width*3);
But, if You just want to copy pieces of a bitmap or a whole one, it
might be the better way, to simply draw or CopyRect it on the other one.
Thats less code and works fast as well.
Quote
And how to modify this code if I want copy bits of pf32bit bitmap into
pf24bit bitmap?
Ooops... than there might be much more to do. You could quite simply
copy the whole bitmap by drawing it an the other one:
Bitmap32->Canvas->Draw(0,0,Bitmap2);
I don't know, wether Convas->CopyRect would work correctly in this
situation, but I think it's worth a try. Modifying each pixel "manually"
doesn't look very effective...
Good luck
Michael Kutscher
 

Re:Drawing on bitmap

Thank you very much for your help and explanations.
Quote
I don't know, wether Convas->CopyRect would work correctly in this
situation, but I think it's worth a try. Modifying each pixel "manually"
doesn't look very effective...
Yes, both Canvas->Draw and Canvas->CopyRect work well.