我应该使用哪个 Gdiplus::Graphics::DrawImage 函数?

发布于 2024-10-17 19:39:48 字数 679 浏览 0 评论 0原文

我想将 (x,y,w,h) 子矩形从 src 粘贴到 dest,以便 (x,y) 源像素显示为 (0,0) 目标像素。我很难决定哪一个重载可以做到这一点。现在我的代码看起来像这样,这显然不起作用:

auto_ptr<Gdiplus::Graphics> g(Gdiplus::Graphics::FromHDC(pDC->GetSafeHdc()));
g->DrawImage(png, r.top, r.bottom, r.Width(), r.Height());
g->Flush(); 

它不起作用,因为 (x,y) 源像素显示为 (x,y) 目标像素,而我希望它显示为 (0 ,0) 目标像素。源矩形和目标矩形应具有相同的大小但不同的偏移量,而不是相同的偏移量。

[编辑] 没关系,我找到了我需要的重载函数。结果我必须添加的只是前面的目标 (x,y) 偏移量和末尾的 UnitPixel。

auto_ptr<Gdiplus::Graphics> g(Gdiplus::Graphics::FromHDC(pDC->GetSafeHdc()));
g->DrawImage(png, 0, 0, r.left, r.top, r.Width(), r.Height(), Gdiplus::UnitPixel);
g->Flush();

I want paste a (x,y,w,h) subrectangle from src to dest, so that the (x,y) source pixel appears as the (0,0) dest pixel. I'm having a difficulty deciding which of the gazillion overloads does this. Right now my code looks like this, which obviously doesn't work:

auto_ptr<Gdiplus::Graphics> g(Gdiplus::Graphics::FromHDC(pDC->GetSafeHdc()));
g->DrawImage(png, r.top, r.bottom, r.Width(), r.Height());
g->Flush(); 

It doesn't work because the (x,y) source pixel appears as the (x,y) destination pixel, whereas I want it to appear as the (0,0) destination pixel. The source and destination rectangles should have the same size but different offsets, instead of the same offset.

[Edit]
Nevermind, I found the overloaded function I need. Turns all I had to add was the destination (x,y) offset in front, and UnitPixel at the end.

auto_ptr<Gdiplus::Graphics> g(Gdiplus::Graphics::FromHDC(pDC->GetSafeHdc()));
g->DrawImage(png, 0, 0, r.left, r.top, r.Width(), r.Height(), Gdiplus::UnitPixel);
g->Flush();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2024-10-24 19:39:48

任何人都会工作,他们都工作得同样好。他们大多只是彼此的别名。我猜您正在使用的将是这个 其中指出:

x [in] REAL 指定的实数
左上角的 x 坐标
目标矩形的角点
用于绘制图像。

y [in] REAL 指定的实数
左上角的 y 坐标
目标矩形的角点
用于绘制图像。

尝试将 x 和 y 值设置为 0,以便调用变为:

auto_ptr<Gdiplus::Graphics> g(Gdiplus::Graphics::FromHDC(pDC->GetSafeHdc()));
g->DrawImage(png, 0, 0, r.Width(), r.Height());
g->Flush();

除此之外,您必须更具体地说明“不起作用”的内容。是不是画错了?是不是根本就没有画出来? png 是否正确加载?
如果没有绘制任何内容,我建议您检查返回值(这是一个 状态)来自 DrawImage,它应该为您指明正确的方向。


编辑

好的,所以如果你不希望它被映射为 src(0, 0) 到 dest(0, 0) 我想你可以使用 这个 代替。这会让你决定

src[x|y] [in] REAL 实数
指定 [x|y] 坐标
部分的左上角
要绘制的源图像。

希望这有帮助。

Anyone would work, they work equally well. They are mostly just aliases for each other. The one I guess you are using would be this one which states that:

x [in] REAL Real number that specifies
the x-coordinate of the upper-left
corner of the destination rectangle at
which to draw the image.

y [in] REAL Real number that specifies
the y-coordinate of the upper-left
corner of the destination rectangle at
which to draw the image.

Try to set the x and y values to 0 instead, so that the call becomes:

auto_ptr<Gdiplus::Graphics> g(Gdiplus::Graphics::FromHDC(pDC->GetSafeHdc()));
g->DrawImage(png, 0, 0, r.Width(), r.Height());
g->Flush();

Other than that you would have to be more specific as to what "Doesn't work". Is it incorrectly drawn? Is it not drawn at all? Is the png correctly loaded?
If nothing is drawn I suggest you check the return value (which is a Status) from DrawImage which should point you in the correct direction.


EDIT

Ok, so if you don't want it to be mapped as src(0, 0) to dest(0, 0) I guess you could use this one instead. This will let you decide

src[x|y] [in] REAL Real number that
specifies the [x|y]-coordinate of the
upper-left corner of the portion of
the source image to be drawn.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文