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;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
byte[] bytes;//用于存放图片字节数组
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "*.jpg|*.jpg|*.gif|*.gif";
openFileDialog1.ShowDialog();
string openfile = this.openFileDialog1.FileName;
this.label1.Text = openfile;
if (string.IsNullOrEmpty(openfile)) return;//选择取消,则返回
this.pictureBox1.Image = Image.FromFile(openfile);
//设置图片显示模式
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//之下是使用文件流将图片保存至字节数组
FileStream fs = new FileStream(openfile, FileMode.Open, FileAccess.Read);
bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
this.saveFileDialog1.Filter = "*.jpg|*.jpg|*.gif|*.gif";
saveFileDialog1.ShowDialog();
string savefile = this.saveFileDialog1.FileName;
if (string.IsNullOrEmpty(savefile)) return;//选择取消,则返回
if (bytes.Length == 0)
{
MessageBox.Show("先选图片再复制");
return;
}
//之下是使用文件流将保存在字节数组中数据复制到指定文件savefile
FileStream fs = new FileStream(savefile, FileMode.Create, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
MessageBox.Show("图片复制成功");
fs.Dispose();
}
}
}