private void SaveImage_Click(object sender, EventArgs e) { try { saveFileDialog.Filter = "图片(*.bmp)|*.bmp"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { MyNegImage.Save(saveFileDialog.FileName); MessageBox.Show("保存成功!"); } }啥意思
时间: 2024-02-10 10:34:29 浏览: 34
这是一个 C# 的 WinForms 代码段,用于实现保存图像的功能。其中,SaveImage_Click 是一个按钮的点击事件,当用户点击该按钮时,会弹出一个 saveFileDialog 对话框,用于选择保存的文件名和路径。如果用户确定保存,就会调用 MyNegImage.Save 方法,将图像保存到指定的文件中。最后,弹出一个消息框提示保存成功。其中,Filter 属性用于指定 saveFileDialog 对话框中文件类型的过滤器,这里只显示 bmp 格式的文件。如果用户选择其他格式的文件,将无法保存成功。
相关问题
任务要求:利用PictureBox控件设计一个图片查看器
好的,我可以帮你设计一个基于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;
}
}
```
这个示例代码实现了打开、保存、缩放等功能。你可以根据自己的需求来添加更多的功能。希望这能帮到你!
你能帮我使用c#,写一串代码,要求是:制作一个可视化类似于word的多文档应用,并插入图片作为封面,
当然可以,以下是一段参考代码:
```csharp
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace MultiDocumentApp
{
public partial class MainForm : Form
{
private int documentCount = 0;
public MainForm()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
documentCount++;
var newDoc = new DocumentForm($"Document {documentCount}");
newDoc.MdiParent = this;
newDoc.Show();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var newDoc = new DocumentForm(openFileDialog.FileName);
newDoc.MdiParent = this;
newDoc.Show();
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
var activeDoc = this.ActiveMdiChild as DocumentForm;
if (activeDoc != null)
{
activeDoc.SaveDocument();
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
var activeDoc = this.ActiveMdiChild as DocumentForm;
if (activeDoc != null)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
activeDoc.SaveDocumentAs(saveFileDialog.FileName);
}
}
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_Load(object sender, EventArgs e)
{
var coverImage = Image.FromFile(@"C:\cover.jpg");
this.BackgroundImage = coverImage;
}
}
public partial class DocumentForm : Form
{
private string filename = "";
private bool isSaved = true;
public DocumentForm(string title)
{
InitializeComponent();
this.Text = title;
}
public DocumentForm(string filename, string title = "") : this(title)
{
this.filename = filename;
this.textBox.Text = File.ReadAllText(filename);
}
private void textBox_TextChanged(object sender, EventArgs e)
{
this.isSaved = false;
}
public void SaveDocument()
{
if (string.IsNullOrEmpty(filename))
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
this.filename = saveFileDialog.FileName;
}
}
}
if (!string.IsNullOrEmpty(filename))
{
File.WriteAllText(filename, this.textBox.Text);
this.isSaved = true;
}
}
public void SaveDocumentAs(string filename)
{
this.filename = filename;
this.SaveDocument();
}
private void DocumentForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!this.isSaved)
{
var result = MessageBox.Show("Do you want to save changes?", "Save Changes", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
this.SaveDocument();
}
else if (result == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}
}
}
```
这段代码实现了一个多文档应用,包含了文件的新建、打开、保存、另存为等操作。同时,主界面载入时会显示一张封面图片。你可以根据需要修改代码,比如增加插入图片的功能。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)