如何用c#直接读取二进制文件,并且可以进行图像显示?

能否高人指点一下,并附带程序?
能否程序详细点?或者给我qq我深度的请教一下?
程序实验了一下 不成功啊!
大哥们 能有一个整体点的程序么?从图像的读取到显示啊!
感谢各位大哥的厚爱,但是确实还是不能满足我的需求,我再说一下我的需求是:无符号整型二进制图像文件的读取,并能在picturebox中显示,而且能够根据图像不同的像素值范围,显示不同的色彩,望高人指点一下具体程序!小弟叩谢了!
最新回答
笑里藏泪

2024-11-07 00:17:35

this.Image1.Visible = true;
string name = this.FileUpload1.FileName;

string type = name.Substring(name.LastIndexOf(".") + 1);

string ipath = Server.MapPath("image") + "\\" + name;

string wpath = "image\\" + name;
string time = DateTime.Now.ToShortDateString();
//string gsname = this.DropDownList1.SelectedValue.ToString();
//string zgname = this.DropDownList2.SelectedValue.ToString();
string sql = "insert into contents (gsname,zgname,problem,cause,resolve,flag,linkman,photo,date) values (@gsname,@zgname,@problem,@cause,@resolve,@flag,@linkman,@ipath,@time)";
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{
FileUpload1.SaveAs(ipath); //服务器保存路径

SqlConnection conn = new SqlConnection(@"server=;uid=;pwd=;database=");
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();

cmd.Parameters.Add("@gsname", SqlDbType.VarChar, 25);
cmd.Parameters["@gsname"].Value = this.DropDownList1.SelectedItem.Text;
cmd.Parameters.Add("@zgname", SqlDbType.VarChar, 25);
cmd.Parameters["@zgname"].Value = this.DropDownList2.SelectedItem.Text;

cmd.Parameters.Add("@problem", SqlDbType.VarChar, 2000);
cmd.Parameters["@problem"].Value = this.txtproblem.Text;

cmd.Parameters.Add("@cause", SqlDbType.VarChar, 2000);
cmd.Parameters["@cause"].Value = this.txtcause.Text.ToString();

cmd.Parameters.Add("@resolve", SqlDbType.VarChar, 2000);
cmd.Parameters["@resolve"].Value = this.txtresolve.Text;

cmd.Parameters.Add("@linkman",SqlDbType.VarChar,25);
cmd.Parameters["@linkman"].Value = this.Txtlinkman.Text;

cmd.Parameters.Add("@ipath", SqlDbType.VarChar, 500);
cmd.Parameters["@ipath"].Value = wpath;

cmd.Parameters.Add("@time", SqlDbType.VarChar, 10);
cmd.Parameters["@time"].Value = time;

cmd.Parameters.Add("@flag",SqlDbType.NVarChar,1);
cmd.Parameters["@flag"].Value = this.txtflag.Text;

cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('保存成功')</script>");
this.Image1.ImageUrl = "image/" + name;

}
else
{
Response.Write("<script>alert('保存失败')</script>");
}

把上面的代码修改下能直接用
PS:
建议数据库保存图片地址,不要直接存储图片,现实图片直接读取图片的路径
别了初心

2024-11-07 00:14:28

下面两个函数,一个是将图片转成字符串,另一个是将字符串转成图片

/// <summary>
/// 获取图片的字符串
/// </summary>
/// <param name="img">图片对象</param>
/// <returns>图片的字符串</returns>
public string getImageString(Image img)
{
//获取字符串
MemoryStream bs = new MemoryStream();
img.Save(bs, System.Drawing.Imaging.ImageFormat.Png);
return Convert.ToBase64String(bs.ToArray());
}
/// <summary>
/// 根据字符串获取图片
/// </summary>
/// <param name="image">图片字符串</param>
/// <returns>图片</returns>
public Image getImageFormString(string image)
{
byte[] bytes=Convert.FromBase64String(image);
MemoryStream bs = new MemoryStream(bytes);
return Image.FromStream(bs);
}