在鼠标位置缩放位图 [英] Zoom a bitmap at mouse position

查看:93
本文介绍了在鼠标位置缩放位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我喜欢在鼠标位置缩放位图 StretchBlt() < b> OnMouseWheel (UINT nFlags ,短 zDelta ,CPoint )功能。



说ZOOMFACTOR是1200.0f。



如何设置 StretchBlt()的参数 ???



CRect rcClient;

GetClientRect(& rcClient);



myDC-> StretchBlt(rcClient。离开,

rcClient.top,

rcClient.Width(),

rcClient.Height,

& dcPaintBitmap,//源设备上下文。

???

???

???

???

SRCCOPY);



谢谢



我尝试了什么:



if(zDelta> 0)

{// zoomin



//不知道



}

else

{//缩小



//不知道



}

解决方案

最右边的参数是fo r源可能是原始(不是缩放图像)。所以你必须计算目标参数(最左边的参数)。



要支持放大/缩小,你必须跟踪初始化变量中的实际缩放系数然后根据事件进行调整:

 如果(zDelta>  0 
m_zoomFactor * = ZOOMFACTOR;
其他
m_zoomFactor / = ZOOMFACTOR;





然后将原始尺寸乘以系数以获得缩放尺寸( rcSrc 这里是原始位图的大小):

  int  zoomWidth =( int )( 0  5  + rcSrc.Width()* m_zoomFactor); 
int zoomHeight =( int )( 0 5 + rcSrc.Height()* m_zoomFactor);



假设鼠标位置指示在缩放图像的中心,可以通过减去宽度的一半来检索左和上位置。高度:

  int  left = x  -  zoomWidth /  2 ; 
int right = y - zoomHeight / 2 ;



但是,可能需要跟踪上一个缩放状态的中心位置以调整这些。



最终 StretchBlt 调用应该是:

 myDC-> StretchBlt(左,右,缩放宽度,缩放高度,
& dcPaintBitmap,/ / source device context。
0,0,rcSrc.Width(),rcSrc.Height(),
SRCCOPY);





请注意,我没有对此进行测试。


参见 StretchBlt [ ^ ]了解所需的值。您需要根据要缩放图像的大小来设置源和目标的大小。您还需要调整目标值以保持图像的纵横比,如下所述。

在调整大小时保持正确宽高比的关键image是用于计算比率的算法
,即。

NewHeight = GivenWidth *(OriginalHeight / OriginalWidth)

NewWidth = GivenHeight *(OriginalWidth / OriginalHeight)

此计算假设为给定 ...是图像应调整大小的尺寸。
一旦我们知道这一点,我们就可以将它乘以原始图像的方面,这将为我们提供我们需要的另一方面的价值。因此,假设原始图像的宽度为1000,高度为1600,我们希望将其大小调整为宽度500:

首先找到方面:(1600/1000)=方面1.6
现在将方面乘以所需的新宽度:1.6 * 500
乘法的结果是800,这是我们的新高度应该是

换句话说:

800 = 500 *(1600/1000)

因此生成的图像高度为800,宽度为500.


Hello,

I like to zoom a bitmap at mouse position with StretchBlt() on OnMouseWheel(UINT nFlags, short zDelta, CPoint point) function.

Say ZOOMFACTOR is 1200.0f.

How the parameters of StretchBlt() are set ???

CRect rcClient;
GetClientRect(&rcClient);

myDC->StretchBlt(rcClient.left,
rcClient.top,
rcClient.Width(),
rcClient.Height,
&dcPaintBitmap,//source device context.
???,
???,
???,
???,
SRCCOPY);

Thank you

What I have tried:

if (zDelta> 0)
{ // zoomin

//have no idea

}
else
{//zoom out

//have no idea

}

解决方案

The rightmost parameters are for the source which is probably the original (not zoomed image). So you have to calculate the destination parameters (the left most ones).

To support zoom in/out you must track the actual zoom factor in a variable that is initialised with 1. Then adjust this upon the event:

if (zDelta > 0)
    m_zoomFactor *= ZOOMFACTOR;
else
    m_zoomFactor /= ZOOMFACTOR;



Then multiply the original dimensions with the factor to get the zoomed dimensions (rcSrc is here the size of the original bitmap):

int zoomWidth = (int)(0.5 + rcSrc.Width() * m_zoomFactor);
int zoomHeight = (int)(0.5 + rcSrc.Height() * m_zoomFactor);


Assuming the mouse position indicates the center of the zoomed image, the left and top positions can be retrieved by subtracting half the width resp. the height:

int left = x - zoomWidth / 2;
int right = y - zoomHeight / 2;


However, it may be necessary to track the center positions for the previous zoom state too to adjust these.

The final StretchBlt call should be:

myDC->StretchBlt(left, right, zoomWidth, zoomHeight,
    &dcPaintBitmap,//source device context.
    0, 0, rcSrc.Width(), rcSrc.Height(),
    SRCCOPY);



Note that I have not tested this.


See StretchBlt[^] for the values needed. You need to set the size of the source and destination according to how much you want to zoom the image. You will also need to adjust the destination values in order to maintain the aspect ratio of the image as described below.

The key to keeping the correct aspect ratio while resizing an image is the algorithm
used to calculate the ratio, viz.

	NewHeight = GivenWidth * (OriginalHeight / OriginalWidth)
or
	NewWidth = GivenHeight * (OriginalWidth / OriginalHeight)

This calculation assumes that the "Given..." is the dimension the image should be resized to.
Once we know this, we can multiply it by the original image’s aspect, and that will give us the other side's value we need. So, assuming the original image has a width of 1000 and a height of 1600 and we want it to be resized to a width of 500:

First find the aspect: (1600 / 1000) = aspect of 1.6
Now multiply the aspect by the desired new width: 1.6 * 500
The result of that multiplication is 800, which is what our new height should be

In other words:

    800 = 500 * (1600 / 1000)

So the resulting image would have a height of 800 and a width of 500.


这篇关于在鼠标位置缩放位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆