如何将 Image 转换为 Byte[] 问题希望大家帮帮忙! 公司不能上网,代码不能拿到,其实就是那几行了 将img写入MemoryStream 就出错,我都快风掉了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void Button1_ServerClick(object sender, System.EventArgs e) { Stream imgStream; int imgLen; string imgName_value; string imgContentType; string imgUploadedName; imgStream = UploadFile.PostedFile.InputStream; imgLen = UploadFile.PostedFile.ContentLength; imgUploadedName = UploadFile.PostedFile.FileName; byte[] imgBinaryData=new byte[imgLen]; imgContentType = UploadFile.PostedFile.ContentType; try { imgName_value = GetLastRightOf(".",imgUploadedName); if (imgName_value == "nopic") { Response.Write( "<script>window.alert('所选文件不是图形文件,请选择图形文件上传!');</script>" ); return; } } catch(Exception myEx) { Response.Write(myEx.Message); } int n = imgStream.Read(imgBinaryData, 0, imgLen); string sheetid = Request.QueryString ["sheetid"].ToString (); string patternname = Request.QueryString ["patternname"].ToString (); string imgid = DataBase.DataFunction.SearchField ("ID","ws_pattern","patternname='"+patternname+"'"); int NumRowsAffected = MyDatabaseMethod(sheetid,patternname,imgid,imgBinaryData); if(NumRowsAffected > 0) { Response.Write( "<script>window.close();</script>" ); } else Response.Write( "<BR> an error occurred uploading the image.d " ); } public string GetLastRightOf(string LookFor,string myString) { int StrPos; StrPos = myString.LastIndexOf(LookFor); string temp = myString.Substring(StrPos + 1); //判断是否图片文件 if (temp != "bmp" ) //|| temp != "gif") { return "nopic"; } else { return myString.Substring(StrPos + 1); } } public int MyDatabaseMethod(string sheetid,string patternname,string imgid,byte[] imgbin) { string strconn = ConfigurationSettings.AppSettings["strConnection"]; SqlConnection connection = new SqlConnection(strconn); //SqlConnection connection = new SqlConnection(Application["Test_Conn"].ToString()); string isfind = DataBase.DataFunction.SearchField ("SHEETID","WS_SHEETIMAGE"," SHEETID="+Session["sheetid"].ToString()); string SQL=""; if (isfind != "") { //已有图片,改为修改记录 SQL="update ws_sheetimage set IMAGE = @IMAGE where SHEETID = " +Session["sheetid"].ToString(); } else { SQL="INSERT INTO ws_sheetimage (SHEETID,PATTERNNAME,ID,IMAGE) VALUES ( @SHEETID, @PATTERNNAME,@ID,@IMAGE )"; } SqlCommand command=new SqlCommand ( SQL,connection ); SqlParameter param0=new SqlParameter ( "@SHEETID", SqlDbType.VarChar,50 ); param0.Value = sheetid; command.Parameters.Add( param0 ); SqlParameter param1=new SqlParameter ( "@PATTERNNAME", SqlDbType.VarChar,30 ); param1.Value = patternname; command.Parameters.Add( param1 ); SqlParameter param2 =new SqlParameter ( "@ID", SqlDbType.SmallInt,2 ); param2.Value = imgid; command.Parameters.Add( param2 ); SqlParameter param3=new SqlParameter ( "@IMAGE", SqlDbType.Image ); param3.Value = imgbin; command.Parameters.Add( param3 ); connection.Open(); int numRowsAffected = command.ExecuteNonQuery(); connection.Close(); return numRowsAffected; } 没有那么麻烦了,你使用BinaryFormatter.Serialize就可以将对象保存为byte[]的了 FileStream fs = new FileStream ("e:\\1.bmp", FileMode.OpenOrCreate, FileAccess.Read);BinaryReader reader = new BinaryReader(fs);int i = Convert.ToInt32(fs.Length);byte[] MyData = new Byte[i];MyData = (byte[])reader.ReadBytes(i);//在这里跟踪下MyData看看 在sql server中存取图片看下边的:http://www.c-sharpcorner.com/Code/2002/Feb/FlashCardsMG.asp CHART控件的设置问题 如何建立数据库文件 Win7自带的刻录功能有没有供我们外部调用的API函数? 怎样将dataGridView里选中的一行内容显示在textBox里 FileUpload上传大小问题 在用http發送一個DataSet時﹐為什么不能直接用byte[]發送呢? 停止计时器后是否清零计时数据?再次启动时是从0开始计时的吗? 怪事了?? 关于测试驱动开发的疑问? 用C#开发的WEB,如何实现用代码定义打印纸张大小、formload时自动打印,然后自动关闭webform? 找到好工作了!!请各位帮忙.在线等!! 请问怎样保存当前的论坛网页
{
Stream imgStream;
int imgLen;
string imgName_value;
string imgContentType;
string imgUploadedName;
imgStream = UploadFile.PostedFile.InputStream;
imgLen = UploadFile.PostedFile.ContentLength;
imgUploadedName = UploadFile.PostedFile.FileName;
byte[] imgBinaryData=new byte[imgLen];
imgContentType = UploadFile.PostedFile.ContentType;
try
{
imgName_value = GetLastRightOf(".",imgUploadedName); if (imgName_value == "nopic")
{
Response.Write( "<script>window.alert('所选文件不是图形文件,请选择图形文件上传!');</script>" );
return;
}
}
catch(Exception myEx)
{
Response.Write(myEx.Message);
}
int n = imgStream.Read(imgBinaryData, 0, imgLen);
string sheetid = Request.QueryString ["sheetid"].ToString ();
string patternname = Request.QueryString ["patternname"].ToString ();
string imgid = DataBase.DataFunction.SearchField ("ID","ws_pattern","patternname='"+patternname+"'"); int NumRowsAffected = MyDatabaseMethod(sheetid,patternname,imgid,imgBinaryData);
if(NumRowsAffected > 0)
{
Response.Write( "<script>window.close();</script>" );
}
else
Response.Write( "<BR> an error occurred uploading the image.d " );
}
public string GetLastRightOf(string LookFor,string myString)
{
int StrPos;
StrPos = myString.LastIndexOf(LookFor);
string temp = myString.Substring(StrPos + 1);
//判断是否图片文件
if (temp != "bmp" ) //|| temp != "gif")
{
return "nopic";
}
else
{
return myString.Substring(StrPos + 1);
}
}
{
string strconn = ConfigurationSettings.AppSettings["strConnection"];
SqlConnection connection = new SqlConnection(strconn);
//SqlConnection connection = new SqlConnection(Application["Test_Conn"].ToString());
string isfind = DataBase.DataFunction.SearchField ("SHEETID","WS_SHEETIMAGE"," SHEETID="+Session["sheetid"].ToString());
string SQL="";
if (isfind != "")
{
//已有图片,改为修改记录
SQL="update ws_sheetimage set IMAGE = @IMAGE where SHEETID = " +Session["sheetid"].ToString();
}
else
{
SQL="INSERT INTO ws_sheetimage (SHEETID,PATTERNNAME,ID,IMAGE) VALUES ( @SHEETID, @PATTERNNAME,@ID,@IMAGE )";
}
SqlCommand command=new SqlCommand ( SQL,connection );
SqlParameter param0=new SqlParameter ( "@SHEETID", SqlDbType.VarChar,50 );
param0.Value = sheetid;
command.Parameters.Add( param0 );
SqlParameter param1=new SqlParameter ( "@PATTERNNAME", SqlDbType.VarChar,30 );
param1.Value = patternname;
command.Parameters.Add( param1 );
SqlParameter param2 =new SqlParameter ( "@ID", SqlDbType.SmallInt,2 );
param2.Value = imgid;
command.Parameters.Add( param2 ); SqlParameter param3=new SqlParameter ( "@IMAGE", SqlDbType.Image );
param3.Value = imgbin;
command.Parameters.Add( param3 );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
return numRowsAffected;
}
FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);int i = Convert.ToInt32(fs.Length);
byte[] MyData = new Byte[i];
MyData = (byte[])reader.ReadBytes(i);//在这里跟踪下MyData看看
http://www.c-sharpcorner.com/Code/2002/Feb/FlashCardsMG.asp