wiform 缩放图片 用winform 如果实现这种缩放图片? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.codeproject.com/Articles/20018/Gradients-made-easy picturebox自己就可以实现缩放。PictureBox.SizeMode 设成StretchImage调整picturebox大小就可以自动缩放了。 使用 StretchImage 值会使将图像拉伸,以便适合 PictureBox 的大小这并不是我要的效果。 这个可以插入图片的哦,亲~(结贴给分的话,可以返还一半可用分)附:我以前用过的一个简单的,叫做pickbox,忘了从哪下的了。/* * 文件名:PickBox * 来源:codeproject * 功能:为窗体控件添加鼠标拖动和调整尺寸功能 * 用法: * PickBox pb = new PickBox();//创建实例 * pb.WireControl(panel1);//绑定控件 * pb.WireControl(pictureBox1); * pb.WireControlCancel(this);//点击该控件时取消之前选中 */using System;using System.Windows.Forms;using System.Drawing;//namespace PickBox//{ /// <summary> /// This class implements sizing and moving functions for /// runtime editing of graphic controls /// </summary> public class PickBox { private const int BOX_SIZE = 8; private Color BOX_COLOR = Color.White; //private ContainerControl m_container; private Control m_control; private Label[] lbl = new Label[8]; private int startl; private int startt; private int startw; private int starth; private int startx; private int starty; private bool dragging; private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE}; private Cursor oldCursor; private const int MIN_SIZE = 20; // // Constructor creates 8 sizing handles & wires mouse events // to each that implement sizing functions // public PickBox() { for (int i = 0; i < 8; i++) { lbl[i] = new Label(); lbl[i].TabIndex = i; lbl[i].FlatStyle = 0; lbl[i].BorderStyle = BorderStyle.FixedSingle; lbl[i].BackColor = BOX_COLOR; lbl[i].Cursor = arrArrow[i]; lbl[i].Text = ""; lbl[i].BringToFront(); lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown); lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove); lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp); } } ////////////////////////////////////////////////////////////////// // PUBLIC METHODS ////////////////////////////////////////////////////////////////// // // Wires a Click event handler to the passed Control // that attaches a pick box to the control when it is clicked // public void WireControl(Control ctl) { ctl.Click += new EventHandler(this.SelectControl); } // HideHandles public void WireControlCancel(Control ctl) { ctl.Click += new EventHandler(this.CancelControl); } ///////////////////////////////////////////////////////////////// // PRIVATE METHODS ///////////////////////////////////////////////////////////////// // // click to cancel select // private void CancelControl(object sender, EventArgs e) { HideHandles(); } // // Attaches a pick box to the sender Control // private void SelectControl(object sender, EventArgs e) { if (m_control is Control) { m_control.Cursor = oldCursor; //Remove event any pre-existing event handlers appended by this class m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown); m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove); m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp); m_control = null; } m_control = (Control)sender; //Add event handlers for moving the selected control around m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown); m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove); m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp); //Add sizing handles to Control's container (Form or PictureBox) for (int i = 0; i < 8; i++) { m_control.Parent.Controls.Add(lbl[i]); lbl[i].BringToFront(); } //Position sizing handles around Control MoveHandles(); //Display sizing handles ShowHandles(); oldCursor = m_control.Cursor; m_control.Cursor = Cursors.SizeAll; } public void Remove() { HideHandles(); //m_control.Cursor = oldCursor; } private void ShowHandles() { if (m_control != null) { for (int i = 0; i < 8; i++) { lbl[i].Visible = true; } } } private void HideHandles() { for (int i = 0; i < 8; i++) { lbl[i].Visible = false; } } private void MoveHandles() { int sX = m_control.Left - BOX_SIZE; int sY = m_control.Top - BOX_SIZE; int sW = m_control.Width + BOX_SIZE; int sH = m_control.Height + BOX_SIZE; int hB = BOX_SIZE / 2; int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB, sX + sW-hB, sX + sW / 2, sX+hB, sX+hB}; int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB, sY + sH-hB, sY + sH-hB, sY + sH / 2}; for (int i = 0; i < 8; i++) lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE); } ///////////////////////////////////////////////////////////////// // MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL ///////////////////////////////////////////////////////////////// // // Store control position and size when mouse button pushed over // any sizing handle // private void lbl_MouseDown(object sender, MouseEventArgs e) { dragging = true; startl = m_control.Left; startt = m_control.Top; startw = m_control.Width; starth = m_control.Height; HideHandles(); } // // Size the picked control in accordance with sizing handle being dragged // 0 1 2 // 7 3 // 6 5 4 // private void lbl_MouseMove(object sender, MouseEventArgs e) { int l = m_control.Left; int w = m_control.Width; int t = m_control.Top; int h = m_control.Height; if (dragging) { switch (((Label)sender).TabIndex) { case 0: // Dragging top-left sizing box l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; w = startl + startw - m_control.Left; h = startt + starth - m_control.Top; break; case 1: // Dragging top-center sizing box t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - m_control.Top; break; case 2: // Dragging top-right sizing box w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - m_control.Top; break; case 3: // Dragging right-middle sizing box w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; break; case 4: // Dragging right-bottom sizing box w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 5: // Dragging center-bottom sizing box h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 6: // Dragging left-bottom sizing box l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - m_control.Left; h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 7: // Dragging left-middle sizing box l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - m_control.Left; break; } // 左侧和顶部不移出边框,去掉 //l = (l < 0) ? 0 : l; //t = (t < 0) ? 0 : t; m_control.SetBounds(l, t, w, h); } } // // Display sizing handles around picked control once sizing has completed // private void lbl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); } ///////////////////////////////////////////////////////////////// // MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM ///////////////////////////////////////////////////////////////// // // Get mouse pointer starting position on mouse down and hide sizing handles // private void ctl_MouseDown(object sender, MouseEventArgs e) { dragging = true; startx = e.X; starty = e.Y; HideHandles(); } // // Reposition the dragged control // private void ctl_MouseMove(object sender, MouseEventArgs e) { if (dragging) { int l = m_control.Left + e.X - startx; int t = m_control.Top + e.Y - starty; int w = m_control.Width; int h = m_control.Height; // 边界判断,现在改为可移出边界 l = ((l + w) < 0) ? (0 - w) : ((l > m_control.Parent.ClientRectangle.Width) ? m_control.Parent.ClientRectangle.Width : l); t = ((t + h) < 0) ? (0 - h) : ((t > m_control.Parent.ClientRectangle.Height) ? m_control.Parent.ClientRectangle.Height : t); //l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ? // m_control.Parent.ClientRectangle.Width - w : l); //t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ? //m_control.Parent.ClientRectangle.Height - h : t); m_control.Left = l; m_control.Top = t; } } // // Display sizing handles around picked control once dragging has completed // private void ctl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); } }//} 【200分求助】如何播放内存中的媒体流? 求解释代码 作用:读取ppt中的数据,但是每次读取的时候 程序都会先打开ppt软件 然后再读数据 但是我希望 不出现ppt打开的界面 不知道有什么好的方法?? 未标记为序列化 0分提问....只用is int.是否能判断1个数是否能整除另外1个数 Lisview的问题,希望知道的人来看看 想暴了头,求实现想visio一样画流程图的思路 端口检测(急) 怎么在一个项目里检查是否存在一个类 可不可以同时实现windows NT验证和form验证 关于C#的登录界面 .net类库现在有没有多媒体相关的类,问题内详 如何用数组内的每一个变量来实例化一个类
PictureBox.SizeMode 设成StretchImage
调整picturebox大小就可以自动缩放了。
这并不是我要的效果。
(结贴给分的话,可以返还一半可用分)
附:我以前用过的一个简单的,叫做pickbox,忘了从哪下的了。/*
* 文件名:PickBox
* 来源:codeproject
* 功能:为窗体控件添加鼠标拖动和调整尺寸功能
* 用法:
* PickBox pb = new PickBox();//创建实例
* pb.WireControl(panel1);//绑定控件
* pb.WireControl(pictureBox1);
* pb.WireControlCancel(this);//点击该控件时取消之前选中
*/using System;
using System.Windows.Forms;
using System.Drawing;//namespace PickBox
//{
/// <summary>
/// This class implements sizing and moving functions for
/// runtime editing of graphic controls
/// </summary>
public class PickBox
{
private const int BOX_SIZE = 8;
private Color BOX_COLOR = Color.White;
//private ContainerControl m_container;
private Control m_control;
private Label[] lbl = new Label[8];
private int startl;
private int startt;
private int startw;
private int starth;
private int startx;
private int starty;
private bool dragging;
private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
Cursors.SizeNESW, Cursors.SizeWE};
private Cursor oldCursor; private const int MIN_SIZE = 20; //
// Constructor creates 8 sizing handles & wires mouse events
// to each that implement sizing functions
//
public PickBox()
{
for (int i = 0; i < 8; i++)
{
lbl[i] = new Label();
lbl[i].TabIndex = i;
lbl[i].FlatStyle = 0;
lbl[i].BorderStyle = BorderStyle.FixedSingle;
lbl[i].BackColor = BOX_COLOR;
lbl[i].Cursor = arrArrow[i];
lbl[i].Text = "";
lbl[i].BringToFront();
lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown);
lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove);
lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp);
}
} //////////////////////////////////////////////////////////////////
// PUBLIC METHODS
////////////////////////////////////////////////////////////////// //
// Wires a Click event handler to the passed Control
// that attaches a pick box to the control when it is clicked
//
public void WireControl(Control ctl)
{
ctl.Click += new EventHandler(this.SelectControl);
} // HideHandles
public void WireControlCancel(Control ctl)
{
ctl.Click += new EventHandler(this.CancelControl);
}
/////////////////////////////////////////////////////////////////
// PRIVATE METHODS
/////////////////////////////////////////////////////////////////
//
// click to cancel select
//
private void CancelControl(object sender, EventArgs e)
{
HideHandles();
} //
// Attaches a pick box to the sender Control
//
private void SelectControl(object sender, EventArgs e)
{ if (m_control is Control)
{
m_control.Cursor = oldCursor; //Remove event any pre-existing event handlers appended by this class
m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp); m_control = null;
} m_control = (Control)sender;
//Add event handlers for moving the selected control around
m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp); //Add sizing handles to Control's container (Form or PictureBox)
for (int i = 0; i < 8; i++)
{
m_control.Parent.Controls.Add(lbl[i]);
lbl[i].BringToFront();
} //Position sizing handles around Control
MoveHandles(); //Display sizing handles
ShowHandles(); oldCursor = m_control.Cursor;
m_control.Cursor = Cursors.SizeAll; } public void Remove()
{
HideHandles();
//m_control.Cursor = oldCursor;
} private void ShowHandles()
{
if (m_control != null)
{
for (int i = 0; i < 8; i++)
{
lbl[i].Visible = true;
}
}
} private void HideHandles()
{
for (int i = 0; i < 8; i++)
{
lbl[i].Visible = false;
}
} private void MoveHandles()
{
int sX = m_control.Left - BOX_SIZE;
int sY = m_control.Top - BOX_SIZE;
int sW = m_control.Width + BOX_SIZE;
int sH = m_control.Height + BOX_SIZE;
int hB = BOX_SIZE / 2;
int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,
sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};
int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,
sY + sH-hB, sY + sH-hB, sY + sH / 2};
for (int i = 0; i < 8; i++)
lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
} /////////////////////////////////////////////////////////////////
// MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL
///////////////////////////////////////////////////////////////// //
// Store control position and size when mouse button pushed over
// any sizing handle
//
private void lbl_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startl = m_control.Left;
startt = m_control.Top;
startw = m_control.Width;
starth = m_control.Height;
HideHandles();
} //
// Size the picked control in accordance with sizing handle being dragged
// 0 1 2
// 7 3
// 6 5 4
//
private void lbl_MouseMove(object sender, MouseEventArgs e)
{
int l = m_control.Left;
int w = m_control.Width;
int t = m_control.Top;
int h = m_control.Height;
if (dragging)
{
switch (((Label)sender).TabIndex)
{
case 0: // Dragging top-left sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
w = startl + startw - m_control.Left;
h = startt + starth - m_control.Top;
break;
case 1: // Dragging top-center sizing box
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
h = startt + starth - m_control.Top;
break;
case 2: // Dragging top-right sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
h = startt + starth - m_control.Top;
break;
case 3: // Dragging right-middle sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
break;
case 4: // Dragging right-bottom sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 5: // Dragging center-bottom sizing box
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 6: // Dragging left-bottom sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
w = startl + startw - m_control.Left;
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 7: // Dragging left-middle sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
w = startl + startw - m_control.Left;
break;
}
// 左侧和顶部不移出边框,去掉
//l = (l < 0) ? 0 : l;
//t = (t < 0) ? 0 : t;
m_control.SetBounds(l, t, w, h);
}
} //
// Display sizing handles around picked control once sizing has completed
//
private void lbl_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
MoveHandles();
ShowHandles();
} /////////////////////////////////////////////////////////////////
// MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM
///////////////////////////////////////////////////////////////// //
// Get mouse pointer starting position on mouse down and hide sizing handles
//
private void ctl_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startx = e.X;
starty = e.Y;
HideHandles();
} //
// Reposition the dragged control
//
private void ctl_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
int l = m_control.Left + e.X - startx;
int t = m_control.Top + e.Y - starty;
int w = m_control.Width;
int h = m_control.Height; // 边界判断,现在改为可移出边界
l = ((l + w) < 0) ? (0 - w) : ((l > m_control.Parent.ClientRectangle.Width) ?
m_control.Parent.ClientRectangle.Width : l);
t = ((t + h) < 0) ? (0 - h) : ((t > m_control.Parent.ClientRectangle.Height) ?
m_control.Parent.ClientRectangle.Height : t);
//l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ?
// m_control.Parent.ClientRectangle.Width - w : l);
//t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ?
//m_control.Parent.ClientRectangle.Height - h : t);
m_control.Left = l;
m_control.Top = t;
}
} //
// Display sizing handles around picked control once dragging has completed
//
private void ctl_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
MoveHandles();
ShowHandles();
} }
//}