# Thread: Showing Image in center

1. ## Showing Image in center

I have a bitmap of size 100X 120.

I want to display this bitmap in the center of the rectangular area in the screeen.

The cordinate of rectangular area is ..
topleft(0,360)
topright(360,360)
bottomlft(0,480)
botom right(360,480)

How to do this?

2. ## Re: Showing Image in center

just calculate the top & left gap from the image size & screen-rect size, and then set the coordinates correct for the draw function..

3. ## Re: Showing Image in center

Originally Posted by siva_321
I have a bitmap of size 100X 120.

I want to display this bitmap in the center of the rectangular area in the screeen.

The cordinate of rectangular area is ..
topleft(0,360)
topright(360,360)
bottomlft(0,480)
botom right(360,480)

How to do this?
if the coordinates are fixed then wht the prob.

draw it on the point (80,360).

4. ## Re: Showing Image in center

Hi,

Use the following code:
Code:
```TSize sz = iBitmap->SizeInPixels();
TInt xPos,yPos;
xPos = (Rect().Width()-sz.iWidth)/2;
yPos = (Rect().Height()-sz.iHeight)/2;

gc.BitBlt(TPoint(xPos,yPos),iBitmap);```
Thanks,
Eswar

5. ## Re: Showing Image in center

siva do all those as u did earlier.

6. ## Re: Showing Image in center

Hi,
My coordinate of the rectangular area in the screen remains constant.
But my bitmap size will change.
I am scaling using the function
Scale(&iStatus, *iBitmap, TSize(120,120),ETrue);

and trying to draw the resulting bitmap in the center of the rectangular area inthe screen mentioned earlier.

7. ## Re: Showing Image in center

Hi siva,

Instead of scaling, use the following code:

Code:
```TSize sz = iBitmap->SizeInPixels();

TInt wd =360,ht=480;

if(sz.iHeight>=ht)
{
TInt percent = (sz.iHeight-ht)*100;
percent = percent/sz.iHeight;

sz.iHeight=sz.iHeight-((sz.iHeight)*percent/100);
sz.iWidth=sz.iWidth-((sz.iWidth)*percent/100);
}
if(sz.iHeight< ht)
{
TInt percent = (ht-sz.iHeight)*100;
percent = percent/sz.iHeight;

sz.iHeight=sz.iHeight+((sz.iHeight)*percent/100);
sz.iWidth=sz.iWidth+((sz.iWidth)*percent/100);
}
if(sz.iWidth>=wd)
{
TInt percent = (sz.iWidth-wd)*100;
percent = percent/sz.iWidth;

sz.iHeight=sz.iHeight-((sz.iHeight)*percent/100);
sz.iWidth=sz.iWidth-((sz.iWidth)*percent/100);
}

TPoint ps;
ps.iX=(wd-sz.iWidth)/2;
ps.iY = (ht-sz.iHeight)/2;

gc.DrawBitmap(TRect(ps,sz),iImage);```
It works find for me in my app.

Thanks,
Eswar

8. ## Re: Showing Image in center

Hi Eswar,
Thanks.It will work fine.But I do want to maintain the aspect ratio.Thats why i am using scale().

9. ## Re: Showing Image in center

You could simply make sure you keep the aspect ratio on drawing rect creation, would save some processing on your application really..

10. ## Re: Showing Image in center

Originally Posted by siva_321
Hi Eswar,
Thanks.It will work fine.But I do want to maintain the aspect ratio.Thats why i am using scale().
Hi siva,

Above code works for aspect ratio.

Thanks,
Eswar

11. ## Re: Showing Image in center

Hi Eswar,
Your code is for the whole screen(360 X 480)
But my rectangular are is not of that size

It is in the bottom portion of the screen..
The cordinate of rectangular area is ..
topleft(0,360)
topright(360,360)
bottomlft(0,480)
botom right(360,480)
My rect is of size (360 X 120) in the bottom portion

Also if i give
TInt wd =360,ht=120;
It will take the top portion and draw.

12. ## Re: Showing Image in center

Hi,

Code:
```TInt offset= 480-(ht); //here ht=120
ps.iX=(wd-sz.iWidth)/2;
ps.iY = (ht-sz.iHeight)/2;
ps.iY+=offset;
gc.DrawBitmap(TRect(ps,sz),iImage);```
Check with above logic.

Thanks,
Eswar

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•