/* * Uploads posted images upon verification */ //private string[] UpLoadAllImages(string[] strImgPathArray) private BlogStruct UpLoadAllImages(ref BlogStruct tmpStruct) { string tmpFile; string thmbNailPath; string userDir = ""; //get user dir userDir = Session["userDir"].ToString(); //if panel is open check for loaded img files if (pnlAddImages.Visible) { //Collect files names, iterate through each and update accordingly HttpFileCollection uploadFilCol = System.Web.HttpContext.Current.Request.Files; int count = uploadFilCol.Count; //loop thru for each posted file for (int i = 0; i < count; i++) { //get handle to file HttpPostedFile file = uploadFilCol[i]; if (file.ContentLength > (int)0) { //get file name & ext string fileName = Path.GetFileName(file.FileName); string fileExt = Path.GetExtension(file.FileName).ToLower(); string tmpDir = AppDomain.CurrentDomain.BaseDirectory + @"\users\" + userDir + @"\temp\"; //check for temp dir and create if non-existent if (!(Directory.Exists(tmpDir))) { //'E:\kunden\homepages\31\d213027625\Showcase\users\0\0\0\0\0\0\0\1\6\2\temp\ Directory.CreateDirectory(tmpDir); } //get physical path to temp dir for upload string path = tmpDir; //Generate random file name tmpFile = GenerateRandomString(8).ToLower(); //concatenate renamed file with ext tmpFile = tmpFile + fileExt; //add together path and file name; This is our fully qualified *temp path where the file gets uploaded //strImgPathArray[i] = Path.Combine(path, tmpFile); tmpStruct.tmpArray[i] = Path.Combine(path, tmpFile); thmbNailPath = Path.Combine(path, "thmbNail_" + tmpFile); //Upload to temp dir; we'll write to perm directory after user confirms on preview! //FIXME: Check for file type and size if (fileName != string.Empty) { //resize the image and save //Create an image object from the uploaded file. try { System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(file.InputStream); //Larger Image variables int maxWidth = 300; int maxHeight = 300; int sourceWidth = UploadedImage.Width; int sourceHeight = UploadedImage.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; //ThumbNail variables int maxWidth_T = 150; int maxHeight_T = 150; int destX_T = 0; int destY_T = 0; float nPercent_T = 0; float nPercentW_T = 0; float nPercentH_T = 0; //Larger Image percents nPercentW = ((float)maxWidth / (float)sourceWidth); nPercentH = ((float)maxHeight / (float)sourceHeight); //Thumbnail percents nPercentW_T = ((float)maxWidth_T / (float)sourceWidth); nPercentH_T = ((float)maxHeight_T / (float)sourceHeight); //Find the biggest change(smallest pct) if (nPercentH < nPercentW) { //Larger Image Calculations nPercent = nPercentH; destX = System.Convert.ToInt16((maxWidth - (sourceWidth * nPercent)) / 2); //Thumbnail Calculations nPercent_T = nPercentH_T; destX_T = System.Convert.ToInt16((maxWidth_T - (sourceWidth * nPercent_T)) / 2); } else { //Larger Image Calculations nPercent = nPercentW; destY = System.Convert.ToInt16((maxHeight - (sourceHeight * nPercent)) / 2); //ThumbNail Calculations nPercent_T = nPercentW_T; destY_T = System.Convert.ToInt16((maxWidth_T - (sourceHeight * nPercent_T)) / 2); } //Larger Image Calculations int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); //Smaller Image Calculations int destWidth_T = (int)(sourceWidth * nPercent_T); int destHeight_T = (int)(sourceHeight * nPercent_T); //create the larger bitmap Bitmap bmPhoto = new Bitmap(maxWidth, maxHeight); bmPhoto.SetResolution(UploadedImage.HorizontalResolution, UploadedImage.VerticalResolution); //create the thumbnail bitmap Bitmap bmPhotoThmbNail = new Bitmap(maxWidth_T, maxHeight_T); bmPhotoThmbNail.SetResolution(UploadedImage.HorizontalResolution, UploadedImage.VerticalResolution); //create larger graphic Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; grPhoto.Clear(Color.Transparent); grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(UploadedImage, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); //create thumbnail graphic Graphics grPhotoThmbNail = Graphics.FromImage(bmPhotoThmbNail); grPhotoThmbNail.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; grPhotoThmbNail.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; grPhotoThmbNail.Clear(Color.Transparent); grPhotoThmbNail.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; grPhotoThmbNail.DrawImage(UploadedImage, new Rectangle(destX_T, destY_T, destWidth_T, destHeight_T), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); //int memberID = 0; //Save the Image(s) // (1) "Saves" the graphic. It really just updates the bitmap with the contents of the graphic. // Basically saving it in memory. // (2) Actually save the bitmap to the file system. //Larger grPhoto.Save(); // (1) bmPhoto.Save(tmpStruct.tmpArray[i]); // (2) .., ImageFormat.Jpeg) //strip out the superfluous server path and just save the file name; this was the cause of much grief as we were saving the entire path! tmpStruct.tmpArray[i] = Path.GetFileName(tmpStruct.tmpArray[i]); //Thumbnail grPhotoThmbNail.Save(); bmPhotoThmbNail.Save(thmbNailPath); //Find out who is set to "change" or "add" so we know who to update // - We call the clearSelection() to let ourselves know that index has been processed //TODO: append (&& File1.Value.Length>0) if (rdoImgMgr1.SelectedValue == "Change" || rdoImgMgr1.SelectedValue == "Add") { tmpStruct.sBlog.ImgPath1 = tmpStruct.tmpArray[i]; rdoImgMgr1.ClearSelection(); } else if (rdoImgMgr2.SelectedValue == "Change" || rdoImgMgr2.SelectedValue == "Add") { tmpStruct.sBlog.ImgPath2 = tmpStruct.tmpArray[i]; rdoImgMgr2.ClearSelection(); } } catch (Exception ex) { lblStatus.Text = "Resize/Save Error!"; } } else { tmpStruct.tmpArray[i] = string.Empty; } } } }//end main if //return strImgPathArray; return(tmpStruct); }
private void btnNext_Click(object sender, System.EventArgs e) { //get object from session object classes.Blog tmpBlog = (classes.Blog)Session["Blog"]; //kick out if validation failed if (!(Page.IsValid)) { return; } //array holder for pics string[] strImgPathArray = new string[2]; //init img array with blank space for (int j = 0; j <= 1; j++) { strImgPathArray[j] = ""; } //Check for new or swapped pics. The user has 3 possible options here; Keep, delete, //or change an image. ***NOTE: For BETA, editing AFTER original post will not be previewed. //This means pics will *not* be moved to a temp dir as we do in posting. This should change eventually. //Delete any files in imgDel array bool blnProcImgs = false; switch (rdoImgMgr1.SelectedValue) { case "Keep": break; case "Delete": tmpBlog.ImgPath1 = ""; break; case "Change": blnProcImgs = true; break; case "Add": blnProcImgs = true; break; default: break; } switch (rdoImgMgr2.SelectedValue) { case "Keep": break; case "Delete": tmpBlog.ImgPath2 = ""; break; case "Change": blnProcImgs = true; break; case "Add": blnProcImgs = true; break; default: break; } //Process media if (pnlAddImages.Visible && blnProcImgs) { //Declare and load a struct to send to UploadAllImages() BlogStruct bStruct = new BlogStruct(); bStruct.sBlog = tmpBlog; bStruct.tmpArray = strImgPathArray; //AHM comment out temporarily bStruct = UpLoadAllImages(ref bStruct); tmpBlog = bStruct.sBlog; } //Save common data and verify strings; we'll check the string after the preview tmpBlog.MyBlog = txtDetails.Text; tmpBlog.Title = txtAdTitle.Text; //Save object to session variable Session["Blog"] = tmpBlog; //Goto preview blog Response.Redirect("BlogPreview.aspx"); }