protected void lnkUploadFile_Click(object sender, EventArgs e) { try { DataKartDataContext dk = new DataKartDataContext(); if (!string.IsNullOrEmpty(flUpload.FileName)) { if (flUpload.HasFile) { if (flUpload.FileName.IndexOf(".jpg") > 0 || flUpload.FileName.IndexOf(".jpeg") > 0 || flUpload.FileName.IndexOf(".gif") > 0) { byte[] fl = flUpload.FileBytes; //Convert a imagem para o tamanho correto. System.Drawing.Image img = ImageUtil.ResizeImage(ImageUtil.BinaryToImage(fl), 170, 190, true); byte[] newFl = ImageUtil.ImageToBinary(img); Usuario u = (from p in dk.Usuarios where p.idUsuario == Convert.ToInt16(IdUsuario.Value) select p).FirstOrDefault(); if (u != null) { u.Foto = newFl; dk.SubmitChanges(ConflictMode.FailOnFirstConflict); ImgPerfil.ImageUrl = "~/Admin/ImageHandler.ashx?id=" + u.idUsuario; Alert("Foto do perfil atualizado com sucesso."); } else { Alert("Usuário não localizado na base!"); } } else { Alert("O upload da foto tem de ser JPG, JPEG ou GIF"); } } else { Alert("Não foi possivel efetuar o upload, tente novamente!"); } } } catch (Exception) { Alert("O upload da foto tem de ser JPG, JPEG ou GIF"); } }
public void ProcessRequest(HttpContext context) { try { context.Response.Clear(); Image image = null; Usuario user = null; if (context.Session != null && context.Session["Usuario"] != null) { user = (Usuario)context.Session["Usuario"]; } if (user == null) { if (!String.IsNullOrEmpty(context.Request.QueryString["id"])) { int id = Int32.Parse(context.Request.QueryString["id"]); DataKartDataContext dk = new DataKartDataContext(); user = (from u in dk.Usuarios where u.idUsuario == id select u).FirstOrDefault(); } } if (user.Foto != null && user.Foto.Length > 0) { image = ImageUtil.BinaryToImage(user.Foto); } else { image = ImageUtil.FileToImage(PathUtil.GetFullPathRoot() + @"\piloto-sem-foto.jpg"); } // Of course set this to whatever your format is of the image context.Response.ContentType = "image/jpeg"; // Save the image to the OutputStream image.Save(context.Response.OutputStream, ImageFormat.Jpeg); } catch (Exception ex) { LogErro.Log.Logar(ex, null); } }