菜单项
|
子项
|
其他属性
|
图片(&P)
|
打开图片(&O)
|
快捷键等其他属性根据自己设计定(下同)
|
复制图片(&C)
|
|
|
关闭(&Q)
|
|
|
///======================================代码编辑===========================================
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.IO;
namespace
FileOptionApplication
{
public partial class Form12 : Form
{
public Form12()
{
InitializeComponent();
}
/// <summary>
/*******************************************************
**方 法 名:GetFileBytes
**输 入参数:Filename:文件名称;
**输 出参数:比特流类型
**返 回 值:byte[]二进制流
**创 建 人:钱哨
**创 建日期: 09-7-9
**描 述:将读取的文件转化成为二进制流。
*******************************************************/
/// <summary>
/// </summary>
/// <param name="Filename">打开的图片具体路径及文件名称</param>
/// <returns>比特流类型</returns>
public byte[] GetFileBytes(string Filename)
{
if (Filename == "")
return null;
try
{
FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
binaryReader.Close();
fileStream.Close();
return fileBytes;
}
catch
{
return null;
}
}
/*******************************************************
**方 法 名:WriteFileBytes
**输 入参数:TargetFilename:目标文件名称;
**输 出参数:布尔类型:是否写成功
**返 回 值:byte[]二进制流
**创 建 人:钱哨
**创 建日期: 09-7-9
**描 述:将读取的文件转化成为二进制流。
*******************************************************/
/// </summary>
/// <param name="TargetFilename">目标文件</param>
/// <param name="fileBytes">文件比特流</param>
/// <returns>布尔类型:是否写成功</returns>
public bool WriteFileBytes(string TargetFilename, byte[] fileBytes)
{
bool k = true;
if (TargetFilename != "" && fileBytes.Length != 0)
{
try
{
FileStream fileStream = new FileStream(TargetFilename, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter binaryWriter = new BinaryWriter(fileStream);
binaryWriter.Write(fileBytes);
binaryWriter.Flush();
binaryWriter.Close();
fileStream.Close();
}
catch
{
k = false;
}
}
else
{
k = false;
}
return k;
}
/// <summary>
/// 菜单:打开图片
/// </summary>
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.bmp)|*.bmp";
if (openfile.ShowDialog() == DialogResult.OK)
{
byte[] picbinary = GetFileBytes(openfile.FileName);
//第一步:打开图片文件,获得比特流
MemoryStream mempicstream = new MemoryStream(picbinary);
//第二步:将比特流还存在内存工作流中。
pictureBox1.Image = Image.FromStream(mempicstream);
//第三步:加载内存流到图片控件
mempicstream.Dispose();
mempicstream.Close();
}
}
catch (Exception m)
{
MessageBox.Show("读取图片出错,可能的问题是:"+Convert.ToString(m) ,"错误提示");
}
}
/// <summary>
/// 将打开的图片进行复制
/// </summary>
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
try
{
if (pictureBox1.Image == null)
{
MessageBox.Show("禁止图片为空时候另存信息。", "错误提示");
}
else
{
saveFileDialog1.Filter = "jpg类型图片(*.jpg)|*.jpg";
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
MemoryStream ms=new MemoryStream();
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
WriteFileBytes(saveFileDialog1.FileName, bytes);
MessageBox.Show("另存图片成功", "提示");
ms.Dispose();
ms.Close();
bm.Dispose();
}
}
}
catch (Exception m)
{
MessageBox.Show("读取图片出错,可能的问题是:" + Convert.ToString(m), "错误提示");
}
}
}
}
|
本文出自 “熊猫写程序” 博客,转载请与作者联系!