怎么把JPG格式图片转化成二进制数据有
using System;
using System。Drawing;
using System。Collections;
using System。ComponentModel;
using System。 Windows。Forms;
using System。Data;
// 添加新的命名空间。
using System。Runtime。Serialization;
using System。 Runtime。Serialization。Formatters。Binary;
using System。IO;
namespace Seri...全部
using System;
using System。Drawing;
using System。Collections;
using System。ComponentModel;
using System。
Windows。Forms;
using System。Data;
// 添加新的命名空间。
using System。Runtime。Serialization;
using System。
Runtime。Serialization。Formatters。Binary;
using System。IO;
namespace Serialization
{
///
/// 对象串行化。
///
public class Form1 : System。Windows。Forms。Form
{
private System。Windows。Forms。
Button button1;
private System。Windows。Forms。TextBox textBox1;
private System。Windows。
Forms。PictureBox pictureBox1;
private System。Windows。Forms。Button button2;
private System。
Windows。Forms。Label label1;
private System。Windows。Forms。Button button3;
private System。
Windows。Forms。OpenFileDialog openFileDialog1;
private System。Windows。Forms。SaveFileDialog saveFileDialog1;
///
/// 必需的设计器变量。
///
private System。ComponentModel。Container components = null;
public Form1()
{
// Windows 窗体设计器支持所必需的
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components。
Dispose();
}
}
base。Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this。button1 = new System。Windows。Forms。
Button();
this。textBox1 = new System。Windows。Forms。TextBox();
this。pictureBox1 = new System。
Windows。Forms。PictureBox();
this。button2 = new System。Windows。Forms。Button();
bel1 = new System。
Windows。Forms。Label();
this。button3 = new System。Windows。Forms。Button();
this。openFileDialog1 = new System。
Windows。Forms。OpenFileDialog();
veFileDialog1 = new System。Windows。Forms。SaveFileDialog();
this。
SuspendLayout();
//
// button1
//
this。button1。Location = new System。Drawing。Point(123, 268);
this。
button1。Name = "button1 ";
this。button1。Size = new System。Drawing。Size(56, 17);
this。
button1。TabIndex = 0;
this。button1。Text = "输出 ";
this。button1。TextAlign = System。Drawing。
ContentAlignment。TopCenter;
this。button1。Click += new System。EventHandler(this。button1_Click);
//
// textBox1
//
this。
textBox1。Location = new System。Drawing。Point(54, 243);
this。textBox1。Name = "textBox1 ";
this。
textBox1。Size = new System。Drawing。Size(240, 21);
this。textBox1。TabIndex = 1;
this。
textBox1。Text = " ";
//
// pictureBox1
//
this。pictureBox1。Dock = System。Windows。
Forms。DockStyle。Top;
this。pictureBox1。Location = new System。Drawing。Point(0, 0);
this。
pictureBox1。Name = "pictureBox1 ";
this。pictureBox1。Size = new System。Drawing。Size(301, 236);
this。
pictureBox1。SizeMode = System。Windows。Forms。PictureBoxSizeMode。StretchImage;
this。pictureBox1。
TabIndex = 2;
this。pictureBox1。TabStop = false;
//this。pictureBox1。Click += new System。
EventHandler(this。pictureBox1_Click);
//
// button2
//
this。button2。Location = new System。
Drawing。Point(206, 268);
this。button2。Name = "button2 ";
this。button2。Size = new System。
Drawing。Size(56, 17);
this。button2。TabIndex = 3;
this。button2。Text = "输入 ";
this。button2。
TextAlign = System。Drawing。ContentAlignment。TopCenter;
this。button2。Click += new System。
EventHandler(this。button2_Click);
//
// label1
//
bel1。AutoSize = true;
bel1。Location = new System。
Drawing。Point(6, 249);
bel1。Name = "label1 ";
bel1。Size = new System。Drawing。Size(48, 17);
bel1。
TabIndex = 4;
bel1。Text = "文件名: ";
//
// button3
//
this。button3。Location = new System。
Drawing。Point(41, 268);
this。button3。Name = "button3 ";
this。button3。Size = new System。
Drawing。Size(56, 17);
this。button3。TabIndex = 5;
this。button3。Text = "选择图片 ";
this。
button3。TextAlign = System。Drawing。ContentAlignment。TopCenter;
this。button3。Click += new System。
EventHandler(this。button3_Click);
//
// openFileDialog1
//
this。openFileDialog1。Filter = "图像文件 (*。
BMP;*。JPG;*。GIF)|*。BMP;*。JPG;*。GIF|自定义文件(*。ser)|*。ser ";
//
// saveFileDialog1
//
veFileDialog1。
FileName = "doc1 ";
veFileDialog1。Filter = "自定义文件(*。ser)|*。ser ";
//
// Form1
//
this。
AutoScaleBaseSize = new System。Drawing。Size(6, 14);
this。ClientSize = new System。
Drawing。Size(301, 287);
this。Controls。Add(this。button3);
this。Controls。Add( bel1);
this。Controls。
Add(this。button2);
this。Controls。Add(this。pictureBox1);
this。Controls。Add(this。textBox1);
this。
Controls。Add(this。button1);
this。FormBorderStyle = System。Windows。Forms。FormBorderStyle。FixedDialog;
this。
MaximizeBox = false;
this。Name = "Form1 ";
this。StartPosition = System。Windows。Forms。
FormStartPosition。CenterScreen;
this。Text = "对象串行化 ";
this。ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application。Run(new Form1());
}
// 定义私有变量。
// 输出文件流对象。
public Stream s;
// 串行化对象。
public BinaryFormatter f;
// 串行化对象输出。
private void button1_Click(object sender, System。EventArgs e)
{
if(pictureBox1。Image != null)
{
saveFileDialog1。
Filter = "自定义文件(*。ser)|*。ser ";
if(saveFileDialog1。ShowDialog() == DialogResult。OK)
{
s = File。
Create(saveFileDialog1。FileName);
f = new BinaryFormatter();
// 存储图形文件和对应的文件名。
f。Serialize(s, pictureBox1。
Image);
f。Serialize(s, textBox1。Text);
s。Close();
}
}
}
// 串行化对象输入。
private void button2_Click(object sender, System。
EventArgs e)
{
openFileDialog1。Filter = "自定义文件(*。ser)|*。ser ";
if(openFileDialog1。ShowDialog() == DialogResult。
OK)
{
s = File。OpenRead(openFileDialog1。FileName);
f = new BinaryFormatter();
pictureBox1。
Image = (Image)f。Deserialize(s);
textBox1。Text = (string)f。Deserialize(s);
s。Close();
}
}
// 选择要存储的图形文件。
private void button3_Click(object sender, System。EventArgs e)
{
openFileDialog1。Filter = "图形文件(*。
bmp;*。jpg;*。jpeg;*。gif)|*。bmp;*。jpg;*。jpeg;*。gif ";
if(openFileDialog1。ShowDialog() == DialogResult。
OK)
{
pictureBox1。Image = Image。FromFile(openFileDialog1。FileName);
textBox1。Text = openFileDialog1。
FileName;
}
}
}
}
。收起