求助! 我想把图片转成二进制 存入数据库 我想把图片编写成二进制 然后存入数据库 以及从数据库中读取出来 这种代码怎么写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很简单,cs.中protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile.FileName == "") { Label1.Text = "您还没有选择图片!"; return; } else { string filepath = FileUpload1.PostedFile.FileName; string filename=filepath.Substring(filepath.LastIndexOf("\\")+1); string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1); string serverpath = Server.MapPath("File/") + filename; if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif" ) { FileUpload1.PostedFile.SaveAs(serverpath); Image1.ImageUrl = "File/" + filename; Label1.Text = "上传成功!"; } else { Label1.Text = "上传的图片扩展名错误!"; } }前台:<td style="width: 100px; height: 30px"> <asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="选择图片"></asp:Label></td> <td align="left" style="width: 100px; height: 30px"> <asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" /></td> </tr> <tr> <td style="width: 100px; height: 30px"> </td> <td align="left" style="width: 100px; height: 30px"> <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="确定" Width="80px" /></td> </tr> <tr> <td style="width: 100px; height: 30px"> </td> <td align="left" style="width: 100px; height: 30px"> <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="9pt" ForeColor="Red" Text="*只允许上传.bpm/.jpg/.gif类型的图片!" Width="243px"></asp:Label></td> </tr> <tr> <td style="width: 100px; height: 30px"> </td> <td align="left" style="width: 100px; height: 30px"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="9pt" ForeColor="Red"></asp:Label></td> </tr> <tr> <td style="width: 100px; height: 30px"> </td> <td align="left" style="width: 100px; height: 30px"> <asp:Image ID="Image1" runat="server" /></td> public void UpFile(string Action,string Id,byte[] bytes) { string UpdateSql=string.Empty; if(Action!='') { UpdateSql=string.Format("update {0} set {1} =@p1 where {2}='{3}'", Business.Data.CTableJiBenYangLaoBaoXian.DBName,//0 Business.Data.JiBenYangLaoBaoXianRow.PJiBenYangLaoBaoXian_BaoXianImage,//1 Business.Data.JiBenYangLaoBaoXianRow.PJiBenYangLaoBaoXian_ID,//2 Id//3 ); } IDBManager dbm = (IDBManager)Serplate.InterfaceManager.InterfaceProvider.GetInterface(typeof(IDBManager)); ICmdAccess cmd = dbm.GetMainCmdAccess(); cmd.Open(); try { DBParamStruct[] paras = new DBParamStruct[]{new DBParamStruct("@p1",bytes)}; dbm.ExecuteNonQuery(new BaseDBCommandStruct(System.Data.CommandType.Text,UpdateSql,paras)); } catch(Exception ex) { throw(ex); } finally { cmd.Close(); } } static void Main(string[] args) { //char a = ''; //byte b = (byte)a; //Console.WriteLine(b); //byte b=009090; //string a=(string) b; //Console.WriteLine(a); string content="小"; string a = ""; int len; //FileStream fs = new FileStream("c:\\content.txt"); Encoding encoding = Encoding.GetEncoding("GB2312"); byte[] data = encoding.GetBytes(content); for (int i = 0; i < data.Length; i++) { a +=data[i]; } //len = content.Length; len = a.Length; Console.WriteLine(a); } protected void Button1_Click(object sender, EventArgs e) { try { string ImgPath = FileUpload1.PostedFile.FileName; string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1); string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1); if (!(ImgExtend == "bmp" || ImgExtend == "jpg" || ImgExtend == "gif")) { Label3.Text = "上传图片的格式不正确!"; return; } int FileLen = this.FileUpload1.PostedFile.ContentLength; Byte[] FileData = new Byte[FileLen]; HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象 Stream sr = hp.InputStream;//创建数据流对象 sr.Read(FileData, 0, FileLen);//将图片数据放到FileData数组对象实例中,其中0代表数组指针的起始位置,FileLen表示要读取流的长度(指针的结素位置) SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db_07"); con.Open(); SqlCommand com = new SqlCommand("INSERT INTO tb_15 (name) VALUES (@imgdata)", con); com.Parameters.Add("@imgdata", SqlDbType.Image); com.Parameters["@imgdata"].Value = FileData; com.ExecuteNonQuery(); Label3.Text = "保存成功!"; } catch (Exception error) { Label3.Text = "处理失败!原因为:" + error.ToString(); } } } 显示: SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db_07"); SqlDataAdapter ada = new SqlDataAdapter("select * from tb_17 ", con); con.Open(); DataSet ds = new DataSet(); ada.Fill(ds); DropDownList1.DataSource = ds; DropDownList1.DataTextField = "id"; DropDownList1.DataValueField = "id"; DropDownList1.DataBind(); Image1.Visible=false; } } protected void Button1_Click(object sender, EventArgs e) { Image1.Visible = true; SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=sa;database=db_07"); string imagename = ""; try { con.Open(); SqlCommand com = new SqlCommand("select name from tb_17 where id="+DropDownList1.Text+"", con); SqlDataReader dr = com.ExecuteReader(); dr.Read(); MemoryStream ms = new MemoryStream((Byte[])dr["name"]); Bitmap image = new Bitmap(ms); string filepath = Server.MapPath("Files/"); DirectoryInfo dir = new DirectoryInfo(filepath); FileInfo[] filecount = dir.GetFiles(); int i = filecount.Length; imagename = filepath + ((i + 1) + ".jpg"); image.Save(imagename); dr.Close(); Image1.ImageUrl = "Files/" + ((i + 1) + ".jpg"); } finally { con.Close(); } tabpage切换问题 [提问]Socket.Select第三个参数IList checkError用法 这条语句该如何改写? 如何获得Access的自动增长字段名称? IIS的问题! 为什么在C#中编写的单元测试总执行结果总是失败呢? 怎样逐个读出表中符合条件的记录? 使用ADO.NET 经常都在线的朋友,加入这个C#讨论群吧 我想用PC发送传真,请问: Visual Studio 2010中C#编写二维码转换时所生成的二维码图片是否可以拼接? 我问一下如何打包C#开发的程序? 不知道怎么写了! 帮忙下!
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Label1.Text = "您还没有选择图片!";
return;
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename=filepath.Substring(filepath.LastIndexOf("\\")+1);
string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);
string serverpath = Server.MapPath("File/") + filename;
if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif" )
{ FileUpload1.PostedFile.SaveAs(serverpath);
Image1.ImageUrl = "File/" + filename;
Label1.Text = "上传成功!";
}
else
{
Label1.Text = "上传的图片扩展名错误!";
} }
前台:<td style="width: 100px; height: 30px">
<asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="选择图片"></asp:Label></td>
<td align="left" style="width: 100px; height: 30px">
<asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" /></td>
</tr>
<tr>
<td style="width: 100px; height: 30px">
</td>
<td align="left" style="width: 100px; height: 30px">
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="确定"
Width="80px" /></td>
</tr>
<tr>
<td style="width: 100px; height: 30px">
</td>
<td align="left" style="width: 100px; height: 30px">
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Size="9pt" ForeColor="Red"
Text="*只允许上传.bpm/.jpg/.gif类型的图片!" Width="243px"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px; height: 30px">
</td>
<td align="left" style="width: 100px; height: 30px">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="9pt" ForeColor="Red"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px; height: 30px">
</td>
<td align="left" style="width: 100px; height: 30px">
<asp:Image ID="Image1" runat="server" /></td>
{
string UpdateSql=string.Empty;
if(Action!='')
{ UpdateSql=string.Format("update {0} set {1} =@p1 where {2}='{3}'",
Business.Data.CTableJiBenYangLaoBaoXian.DBName,//0
Business.Data.JiBenYangLaoBaoXianRow.PJiBenYangLaoBaoXian_BaoXianImage,//1
Business.Data.JiBenYangLaoBaoXianRow.PJiBenYangLaoBaoXian_ID,//2
Id//3
);
}
IDBManager dbm = (IDBManager)Serplate.InterfaceManager.InterfaceProvider.GetInterface(typeof(IDBManager));
ICmdAccess cmd = dbm.GetMainCmdAccess();
cmd.Open();
try
{
DBParamStruct[] paras = new DBParamStruct[]{new DBParamStruct("@p1",bytes)};
dbm.ExecuteNonQuery(new BaseDBCommandStruct(System.Data.CommandType.Text,UpdateSql,paras));
}
catch(Exception ex)
{
throw(ex);
}
finally
{
cmd.Close();
}
}
{
//char a = '';
//byte b = (byte)a;
//Console.WriteLine(b);
//byte b=009090;
//string a=(string) b;
//Console.WriteLine(a);
string content="小";
string a = "";
int len;
//FileStream fs = new FileStream("c:\\content.txt");
Encoding encoding = Encoding.GetEncoding("GB2312");
byte[] data = encoding.GetBytes(content);
for (int i = 0; i < data.Length; i++)
{
a +=data[i];
}
//len = content.Length;
len = a.Length;
Console.WriteLine(a);
}
{
try
{
string ImgPath = FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1);
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1);
if (!(ImgExtend == "bmp" || ImgExtend == "jpg" || ImgExtend == "gif"))
{
Label3.Text = "上传图片的格式不正确!";
return;
}
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象
Stream sr = hp.InputStream;//创建数据流对象
sr.Read(FileData, 0, FileLen);//将图片数据放到FileData数组对象实例中,其中0代表数组指针的起始位置,FileLen表示要读取流的长度(指针的结素位置)
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db_07");
con.Open();
SqlCommand com = new SqlCommand("INSERT INTO tb_15 (name) VALUES (@imgdata)", con);
com.Parameters.Add("@imgdata", SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
Label3.Text = "保存成功!";
}
catch (Exception error)
{
Label3.Text = "处理失败!原因为:" + error.ToString();
}
}
}
显示:
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=;database=db_07");
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_17 ", con);
con.Open();
DataSet ds = new DataSet();
ada.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "id";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
Image1.Visible=false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Image1.Visible = true;
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=sa;database=db_07");
string imagename = "";
try
{
con.Open();
SqlCommand com = new SqlCommand("select name from tb_17 where id="+DropDownList1.Text+"", con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
MemoryStream ms = new MemoryStream((Byte[])dr["name"]);
Bitmap image = new Bitmap(ms);
string filepath = Server.MapPath("Files/");
DirectoryInfo dir = new DirectoryInfo(filepath);
FileInfo[] filecount = dir.GetFiles();
int i = filecount.Length;
imagename = filepath + ((i + 1) + ".jpg");
image.Save(imagename);
dr.Close();
Image1.ImageUrl = "Files/" + ((i + 1) + ".jpg");
}
finally
{
con.Close();
}