C#控件picturebox實(shí)現(xiàn)圖像拖拽和縮放
本文實(shí)例為大家分享了C# picturebox實(shí)現(xiàn)圖像拖拽和縮放的具體代碼,供大家參考,具體內(nèi)容如下
1.核心步驟:
①新建Point類(lèi)型全局變量mouseDownPoint,記錄拖拽過(guò)程中鼠標(biāo)位置;
②MouseDown事件記錄Cursor位置;
③MouseMove事件計(jì)算移動(dòng)矢量,并更新pictureBox1.Location。
代碼:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X; //記錄鼠標(biāo)左鍵按下時(shí)位置
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
pictureBox1.Focus(); //鼠標(biāo)滾輪事件(縮放時(shí))需要picturebox有焦點(diǎn)
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Focus(); //鼠標(biāo)在picturebox上時(shí)才有焦點(diǎn),此時(shí)可以縮放
if (isMove)
{
int x, y; //新的pictureBox1.Location(x,y)
int moveX, moveY; //X方向,Y方向移動(dòng)大小。
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = pictureBox1.Location.X + moveX;
y = pictureBox1.Location.Y + moveY;
pictureBox1.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X; //記錄鼠標(biāo)左鍵按下時(shí)位置
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
}
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
panel2.Focus(); //鼠標(biāo)不在picturebox上時(shí)焦點(diǎn)給別的控件,此時(shí)無(wú)法縮放
if (isMove)
{
int x, y; //新的pictureBox1.Location(x,y)
int moveX, moveY; //X方向,Y方向移動(dòng)大小。
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = pictureBox1.Location.X + moveX;
y = pictureBox1.Location.Y + moveY;
pictureBox1.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
2.圖像縮放
核心思想:利用picturebox的zoom模式,根據(jù)圖像顯示大小更改picturebox大小,記錄鼠標(biāo)位置補(bǔ)償縮放位移,實(shí)現(xiàn)錨點(diǎn)縮放,即以鼠標(biāo)位置為中心進(jìn)行縮放。
zoomstep --- 自己定義滾輪滑動(dòng)縮放大小
代碼:
//實(shí)現(xiàn)錨點(diǎn)縮放(以鼠標(biāo)所指位置為中心縮放);
//步驟:
//①先改picturebox長(zhǎng)寬,長(zhǎng)寬改變量一樣;
//②獲取縮放后picturebox中實(shí)際顯示圖像的長(zhǎng)寬,這里長(zhǎng)寬是不一樣的;
//③將picturebox的長(zhǎng)寬設(shè)置為顯示圖像的長(zhǎng)寬;
//④補(bǔ)償picturebox因縮放產(chǎn)生的位移,實(shí)現(xiàn)錨點(diǎn)縮放。
// 注釋?zhuān)簽樯兑冖鄄??由于zoom模式的機(jī)制,把picturebox背景設(shè)為黑就知道為啥了。
//這里需要獲取zoom模式下picturebox所顯示圖像的大小信息,添加 using System.Reflection;
//pictureBox1_MouseWheel事件沒(méi)找到。。。手動(dòng)添加,別忘在Form1.Designer.cs的“Windows 窗體設(shè)計(jì)器生成的代碼”里加入:
//this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel)。
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
int ow = pictureBox1.Width;
int oh = pictureBox1.Height;
int VX, VY; //因縮放產(chǎn)生的位移矢量
if (e.Delta > 0) //放大
{
//第①步
pictureBox1.Width += zoomStep;
pictureBox1.Height += zoomStep;
//第②步
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
//第③步
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
if (e.Delta < 0) //縮小
{
//防止一直縮成負(fù)值
if (pictureBox1.Width < myBmp.Width / 10)
return;
pictureBox1.Width -= zoomStep;
pictureBox1.Height -= zoomStep;
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
//第④步,求因縮放產(chǎn)生的位移,進(jìn)行補(bǔ)償,實(shí)現(xiàn)錨點(diǎn)縮放的效果
VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中計(jì)算時(shí)間差中的小數(shù)問(wèn)題解決
C#中計(jì)算時(shí)間差中的小數(shù)問(wèn)題解決需要的朋友可以參考一下2013-03-03
C# DataGridView中實(shí)現(xiàn)勾選存儲(chǔ)數(shù)據(jù)和右鍵刪除數(shù)據(jù)(示例代碼)
這篇文章主要介紹了C# DataGridView中實(shí)現(xiàn)勾選存儲(chǔ)數(shù)據(jù)和右鍵刪除數(shù)據(jù)的示例代碼,通過(guò)示例代碼給大家展示運(yùn)行效果圖,需要的朋友可以參考下2021-07-07
C#?守護(hù)進(jìn)程的介紹及實(shí)現(xiàn)詳解
本文主要介紹了C#?守護(hù)進(jìn)程的介紹及實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#多線(xiàn)程處理多個(gè)隊(duì)列數(shù)據(jù)的方法
本文將結(jié)合實(shí)例代碼,介紹C#多線(xiàn)程處理多個(gè)隊(duì)列數(shù)據(jù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
unity3d?對(duì)接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能
workerman?是一款開(kāi)源高性能?PHP?應(yīng)用容器,他除了用于互聯(lián)網(wǎng)、即時(shí)通訊、APP?開(kāi)發(fā)、硬件通訊、智能家居、物聯(lián)網(wǎng)等領(lǐng)域的開(kāi)發(fā)外,這篇文章主要介紹了unity3d?對(duì)接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲,需要的朋友可以參考下2022-10-10
C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果
這篇文章主要介紹了C#實(shí)現(xiàn)漂亮的數(shù)字時(shí)鐘效果,涉及時(shí)間函數(shù)的應(yīng)用及繪圖的方法,需要的朋友可以參考下2014-10-10

