任务要求:利用PictureBox控件设计一个图片查看器
时间: 2023-08-08 15:07:26 浏览: 98
好的,我可以帮你设计一个基于PictureBox控件的图片查看器。首先,你需要在窗体上添加一个PictureBox控件,它将用于显示图片。然后,你可以添加一些按钮来实现图片的打开、保存、缩放等功能。
下面是一个简单的示例代码,可以帮助你开始编写这个图片查看器:
```csharp
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files (*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(openFileDialog.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "JPEG Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
switch (saveFileDialog.FilterIndex)
{
case 1:
pictureBox1.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
pictureBox1.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
pictureBox1.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
}
}
}
private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Width += 10;
pictureBox1.Height += 10;
}
private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Width -= 10;
pictureBox1.Height -= 10;
}
}
```
这个示例代码实现了打开、保存、缩放等功能。你可以根据自己的需求来添加更多的功能。希望这能帮到你!
阅读全文