110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace PictureView
{
public partial class pictureView : UserControl
{
private float _zoomScale;
private Point _startPoint, _endPoint;
private bool _isDown;
private bool _isLoadBmp;
private Bitmap _currBmp;
public pictureView()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
this.MouseEnter += new EventHandler(panel1_MouseEnter);
}
private void pictureView_Load(object sender, EventArgs e)
{
SetStyle(ControlStyles.UserPaint, true);
//防止窗口跳动
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//防止控件跳动
SetStyle(ControlStyles.DoubleBuffer, true);
_isLoadBmp = false;
_isDown = false;
_zoomScale = 1.0f;
_startPoint = new Point(0, 0);
_endPoint = new Point(0, 0);
}
public void panel1_MouseEnter(object sender, EventArgs e)
{
this.pictureBox1.Focus();
}
/// <summary>
/// 处理鼠标滚动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void panel1_MouseWheel(object sender, MouseEventArgs e)
{
_zoomScale += (float)e.Delta / 1000;
if (_zoomScale < 0.1)
_zoomScale = 0.1f;
if (_zoomScale > 10.0)
_zoomScale = 10.0f;
pictureBox1.Refresh();
Point p = e.Location;
Point p1 = pictureBox1.PointToClient(p);
Point p2 = pictureBox1.PointToScreen(p);
}
public void FromFile(string filePath)
{
Image img = Image.FromFile(filePath);
_currBmp = new Bitmap(img);
if (pictureBox1.Width < _currBmp.Width || pictureBox1.Height < _currBmp.Height)
_zoomScale = Math.Min((float)pictureBox1.Width / (float)_currBmp.Width, (float)pictureBox1.Height / (float)_currBmp.Height);
else
_zoomScale = 1.0f;
img.Dispose();
_isLoadBmp = true;
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (_isLoadBmp)
{
Rectangle rect = new Rectangle(0, 0, _currBmp.Width, _currBmp.Height);
rect.Width = (int)(rect.Width * _zoomScale);
rect.Height = (int)(rect.Height * _zoomScale);
rect.X = (int)((pictureBox1.Width - rect.Width) / 2) + _endPoint.X;
rect.Y = (int)((pictureBox1.Height - rect.Height) / 2) + _endPoint.Y;
g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
g.DrawImage(_currBmp, rect, new Rectangle(0, 0, _currBmp.Width, _currBmp.Height), GraphicsUnit.Pixel);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_isDown = true;
_startPoint.X = e.Location.X - _endPoint.X;
_startPoint.Y = e.Location.Y - _endPoint.Y;
this.Cursor = Cursors.Hand;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_isDown)
{
Point temp = e.Location;
_endPoint.X = temp.X - _startPoint.X;
_endPoint.Y = temp.Y - _startPoint.Y;
pictureBox1.Refresh();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_isDown = false;
this.Cursor = Cursors.Default;
}
private void pictureView_Resize(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace NewTemp
{
public partial class Form3 : Form
{
Point p = new Point();//图片位置
public Form3()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(this_MouseWheel);
Bitmap img = new Bitmap(@"F:\2.jpg");
pictureBox1.Image = img;
pictureBox1.Width = img.Width;
pictureBox1.Height = img.Height;
pictureBox1.Location = new Point(0, 0);
pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
getPoint(pictureBox1);
//MessageBox.Show(p.X.ToString() + " " + p.Y.ToString());
}
private void getPoint(Control c)
{
if (c.Parent != null)
{
p.X += c.Location.X;
p.Y += c.Location.Y;
getPoint(c.Parent);
}
}
void this_MouseWheel(object sender, MouseEventArgs e)
{
//MessageBox.Show(e.X.ToString() +" "+ e.Y.ToString());
System.Drawing.Size t = pictureBox1.Size;
int _w = t.Width;
int w = t.Width + e.Delta;
int h = Convert.ToInt32(t.Height + e.Delta * (Convert.ToDecimal(t.Height) / t.Width));
if (w > 0 && h > 0)
{
pictureBox1.Width = w;
pictureBox1.Height = h;
}
p = new Point(0, 0);
getPoint(pictureBox1);
//MessageBox.Show(p.X.ToString() + " " + p.Y.ToString());
try
{
Point p1 = pictureBox1.Location;
int w1 = e.X - p.X;
int h1 = e.Y - p.Y;
int w2 = Convert.ToInt32(e.Delta * (Convert.ToDecimal(w1) / _w));
int x = p1.X - w2;
int y = Convert.ToInt32(p1.Y - w2 * (Convert.ToDecimal(h1) / w1));
pictureBox1.Location = new Point(x, y);
}
catch { }
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(152, 57);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(280, 221);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 267);
this.Controls.Add(this.pictureBox1);
this.Name = "Form3";
this.Text = "Form3";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
试下这个
之前private void getPoint(Control c)
{
p.X += c.Location.X;
p.Y += c.Location.Y;
if (c.Parent != null)
getPoint(c.Parent);
}有问题的,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace NewTemp
{
public partial class Form3 : 基类.BaseDockContent
{
Point p = new Point();//图片位置
public Form3()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(this_MouseWheel);
Bitmap img = new Bitmap(@"F:\2.jpg");
pictureBox1.Image = img;
pictureBox1.Width = img.Width;
pictureBox1.Height = img.Height;
pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
getPoint(pictureBox1);
}
private void getPoint(Control c)
{
p.X += c.Location.X;
p.Y += c.Location.Y;
if (c.Parent != null)
getPoint(c.Parent);
}
void this_MouseWheel(object sender, MouseEventArgs e)
{
//MessageBox.Show(e.X.ToString() +" "+ e.Y.ToString());
System.Drawing.Size t = pictureBox1.Size;
int _w = t.Width;
int w = t.Width + e.Delta;
int h = Convert.ToInt32(t.Height + e.Delta * (Convert.ToDecimal(t.Height) / t.Width));
if (w > 0 && h > 0)
{
pictureBox1.Width = w;
pictureBox1.Height = h;
}
p = new Point(0, 0);
getPoint(pictureBox1);
Point p1 = pictureBox1.Location;
int w1 = e.X - p.X;
int h1 = e.Y - p.Y;
int w2 = Convert.ToInt32(e.Delta * (Convert.ToDecimal(w1) / _w));
int x = p1.X -w2;
int y = Convert.ToInt32(p1.Y - w2 * (Convert.ToDecimal(p1.Y) / p1.X));
pictureBox1.Location = new Point(x,y);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(152, 57);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(280, 221);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 267);
this.Controls.Add(this.pictureBox1);
this.Name = "Form3";
this.Text = "Form3";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
我写的这个可以缩放,但没有判断鼠标在图片上面才缩放,你判断一下就行了
rect.X = (int)((pictureBox1.Width - rect.Width) / 2) + _endPoint.X;
rect.Y = (int)((pictureBox1.Height - rect.Height) / 2) + _endPoint.Y;
我现在想控制这两个值,但是想了好久,不知道该如何去控制它
public void panel1_MouseWheel(object sender, MouseEventArgs e)
{
_zoomScale += (float)e.Delta / 1000;
if (_zoomScale < 0.1)
_zoomScale = 0.1f;
if (_zoomScale > 10.0)
_zoomScale = 10.0f;
pictureBox1.Refresh();
}