请问:在windows(C#)程序里上传图片操作怎么写的??
呵  我初学 不太懂 有哪位大虾能否给出代码! 谢谢

解决方案 »

  1.   

    private void btnupload_images_Click(object sender, System.EventArgs e)
    {
       if(this.File1.Value.ToString()=="")//如果没有图片上传,则提示出错
     {
       Response.Write("<script>alert('请选择正确的图片文件')</script>");
     }
      else
     {
     string imagename=this.File1.Value.Substring(this.File1.Value.IndexOf(".")+1).ToUpper();
      if(imagename=="GIF" || imagename=="JPG")
      {
      if(this.File1.PostedFile.ContentLength>204800)//大小是否大于200K
       {
       Response.Write("<script>alert('你上传的图片文件过大')</script>");
       }
       else
        {
      //根据时间生成图片名
       string SaveName=DateTime.Now.ToString("yyyyMMddhhmmssfff");
           
      string fn=this.File1.PostedFile.FileName;
     string strImageName=SaveName+fn.Substring(fn.LastIndexOf("."));//图片名加上图片后缀名
      string strpath=Server.MapPath("")+"\\UpImages\\"+strImageName;//得到将要保存图片的路径
    this.File1.PostedFile.SaveAs(strpath);//把图片保存在此路径中
    this.Image1.ImageUrl="\\Web\\Webblog\\UpImages\\"+strImageName;//图片控件赋值
    this.Panel1.Visible=false;
    }
    }
    else
    {
    Response.Write("<script>alert('你上传的图片文件格式不对')</script>");
    }
    }
    }
      

  2.   

    老大,这个是用于网站后台的上传图片的代码啊,不是C# winFrom代码
      

  3.   

    private void menuItem5_Click(object sender, System.EventArgs e)//图片加载
    {
    OpenFileDialog of=new OpenFileDialog();
    FileStream fs;
    of.Filter="Picture Files(*.jpg)|*.jpg|Bmp(*.bmp)|*.bmp|All Files(*.*)|*.*";
    if(of.ShowDialog()==DialogResult.OK&&of.FileName!="")
    {
    fs=new FileStream(of.FileName,FileMode.Open,FileAccess.Read);
    byte[] btyPic=new byte[(int)fs.Length];
    fs.Read(btyPic,0,btyPic.Length);
    try
    {
    this.ptbImage.SizeMode=PictureBoxSizeMode.Normal;
    this.ptbImage.Image=Image.FromFile(of.FileName);
    }
    catch(Exception ImageExp)
    {
    MessageBox.Show(ImageExp.Message);
    }
    fs.Close();
    System.Data.SqlClient.SqlCommand sqlUpdateCommand11=new System.Data.SqlClient.SqlCommand();
    sqlUpdateCommand11.Connection = this.sqlCon;
    sqlUpdateCommand11.CommandText = "Update JBDA set zp=@zp where zgbh=@zgbh";  sqlUpdateCommand11.Parameters.Add(new System.Data.SqlClient.SqlParameter("@zgbh", System.Data.SqlDbType.VarChar, 50, "zgbh"));
    sqlUpdateCommand11.Parameters["@zgbh"].Value=this.txtzgbh.Text;
    sqlUpdateCommand11.Parameters.Add(new System.Data.SqlClient.SqlParameter("@zp", System.Data.SqlDbType.Image,16, "zp"));
    sqlUpdateCommand11.Parameters["@zp"].Value=btyPic;
    try{sqlUpdateCommand11.ExecuteNonQuery();}
    catch(Exception exp)
    {MessageBox.Show(exp.Message);}
    this.sqljbda.Update(this.dsResult,"JBDA");
    this.dsResult.Clear();
    this.sqljbda.Fill(this.dsResult,"JBDA");//sqlUpdateCommand11.Connection.Close();
    MessageBox.Show("图片插入成功");}
    }