菜鸟散分求救~~~~关于图片显示。。。
学.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];
file.Read(buffer,0,buffer.Length);
file.Close();
[解决办法]
hoho 补充一个
im.Save(@ "c:\test.jpg ")//将当前内存中的image对象直接保存为文件
[解决办法]
//自己的做法
this.pictureBox1.Image = Image.FromFile(@ "G:\其它文件\图片\0000.jpg ");
////用流的读出图片
//FileStream f = new FileStream(@ "G:\其它文件\图片\0000.jpg ", FileMode.Open);
//byte[] b = new byte[f.Length]; //这步和下面那步有点多余
//f.Read(b, 0, b.Length); //保存到数组
//this.pictureBox1.Image = Image.FromStream(f); //直接这样就好了
//f.Close();
////按你的要求还要更麻烦
//FileStream f = new FileStream(@ "G:\其它文件\图片\0000.jpg ", FileMode.Open);
//byte[] b = new byte[f.Length];
//f.Read(b, 0, b.Length);
//f.Close();
////后面要把数组还原回流
//MemoryStream ms = new MemoryStream(); //创建内存流
//ms.Write(b, 0, b.Length); //写入内存流
//this.pictureBox1.Image = Image.FromStream(ms); //显示图片
//ms.Close();