设置pictureBox.sizemode为CenterImage 可以将图片显示在当中下面的示例将向 PictureBox 控件添加水平和垂直的滚动条,并在发生该控件的 DoubleClick 事件时在图片框中加载一个 Image。在任何时候都可以双击该图片框以加载一个新图像。如果图像大于控件,则会显示滚动条,以便您进行滚动以查看到图像的其他部分。该示例假定已在 Form 上实例化了一个 PictureBox(它的两个边缘上分别停靠一个 VScrollBar 和 HScrollBar)。它还假定已添加了一个对 System.Drawing 命名空间的引用。确保 HandleScroll 方法已设置为 VScrollBar 和 HScrollBar 两者的 Scroll 事件处理程序委托。有关可扩展此示例的其他代码,请参阅 LargeChange、SmallChange、Maximum、Minimum 或 Value 成员。可以考虑在其父控件属性的基础上设置滚动条的属性。例如,可以给该示例添加一个方法来设置 Maximum 值,使该值等于分配给 PictureBox 的 Image 的 Height 或 Width。设置 LargeChange 值,使之等于 PictureBox 的高度或宽度(减去滚动条的高度或宽度)。这样就避免了用户滚动时超出图像的边缘,而 LargeChange 则使得图像可见区域移动的距离只能同图片框中显示的区域一样大。[Visual Basic]
Protected Sub pictureBox1_DoubleClick(sender As Object, e As EventArgs)
' Open the dialog box so the user can select a new image.
If openFileDialog1.ShowDialog() <> DialogResult.Cancel Then
' Display the image in the PictureBox.
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName)
' Add code to set the scroll bar properties, such as
' Minimum, Maximum, LargeScroll, and SmallScroll.
Me.DisplayScrollBars()
End If
End Sub Protected Sub Form1_Resize(sender As Object, e As EventArgs)
' If the PictureBox has an image, see if it needs
' scrollbars and refresh the image.
If Not (pictureBox1.Image Is Nothing) Then
Me.SetScrollBarValues()
Me.DisplayScrollBars()
Me.Refresh()
End If
End SubPublic Sub DisplayScrollBars()
' If the image is wider than the PictureBox, show the HScrollBar.
If pictureBox1.Width > pictureBox1.Image.Width Then
hScrollBar1.Visible = False
Else
hScrollBar1.Visible = True
End If
' If the image is taller than the PictureBox, show the VScrollBar.
If pictureBox1.Height > pictureBox1.Image.Height Then
vScrollBar1.Visible = False
Else
vScrollBar1.Visible = True
End If
End Sub Public Sub HandleScroll(sender As Object, se As ScrollEventArgs)
' Creates a graphics object and draws a portion
' of the image in the PictureBox.
Dim g As Graphics = pictureBox1.CreateGraphics()
g.DrawImage(pictureBox1.Image, New Rectangle(0, 0, _
pictureBox1.Width - hScrollBar1.Width, _
pictureBox1.Height - vScrollBar1.Height), _
New Rectangle(hScrollBar1.Value, vScrollBar1.Value, _
pictureBox1.Width - hScrollBar1.Width, _
pictureBox1.Height - vScrollBar1.Height), GraphicsUnit.Pixel)
End Sub
[C#]
protected void pictureBox1_DoubleClick (Object sender,
EventArgs e)
{
// Open the dialog box so the user can select a new image.
if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
// Display the image in the PictureBox.
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
/* Add code to set the scroll bar properties, such as
Minimum, Maximum, LargeScroll, and SmallScroll. */
this.DisplayScrollBars();
}
}
protected void Form1_Resize (Object sender,
EventArgs e)
{
/* If the PictureBox has an image, see if it needs
scrollbars and refresh the image. */
if(pictureBox1.Image != null)
{
this.SetScrollBarValues();
this.DisplayScrollBars();
this.Refresh();
}
}
public void DisplayScrollBars()
{
// If the image is wider than the PictureBox, show the HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}
// If the image is taller than the PictureBox, show the VScrollBar.
if (pictureBox1.Height > pictureBox1.Image.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}
public void HandleScroll(Object sender,
ScrollEventArgs se)
{
/* Create a graphics object and draw a portion
of the image in the PictureBox. */
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height),
new Rectangle (hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height), GraphicsUnit.Pixel);
}
[JScript]
protected function pictureBox1_DoubleClick (sender : Object,
e : EventArgs)
{
// Open the dialog so the user can select a new image.
if(openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
// Open the image in the PictureBox and display
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
/* Add code to set the scrollbars properties, such as
Minimum, Maximum, LargeScroll, and SmallScroll. */
this.DisplayScrollBars();
}
}
protected function Form1_Resize (sender : Object,
e : EventArgs)
{
/* If the PictureBox has an image, see if it needs
scrollbars and refresh the image. */
if(pictureBox1.Image != null)
{
this.SetScrollBarValues();
this.DisplayScrollBars();
this.Refresh();
}
}
public function DisplayScrollBars()
{
// If the image is wider than the PictureBox, show the HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}
// If the image is taller than the PictureBox, show the VScrollBar.
if (pictureBox1.Height > pictureBox1.Image.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}
public function HandleScroll(sender : Object,
se : ScrollEventArgs)
{
/* Creates a graphics object and draws a portion
of the image in the PictureBox. */
var g : Graphics = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height),
new Rectangle (hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height), GraphicsUnit.Pixel);
}
[C++] 没有可用于 C++ 的示例。若要查看 Visual Basic、C# 或 JScript 示例,请单击页左上角的语言筛选器按钮 。
Protected Sub pictureBox1_DoubleClick(sender As Object, e As EventArgs)
' Open the dialog box so the user can select a new image.
If openFileDialog1.ShowDialog() <> DialogResult.Cancel Then
' Display the image in the PictureBox.
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName)
' Add code to set the scroll bar properties, such as
' Minimum, Maximum, LargeScroll, and SmallScroll.
Me.DisplayScrollBars()
End If
End Sub Protected Sub Form1_Resize(sender As Object, e As EventArgs)
' If the PictureBox has an image, see if it needs
' scrollbars and refresh the image.
If Not (pictureBox1.Image Is Nothing) Then
Me.SetScrollBarValues()
Me.DisplayScrollBars()
Me.Refresh()
End If
End SubPublic Sub DisplayScrollBars()
' If the image is wider than the PictureBox, show the HScrollBar.
If pictureBox1.Width > pictureBox1.Image.Width Then
hScrollBar1.Visible = False
Else
hScrollBar1.Visible = True
End If
' If the image is taller than the PictureBox, show the VScrollBar.
If pictureBox1.Height > pictureBox1.Image.Height Then
vScrollBar1.Visible = False
Else
vScrollBar1.Visible = True
End If
End Sub Public Sub HandleScroll(sender As Object, se As ScrollEventArgs)
' Creates a graphics object and draws a portion
' of the image in the PictureBox.
Dim g As Graphics = pictureBox1.CreateGraphics()
g.DrawImage(pictureBox1.Image, New Rectangle(0, 0, _
pictureBox1.Width - hScrollBar1.Width, _
pictureBox1.Height - vScrollBar1.Height), _
New Rectangle(hScrollBar1.Value, vScrollBar1.Value, _
pictureBox1.Width - hScrollBar1.Width, _
pictureBox1.Height - vScrollBar1.Height), GraphicsUnit.Pixel)
End Sub
[C#]
protected void pictureBox1_DoubleClick (Object sender,
EventArgs e)
{
// Open the dialog box so the user can select a new image.
if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
// Display the image in the PictureBox.
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
/* Add code to set the scroll bar properties, such as
Minimum, Maximum, LargeScroll, and SmallScroll. */
this.DisplayScrollBars();
}
}
protected void Form1_Resize (Object sender,
EventArgs e)
{
/* If the PictureBox has an image, see if it needs
scrollbars and refresh the image. */
if(pictureBox1.Image != null)
{
this.SetScrollBarValues();
this.DisplayScrollBars();
this.Refresh();
}
}
public void DisplayScrollBars()
{
// If the image is wider than the PictureBox, show the HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}
// If the image is taller than the PictureBox, show the VScrollBar.
if (pictureBox1.Height > pictureBox1.Image.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}
public void HandleScroll(Object sender,
ScrollEventArgs se)
{
/* Create a graphics object and draw a portion
of the image in the PictureBox. */
Graphics g = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height),
new Rectangle (hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height), GraphicsUnit.Pixel);
}
[JScript]
protected function pictureBox1_DoubleClick (sender : Object,
e : EventArgs)
{
// Open the dialog so the user can select a new image.
if(openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
// Open the image in the PictureBox and display
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
/* Add code to set the scrollbars properties, such as
Minimum, Maximum, LargeScroll, and SmallScroll. */
this.DisplayScrollBars();
}
}
protected function Form1_Resize (sender : Object,
e : EventArgs)
{
/* If the PictureBox has an image, see if it needs
scrollbars and refresh the image. */
if(pictureBox1.Image != null)
{
this.SetScrollBarValues();
this.DisplayScrollBars();
this.Refresh();
}
}
public function DisplayScrollBars()
{
// If the image is wider than the PictureBox, show the HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}
// If the image is taller than the PictureBox, show the VScrollBar.
if (pictureBox1.Height > pictureBox1.Image.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}
public function HandleScroll(sender : Object,
se : ScrollEventArgs)
{
/* Creates a graphics object and draws a portion
of the image in the PictureBox. */
var g : Graphics = pictureBox1.CreateGraphics();
g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height),
new Rectangle (hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Width - hScrollBar1.Width,
pictureBox1.Height - vScrollBar1.Height), GraphicsUnit.Pixel);
}
[C++] 没有可用于 C++ 的示例。若要查看 Visual Basic、C# 或 JScript 示例,请单击页左上角的语言筛选器按钮 。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货