# [Easy] Direct2D - Bitmap not at correct x,y

## Recommended Posts

Hopefully this is the right place to post. For some reason in my game, bitmaps are not being placed correctly.

Here I'm trying to draw a Bitmap at 200, 200 (width is 16x16) but instead its being drawn at roughly 194, 186

However when I try drawing it at 0,0, it draws correctly:

I pretty much followed exactly the MSDN articles, here's the code:

ResourceLoader::GetImageAssets().at(4);
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(200.f, 200.f);
target->DrawBitmap(
D2D1::RectF(
upperLeftCorner.x,
upperLeftCorner.y,
upperLeftCorner.x + size.width,
upperLeftCorner.y + size.height),

1.0f
);

ResourceLoader is some custom class, GetImageAssets is a vector with a class called ImageAsset, ImageAsset contains the Bitmap.

I've tried with no defined transforms and with the Matrix3x4 identity transform with still the same result.

Edit: It appears that the more it strays from 0,0, the larger the "offset". It doesn't seem to be some static x,y offset/transform.

Edit2: Same issue with DrawRectangle. Seems to be an issue with the Direct2D drawing and not the DrawBitmap method itself.

Edit3: Fixed. I was not creating my HwndRenderTarget correctly.

Edited by VQuery

##### Share on other sites

is your projection matrix the size of your screen?  this could be a problem if you are projecting to say 800x600 when your resolution is something different.

##### Share on other sites

is your projection matrix the size of your screen?  this could be a problem if you are projecting to say 800x600 when your resolution is something different.

Hey I made an edit yesterday, you are correct. It was a really easy fix.

RECT rc;
GetClientRect(hWnd, &rc);

D2D1_SIZE_U size = D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top
);


Then for the HwndRenderTarget:

D2D1::HwndRenderTargetProperties(hWnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY);

Thanks ncsu121978 for following up!

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628726
• Total Posts
2984411

• 25
• 11
• 10
• 16
• 14