winform程序中打开和保存一幅图像

      作为一个c#的初学者,开始感觉自己对于这方面的知识很茫然,懂得非常少,但是我没有放弃,所以我相信坚持下去会好的,对于每一位初学者,大家都不要放弃,大家彼此互勉,下面和大家分享下我写的一个简单的小程序:

      打开和保存一幅图像。本人用的vs2010中的winform利用button控件、pictureBox控件;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace 对话框的使用
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Title = "打开一幅图像";
ofd.Filter = "媒体文件(*.mp4)|*.mp4|图片文件(*.jpg)|*.jpg|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
ofd.ShowDialog();
string path = ofd.FileName;
pictureBox1.Image = Image.FromFile(path);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "保存图片";
sfd.Filter = "图片文件(*bmp)|*.bmp";
sfd.FilterIndex = 0;
sfd.ShowDialog();
string path = sfd.FileName;
using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Save(fsWrite, System.Drawing.Imaging.ImageFormat.Bmp);
}

}
}
}
}

 

posted on 2016-10-24 15:35  指尖&流沙  阅读(206)  评论(0编辑  收藏  举报

导航