參考CSND論壇
一,點擊圖片上任意位置,此位置移動至圖框(picturebox)中心.
需要三個坐標:1,圖片中心坐標(根據圖片尺寸計算);2,圖框中心坐標(常量,已確定);3點擊坐標(圖框坐標系)
方法:根據1與2的位置差,先將圖片中心與圖框中心重合,然后使用全局變量疊加存儲每次的3與2的偏差值(疊加存儲作用是讓圖片在上一次偏移的基礎上,進行補償),最后將最新偏差值賦予圖片做位置補償,然后存儲最新偏差值。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFA3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Bitmap bit1;//上提示圖片(圖片1)
public Bitmap bitBig;//放大后圖片
bool mark=false;
//點擊位置坐標
Point MD;
//存儲點擊位置坐標
Point md;
//picbox中心坐標
Point PbCenter;
//圖片中心坐標
Point PicCenter;
Point PicCenterB;
//圖片偏移量緩存(存儲上一次圖片偏移量)
int Sx;
int Sy;
//pb中心與pic中心位置差
Size CenDist;
//大圖片時pb中心與pic中心位置差
Size CenDistBig;
private void button1_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.openFileDialog1.FileName;//獲得圖片路徑
bit1 = new Bitmap(path);
this.pictureBox1.Image = bit1;
//圖片中心坐標,據pb中心距離114,200
PicCenter = new Point((bit1.Width / 2), (bit1.Height / 2));
//pb中心與pic中心位置差
Size CenDis = (Size)PicCenter - (Size)PbCenter;
label1.Text = Convert.ToString(CenDis.Width);
label2.Text = Convert.ToString(CenDis.Height);
CenDist = CenDis;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if(mark==false)
{
if (true)
{
}
mark = true;
}
else
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
label9.Text = "PbCenter";
label10.Text = "PicCenter";
label11.Text = "CenDis";
label12.Text = "Mouse Click Postion";
//圖框中心坐標
PbCenter = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
label1.Text = Convert.ToString(PbCenter.X);
label2.Text = Convert.ToString(PbCenter.Y);
}
//小圖繪圖
private void button2_Click(object sender, EventArgs e)
{
int sx = MD.X - PbCenter.X;
int sy = MD.Y - PbCenter.Y;
label5.Text = Convert.ToString(sx);
label6.Text = Convert.ToString(sy);
Bitmap bit = new Bitmap(500, 500);
Graphics g = Graphics.FromImage(bit);
//g.DrawImage(bit1,114-Sx-sx, 200-Sy-sy, bit1.Width,bit1.Height);
g.DrawImage(bit1, -CenDist.Width- Sx - sx,-CenDist.Height - Sy - sy, bit1.Width, bit1.Height);//圖片偏移量計算,圖中心與圖框中心偏移量+之前點擊偏移累加量+最新一次點擊偏移量,三者之和做補償(所以全部為負)
DrawNumber(pictureBox1, g);
this.pictureBox1.Image = bit;
Sx += sx;
Sy += sy;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
label7.Text = Convert.ToString(e.X);
label8.Text = Convert.ToString(e.Y);
MD.X = e.X;
MD.Y = e.Y;
}
//繪制圖框中心標記,調用繪圖方法
public static void DrawNumber(PictureBox pictureBox, Graphics g)
{
//DrawString(g, "點擊左邊的字", 115, 300);
//DrawString(g, "請點擊圖中最大的正方體", 10, 410);
DrawPoint(g,249,249,3,3);
}
//繪圖方法函數
private static void DrawPoint(Graphics g, int x,int y,int width,int height)
{
//g.DrawString(s, new System.Drawing.Font("????", 10, FontStyle.Bold), new System.Drawing.SolidBrush(System.Drawing.Color.Red), x, y);
g.DrawRectangle(new Pen(Color.Red),x,y,width,height);
}
//圖片縮放函數,未完成
private static Bitmap ZoomP(Bitmap a, float s, float v,int x,int y)
{
Bitmap bmp = new Bitmap((int)(a.Width * s), (int)(a.Height * v), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.ScaleTransform(s, v);
g.DrawImage(a, x, y, a.Width, a.Height);
a = bmp;
return a;
}
//圖片放大,未完成
private void button3_Click(object sender, EventArgs e)
{
Bitmap bitB = new Bitmap(500,500);
bitB = ZoomP(bit1, 2, 2,Sx,Sy);
this.pictureBox1.Image = bitB;
bitBig = bitB;
//大圖中心點位置
//PicCenter = new Point((bitB.Width / 2), (bitB.Height / 2));
//label3.Text = Convert.ToString(PicCenter.X);
//label4.Text = Convert.ToString(PicCenter.Y);
////圖片中心變成了272,100
////距離pb中心為-22,150
////大圖片時,pb與picbig的中心差值
//PicCenterB = new Point((bitBig.Width / 2), (bitBig.Height / 2));
//Size CenDisB = (Size)PicCenterB - (Size)PbCenter;
//CenDistBig = CenDisB;
//label5.Text = Convert.ToString(CenDistBig.Width);
//label6.Text = Convert.ToString(CenDistBig.Height);
}
//大圖繪圖,未完成
private void button4_Click(object sender, EventArgs e)
{
int sx = MD.X - PbCenter.X;
int sy = MD.Y - PbCenter.Y;
label5.Text = Convert.ToString(sx);
label6.Text = Convert.ToString(sy);
Bitmap bit = new Bitmap(500, 500);
Graphics g = Graphics.FromImage(bit);
//g.DrawImage(bit1,114-Sx-sx, 200-Sy-sy, bit1.Width,bit1.Height);
g.DrawImage(bitBig, -CenDistBig.Width - Sx - sx, -CenDistBig.Height - Sy - sy, bitBig.Width, bitBig.Height);
DrawNumber(pictureBox1, g);
this.pictureBox1.Image = bit;
Sx += sx;
Sy += sy;
}
}
}