简体   繁体   中英

Bitmap is stretching and not tiling

I'm trying to get a bitmap to tile something like this:

 ----  ----  ----
|    ||    ||    |
 ----  ----  ----

doing the following as an example:

CD2DBitmapBrush* m_pBitBrush;
m_pBitBrush = new CD2DBitmapBrush(GetRenderTarget(), _T("grass.bmp"));

POINT pt;
pt.x = 0;
pt.y = 0;
CRect rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize());

pRenderTarget->FillRectangle(rect, m_pBitBrush);


for(int i = 0; i < 5; i++)
{
    pt.x += 40; 
    rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize()); 
    pRenderTarget->FillRectangle(rect, m_pBitBrush);
}

When I do that, the bitmap displays correctly once, but every instance after that is "stretched" (ie the last column of pixels is repeated, but not the rest of the image).

If I change x, the right-most column is repeated. If I change y, the bottom row is repeated. And if i change both x and y (going diagonally), the corner pixel fills the rectangle.

x:
 ----  -----------
|    |||||||||||||
 ----  -----------

y:             x and y:
 ----                  *----*
|    |                 |    |
 ----                  *----*
 ----                        ******
 ----                        ******
 ----                        ****** 

Also, I have tried changing the image that the brush uses after it is called the first time, and nothing displays after the original.

CRect rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize());
pRenderTarget->FillRectangle(rect, m_pBitBrush); 

CD2DBitmap* bit = new CD2DBitmap(GetRenderTarget()/*pRenderTarget*/, _T("stone.bmp"));  
m_pBitBrush->SetBitmap(bit);
for(int i = 0; i < 5; i++)
{
    pt.x += 40;
    rect = new CRect(pt, m_pBitBrush->GetBitmap()->GetSize()); 
    pRenderTarget->FillRectangle(rect, m_pBitBrush);
}

Surely, I am doing something wrong here since I am new, but I cannot seem to get this to work. Can anyone point me in the right direction?

You want to use SetExtendModeX(D2D1_EXTEND_MODE mode) and/or SetExtendModeY(D2D1_EXTEND_MODE mode) on your CD2DBitmapBrush . The default seems to be D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_CLAMP , which causes the stretching. You want D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_WRAP .

Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368100(v=vs.85).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM