我如何更正我的代码 [英] How do I correct my code
本文介绍了我如何更正我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.Security.Cryptography;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string s1 =richTextBox1.Text;
string s2 =richTextBox2.Text;
string s3 =richTextBox3.Text;
Byte[] newdata = encoding.GetBytes(s1);
Byte[] encrypted = myrsa.Encrypt(newdata, false);
for (int i = 0; i < encrypted.Length;i++ )
richTextBox2.Text += encrypted[i].ToString();
Byte[] decrypted = myrsa.Decrypt(encrypted, false);//decrypt
string dData = encoding.GetString(decrypted);
richTextBox3.Text += dData;
}
private void button2_Click(object sender, EventArgs e)
{
RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
openFileDialog1.Title = "Insert Image";
openFileDialog1.ShowDialog();
openFileDialog1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif";
object r = new object();
EventArgs en = new EventArgs();
string s1 = richTextBox1.Text;
string s2 = richTextBox2.Text;
string s3 = richTextBox3.Text;
string Chosen_File = "";
Chosen_File = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(Chosen_File);
byte[] byteData = imageToByteArray(pictureBox1.Image);
string ToS1 = byteData.ToString();
Byte[] newdata = encoding.GetBytes(ToS1);
Byte[] encrypted = myrsa.Encrypt(newdata, false);
for (int i = 0; i < encrypted.Length; i++)
richTextBox4.Text += encrypted[i].ToString();
Byte[] decrypted = myrsa.Decrypt(encrypted, false);//decrypt
pictureBox2.Image = byteArrayToImage(decrypted);
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
Stream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
}
有错误按下按钮2.请复习。
There is an error while pressing button 2.Please Review.
推荐答案
简单:
Easy:
byte[] byteData = imageToByteArray(pictureBox1.Image);
string ToS1 = byteData.ToString();
Byte[] newdata = encoding.GetBytes(ToS1);
当你在数组上使用ToString时,你获取一个包含类名称的字符串,而不是数组数据的字符串表示。
在上面三行中的第三行放一个断点并查看ToS1的内容,你我会明白我的意思。
我不知道你认为代码应该怎么做,所以我不能告诉你怎么做才能解决它!
When you use ToString on an array, you get back a string which contains the name of the class, rather than as string representation of the array data.
Put a breakpoint on the third of the three lines above and look at the content of ToS1, you will see what I mean.
I have no idea what you think that code should do, so I can't really tell you what to do to fix it!
你的字节数组不包含有效的图像文件,所以行
It' seams that you byte array does not contain a valid image file, so the line
Image returnImage = Image.FromStream(ms);
失败,参数无效。
您可以尝试使用Image.FromStream的重写:http://msdn.microsoft.com/en-us/library/21zw9ah6(v=vs .110).aspx [ ^ ] ...
这篇关于我如何更正我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文