C# 使用鼠标滚轮缩放图像。
在下面的代码中,我试图通过鼠标滚轮缩放图像。但是代码工作不正常,它只是刷新面板,但没有调整面板大小。实际上,我从另一个名为decrypt的类创建的内存流中获取图像。完整图像正确显示,但我无法使用鼠标滚轮事件执行图像缩放。 请帮帮我C# 使用鼠标滚轮缩放图像。,c#,mousewheel,zooming,C#,Mousewheel,Zooming,在下面的代码中,我试图通过鼠标滚轮缩放图像。但是代码工作不正常,它只是刷新面板,但没有调整面板大小。实际上,我从另一个名为decrypt的类创建的内存流中获取图像。完整图像正确显示,但我无法使用鼠标滚轮事件执行图像缩放。 请帮帮我 private void Form2_Load(object sender, EventArgs e) { this.Width = Screen.PrimaryScreen.WorkingArea.Width; this.Height = Scree
private void Form2_Load(object sender, EventArgs e)
{
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.CenterToScreen();
PicturePanel= new PictureBox();
PicturePanel.Dock = DockStyle.Fill;
//PicturePanel.SizeMode = PictureBoxSizeMode.AutoSize;
//PicturePanel.SizeMode = PictureBoxSizeMode.CenterImage;
PicturePanel.Focus();
//PicturePanel.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.OnMouseWheel);
this.Controls.Add(PicturePanel);
View_codee v = new View_codee();
try
{
PicturePanel.Image = Image.FromStream(Decrypt.ms1);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
this.Name = "";
}
protected override void OnMouseWheel(MouseEventArgs mea)
{
// Override OnMouseWheel event, for zooming in/out with the scroll wheel
if (PicturePanel.Image != null)
{
// If the mouse wheel is moved forward (Zoom in)
if (mea.Delta > 0)
{
// Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
if ((PicturePanel.Width < (15 * this.Width)) && (PicturePanel.Height < (15 * this.Height)))
{
// Change the size of the picturebox, multiply it by the ZOOMFACTOR
PicturePanel.Width = (int)(PicturePanel.Width * 1.25);
PicturePanel.Height = (int)(PicturePanel.Height * 1.25);
// Formula to move the picturebox, to zoom in the point selected by the mouse cursor
PicturePanel.Top = (int)(mea.Y - 1.25 * (mea.Y - PicturePanel.Top));
PicturePanel.Left = (int)(mea.X - 1.25 * (mea.X - PicturePanel.Left));
}
}
else
{
// Check if the pictureBox dimensions are in range (15 is the minimum and maximum zoom level)
if ((PicturePanel.Width > (this.Width / 15)) && (PicturePanel.Height > (this.Height / 15)))
{
// Change the size of the picturebox, divide it by the ZOOMFACTOR
PicturePanel.Width = (int)(PicturePanel.Width / 1.25);
PicturePanel.Height = (int)(PicturePanel.Height / 1.25);
// Formula to move the picturebox, to zoom in the point selected by the mouse cursor
PicturePanel.Top = (int)(mea.Y - 0.80 * (mea.Y - PicturePanel.Top));
PicturePanel.Left = (int)(mea.X - 0.80 * (mea.X - PicturePanel.Left));
}
}
}
}
private void Form2\u加载(对象发送方,事件参数e)
{
this.Width=Screen.PrimaryScreen.WorkingArea.Width;
this.Height=Screen.PrimaryScreen.WorkingArea.Height;
此.CenterToScreen();
PicturePanel=新PictureBox();
PicturePanel.Dock=DockStyle.Fill;
//PicturePanel.SizeMode=PictureBoxSizeMode.AutoSize;
//PicturePanel.SizeMode=PictureBoxSizeMode.CenterImage;
PicturePanel.Focus();
//PicturePanel.MouseWheel+=新系统.Windows.Forms.MouseEventHandler(this.OnMouseWheel);
this.Controls.Add(PicturePanel);
View_codee v=新视图_codee();
尝试
{
PicturePanel.Image=Image.FromStream(Decrypt.ms1);
}
捕获(异常ee)
{
MessageBox.Show(ee.Message);
}
此。名称=”;
}
鼠标滚轮上的受保护超控无效(MouseEventArgs mea)
{
//覆盖OnMouseWheel事件,用于使用滚轮放大/缩小
if(PicturePanel.Image!=null)
{
//如果鼠标滚轮向前移动(放大)
如果(平均增量>0)
{
//检查pictureBox尺寸是否在范围内(15是最小和最大缩放级别)
如果((PicturePanel.Width<(15*此.Width))&(PicturePanel.Height<(15*此.Height)))
{
//更改picturebox的大小,将其乘以ZOOMFACTOR
PicturePanel.Width=(int)(PicturePanel.Width*1.25);
PicturePanel.Height=(int)(PicturePanel.Height*1.25);
//公式移动picturebox,放大鼠标光标选择的点
PicturePanel.Top=(int)(平均值为-1.25*(平均值为-PicturePanel.Top));
PicturePanel.Left=(int)(平均值X-1.25*(平均值X-PicturePanel.Left));
}
}
其他的
{
//检查pictureBox尺寸是否在范围内(15是最小和最大缩放级别)
如果((PicturePanel.Width>(this.Width/15))&(PicturePanel.Height>(this.Height/15)))
{
//更改picturebox的大小,将其除以ZOOMFACTOR
PicturePanel.Width=(int)(PicturePanel.Width/1.25);
PicturePanel.Height=(int)(PicturePanel.Height/1.25);
//公式移动picturebox,放大鼠标光标选择的点
PicturePanel.Top=(int)(平均值为-0.80*(平均值为-PicturePanel.Top));
PicturePanel.Left=(int)(平均值X-0.80*(平均值X-PicturePanel.Left));
}
}
}
}
通过添加新的ImageProperty
更新了代码,因此您可以直接设置图像
public class PictureBox : System.Windows.Forms.UserControl
{
#region Members
private System.Windows.Forms.PictureBox PicBox;
private Panel OuterPanel;
private Container components = null;
private string m_sPicName = "";
#endregion
#region Constants
private double ZOOMFACTOR = 1.25; // = 25% smaller or larger
private int MINMAX = 5; // 5 times bigger or smaller than the ctrl
#endregion
#region Designer generated code
private void InitializeComponent()
{
this.PicBox = new System.Windows.Forms.PictureBox();
this.OuterPanel = new System.Windows.Forms.Panel();
this.OuterPanel.SuspendLayout();
this.SuspendLayout();
//
// PicBox
//
this.PicBox.Location = new System.Drawing.Point(0, 0);
this.PicBox.Name = "PicBox";
this.PicBox.Size = new System.Drawing.Size(150, 140);
this.PicBox.TabIndex = 3;
this.PicBox.TabStop = false;
//
// OuterPanel
//
this.OuterPanel.AutoScroll = true;
this.OuterPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.OuterPanel.Controls.Add(this.PicBox);
this.OuterPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.OuterPanel.Location = new System.Drawing.Point(0, 0);
this.OuterPanel.Name = "OuterPanel";
this.OuterPanel.Size = new System.Drawing.Size(210, 190);
this.OuterPanel.TabIndex = 4;
//
// PictureBox
//
this.Controls.Add(this.OuterPanel);
this.Name = "PictureBox";
this.Size = new System.Drawing.Size(210, 190);
this.OuterPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region Constructors
public PictureBox()
{
InitializeComponent();
InitCtrl(); // my special settings for the ctrl
}
#endregion
#region Properties
private Image _pictureImage;
public Image PictureImage
{
get { return _pictureImage; }
set
{
if (null != value)
{
try
{
PicBox.Image = value;
_pictureImage = value;
}
catch (OutOfMemoryException ex)
{
RedCross();
}
}
else
{
RedCross();
}
}
}
/// <summary>
/// Property to select the picture which is displayed in the picturebox. If the
/// file doesn´t exist or we receive an exception, the picturebox displays
/// a red cross.
/// </summary>
/// <value>Complete filename of the picture, including path information</value>
/// <remarks>Supported fileformat: *.gif, *.tif, *.jpg, *.bmp</remarks>
///
[Browsable(false)]
public string Picture
{
get { return m_sPicName; }
set
{
if (null != value)
{
if (System.IO.File.Exists(value))
{
try
{
PicBox.Image = Image.FromFile(value);
m_sPicName = value;
}
catch (OutOfMemoryException ex)
{
RedCross();
}
}
else
{
RedCross();
}
}
}
}
/// <summary>
/// Set the frametype of the picturbox
/// </summary>
[Browsable(false)]
public BorderStyle Border
{
get { return OuterPanel.BorderStyle; }
set { OuterPanel.BorderStyle = value; }
}
#endregion
#region Other Methods
/// <summary>
/// Special settings for the picturebox ctrl
/// </summary>
private void InitCtrl()
{
PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
PicBox.Location = new Point(0, 0);
OuterPanel.Dock = DockStyle.Fill;
OuterPanel.Cursor = System.Windows.Forms.Cursors.NoMove2D;
OuterPanel.AutoScroll = true;
OuterPanel.MouseEnter += new EventHandler(PicBox_MouseEnter);
PicBox.MouseEnter += new EventHandler(PicBox_MouseEnter);
OuterPanel.MouseWheel += new MouseEventHandler(PicBox_MouseWheel);
}
/// <summary>
/// Create a simple red cross as a bitmap and display it in the picturebox
/// </summary>
private void RedCross()
{
Bitmap bmp = new Bitmap(OuterPanel.Width, OuterPanel.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics gr;
gr = Graphics.FromImage(bmp);
Pen pencil = new Pen(Color.Red, 5);
gr.DrawLine(pencil, 0, 0, OuterPanel.Width, OuterPanel.Height);
gr.DrawLine(pencil, 0, OuterPanel.Height, OuterPanel.Width, 0);
PicBox.Image = bmp;
gr.Dispose();
}
#endregion
#region Zooming Methods
/// <summary>
/// Make the PictureBox dimensions larger to effect the Zoom.
/// </summary>
/// <remarks>Maximum 5 times bigger</remarks>
private void ZoomIn()
{
if ((PicBox.Width < (MINMAX * OuterPanel.Width)) &&
(PicBox.Height < (MINMAX * OuterPanel.Height)))
{
PicBox.Width = Convert.ToInt32(PicBox.Width * ZOOMFACTOR);
PicBox.Height = Convert.ToInt32(PicBox.Height * ZOOMFACTOR);
PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
/// <summary>
/// Make the PictureBox dimensions smaller to effect the Zoom.
/// </summary>
/// <remarks>Minimum 5 times smaller</remarks>
private void ZoomOut()
{
if ((PicBox.Width > (OuterPanel.Width / MINMAX)) &&
(PicBox.Height > (OuterPanel.Height / MINMAX)))
{
PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
PicBox.Width = Convert.ToInt32(PicBox.Width / ZOOMFACTOR);
PicBox.Height = Convert.ToInt32(PicBox.Height / ZOOMFACTOR);
}
}
#endregion
#region Mouse events
/// <summary>
/// We use the mousewheel to zoom the picture in or out
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PicBox_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta < 0)
{
ZoomIn();
}
else
{
ZoomOut();
}
}
/// <summary>
/// Make sure that the PicBox have the focus, otherwise it doesn´t receive
/// mousewheel events !.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PicBox_MouseEnter(object sender, EventArgs e)
{
if (PicBox.Focused == false)
{
PicBox.Focus();
}
}
#endregion
#region Disposing
/// <summary>
/// Die verwendeten Ressourcen bereinigen.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
private void Form2_Load(object sender, EventArgs e)
{
pictureBox1.PictureImage = Image.FromStream(Decrypt.ms1);
}
公共类PictureBox:System.Windows.Forms.UserControl
{
#区域成员
private System.Windows.Forms.PictureBox PicBox;
专用面板外板;
私有容器组件=null;
私有字符串m_sPicName=“”;
#端区
#区域常数
专用双缩放因子=1.25;//=25%更小或更大
private int MINMAX=5;//比ctrl大或小5倍
#端区
#区域设计器生成的代码
私有void InitializeComponent()
{
this.PicBox=new System.Windows.Forms.PictureBox();
this.OuterPanel=new System.Windows.Forms.Panel();
this.OuterPanel.SuspendLayout();
这个.SuspendLayout();
//
//PicBox
//
this.PicBox.Location=新系统图点(0,0);
this.PicBox.Name=“PicBox”;
this.PicBox.Size=新系统图纸尺寸(150140);
this.PicBox.TabIndex=3;
this.PicBox.TabStop=false;
//
//外板
//
this.OuterPanel.AutoScroll=true;
this.OuterPanel.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.OuterPanel.Controls.Add(this.PicBox);
this.OuterPanel.Dock=System.Windows.Forms.DockStyle.Fill;
this.OuterPanel.Location=新系统.Drawing.Point(0,0);
this.OuterPanel.Name=“OuterPanel”;
this.OuterPanel.Size=新系统图尺寸(210190);
this.OuterPanel.TabIndex=4;
//
//图片盒
//
this.Controls.Add(this.OuterPanel);
this.Name=“PictureBox”;
该尺寸=新系统图纸尺寸(210190);
this.OuterPanel.ResumeLayout(false);
此选项为.resume布局(false);
}
#端区
#区域构造函数
公共图片盒()
{
初始化组件();
InitCtrl();//我对ctrl的特殊设置
}
#端区
#区域属性
私人图像(图片图像),;
公众形象图片
{
获取{return\u pictureImage;}
设置
{
if(null!=值)
{
尝试
{
PicBox.Image=值;
_图片图像=值;
}
捕获(OutOfMemoryException ex)
{
红十字会();
}
}
其他的
{
红十字会();
}
}
}
///
///对s的属性