日期:2014-05-19  浏览次数:20910 次

菜鸟散分求救~~~~关于图片显示。。。
学.net没有多久

现在想用c#实现以下两个步骤

1、从本地磁盘中将以图片装入byte数组中;
2、从byte数组中读出图片并在pictuerbox中显示出来。

请问如何实现啊???



------解决方案--------------------
Bitmap tmpBmp=new Bitmap(sFilePath);
this.pictureBox1.Image=tmpBmp;
this.pictureBox1.Size=tmpBmp.Size;
------解决方案--------------------
/// <summary>
/// 上传文件到服务器的指定路径下,文件名自动生成
/// 如产生重复,增加#和顺序号避免冲突
/// </summary>
/// <param name= "file "> 上传图片的控件 </param>
/// <param name= "post_file_url "> 要保存到数据库字段中的文件名和相对路径,传入时是相对路径,传出时是文件名和相对路径 </param>
/// <returns> 成功时返回“成功”,失败时返回失败原因 </returns>
public string uploadFile(HttpRequest request,HtmlInputFile file,ref string post_file_url)
{
string fileName=file.PostedFile.FileName.Substring (file.PostedFile.FileName.LastIndexOf( '\\ ')+1);//文件名,包括扩展名
string file_name= " "; //文件名(包括路径,不包括扩展名)
string result= " "; //返回结果字符串
string strPicName= " ";
int i=0;
int j=0;
try
{
if(file.PostedFile.FileName.ToString().Trim()!= " ")
{
if(file.PostedFile.ContentLength > 2097152)
{
result= "上传文件过大! ";
return result;
}
System.DateTime dtime=DateTime.Now;
strPicName=dtime.Year.ToString()+dtime.Month.ToString()+dtime.Day.ToString()+dtime.Hour.ToString()+dtime.Minute.ToString()+dtime.Second.ToString()+dtime.Millisecond;
file_name=request.PhysicalApplicationPath+@ "tempimage\ "+ strPicName;

j=fileName.LastIndexOf( ". ");
System.IO.FileInfo fileInfo=new System.IO.FileInfo(file_name);
if(!fileInfo.Exists )
{
file.PostedFile.SaveAs(file_name+fileName.Substring(j));
post_file_url= @ "tempimage\ " + strPicName + fileName.Substring(j);
}
else
{
bool file_save=true;
while(file_save)
{
j=fileName.LastIndexOf( ". ");
fileInfo = new System.IO.FileInfo(file_name+ "# "+i.ToString() + fileName.Substring(j));
if(!fileInfo.Exists)
{
file.PostedFile.SaveAs(file_name+ "# "+i.ToString()+ fileName.Substring(j));
file_save=false;
post_file_url = @ "tempimage\ " + strPicName + "# "+i.ToString() + fileName.Substring(j);
}
else
i++;
}
}
}
result= "成功 ";
return result;
}
catch(Exception ex)
{
result= "错误: "+ex.Message.ToString()+ "文件上传出错,提交失败! ";
return result;
}
}


这是上传图片得 缩进懒得调了
------解决方案--------------------
再帮你一把 将指定图片生成64位字节流

public string GetPicBase64Code(string path)
{
string str= " ";
try
{
FileStream fs = new FileStream(path,FileMode.Open);
BinaryReader br = new BinaryReader(fs);

byte[] buffer = new byte[fs.Length];
br.Read(buffer,0,buffer.Length);
br.Close();
fs.Close();
str = Convert.ToBase64String(buffer);
}
catch
{
str= " ";
}

return str;

}
------解决方案--------------------
读到byte
string path = this.openFileDialog1.FileName;
Stream file = new StreamReader(path,System.Text.Encoding.Default).BaseStream;
byte[] buffer = new byte[file.Length];