}//end of useUser #endregion #region Private Funcation //crop an image base on what the user has selected using jCrop private byte[] cropImage(string strImageName, int intImageWidth, int intImageHeight, int intImageX, int intImageY) { try { using (SD.Image OriginalImage = SD.Image.FromFile(strImageName)) { int intNewImageHeight = intImageHeight;//holds the height of the new image that will be create out of the crop old image int intNewImageWidth = intImageWidth;//holds the width of the new image that will be create out of the crop old image int intNewImageYStart = 0;//holds the Y coordinate to center the image at the start int intNewImageXStart = 0;//holds the X coordinate to center the image at the start //checks if the intNewImageWidth is not intDefaultWidth if(intNewImageWidth != intDefaultWidth) { //does the calucations for the center the image for the X coordinate intNewImageXStart = ((intDefaultWidth - intNewImageWidth) / 2); //default intNewImageWidth to the max width allowed in order to created bars on the side intNewImageWidth = intDefaultWidth; }//end of if //checks if the intNewImageHeight is not intDefaultHeight if(intNewImageHeight != intDefaultHeight) { //does the calucations for the center the image for the Y coordinate intNewImageYStart = ((intDefaultHeight - intNewImageHeight) / 2); //default intNewImageHeight to the max height allowed in order to created bars intNewImageHeight = intDefaultHeight; }//end of if using (SD.Bitmap bmp = new SD.Bitmap(intNewImageWidth, intNewImageHeight)) { bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution); using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp)) { Graphic.SmoothingMode = SmoothingMode.AntiAlias; Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; Graphic.DrawImage(OriginalImage, new SD.Rectangle(intNewImageXStart, intNewImageYStart, intImageWidth, intImageHeight), intImageX, intImageY, intImageWidth, intImageHeight, SD.GraphicsUnit.Pixel); MemoryStream ms = new MemoryStream(); bmp.Save(ms, OriginalImage.RawFormat); return ms.GetBuffer(); }//end of using }//end of using }//end of using }//end of try catch (Exception ex) { throw (ex); }//end of catch }//end of cropImage()
}//end of genPassword() //resizes a image public static SD.Image resizeImage(SD.Image imgBigImage, int ingMaxWidth, int ingMaxHeight) { int intNewWidth = 0;//holds the new width of the new image int intNewHeight = 0;//holds the new height of the new image int intCurrentWidth = imgBigImage.Width;//holds the current width of the image int intCurrentHeight = imgBigImage.Height;//holds the current height of the image //checks if the width is larger then the heigth if so then //floors the width and set the height to be apporenit to the width if ((intCurrentWidth / (double)ingMaxWidth) > (intCurrentHeight / (double)ingMaxHeight)) { //adjuest the height to be apporent now and floors the width intNewWidth = ingMaxWidth; intNewHeight = Convert.ToInt32(intCurrentHeight * (ingMaxWidth / (double)intCurrentWidth)); //checks if the new height is not bigger then the max if (intNewHeight > ingMaxHeight) { //adjuest the width to be apporent now and floors the height intNewWidth = Convert.ToInt32(ingMaxWidth * (ingMaxHeight / (double)intNewHeight)); intNewHeight = ingMaxHeight; }//end of if }//end of if //else floors the height and set the width to be apporenit to the height else { //adjuest the width to be apporent now and floors the height intNewWidth = Convert.ToInt32(intCurrentWidth * (ingMaxHeight / (double)intCurrentHeight)); intNewHeight = ingMaxHeight; //checks if the new width is not bigger then the max if (intNewWidth > ingMaxWidth) { //adjuest the height to be apporent now and floors the width intNewWidth = ingMaxWidth; intNewHeight = Convert.ToInt32(ingMaxHeight * (ingMaxWidth / (double)intNewWidth)); }//end of if }//end of else SD.Bitmap bitNewImage = new SD.Bitmap(intNewWidth, intNewHeight);//holds the new bitmap of the image //web resolution; bitNewImage.SetResolution(72, 72); SD.Graphics grImage = SD.Graphics.FromImage(bitNewImage);//holds the graphics object grImage.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; //creates a new holder of where the new image will be drawen to grImage.FillRectangle(new SD.SolidBrush(SD.Color.White), 0, 0, bitNewImage.Width, bitNewImage.Height); //Re-draw the image to the specified height and width grImage.DrawImage(imgBigImage, 0, 0, bitNewImage.Width, bitNewImage.Height); return bitNewImage; }//end of resizeImage()
}//end of cmdResize_Click() protected void cmdCrop_Click(object sender, EventArgs e) { try { //turns off the error message lblUploadCropError.Visible = false; //checks if there cropping image as the user may refresh the page this will active it if(File.Exists(Server.MapPath("~" + imgCrop.ImageUrl))) { byte[] bytCropImage = cropImage(Server.MapPath("~" + imgCrop.ImageUrl), Convert.ToInt32(W.Value), Convert.ToInt32(H.Value), Convert.ToInt32(X.Value), Convert.ToInt32(Y.Value));//holds the byties of the crop image //ues a memory stream of the image using (MemoryStream ms = new MemoryStream(bytCropImage, 0, bytCropImage.Length)) { //writes to memory stream using the bytes from from the image ms.Write(bytCropImage, 0, bytCropImage.Length); //uses a image from the memory stream to recreate the image as a file using(SD.Image sdimgCroppedImage = SD.Image.FromStream(ms, true)) { // Make sure a duplicate file doesn't exist. If it does, keep on appending an // incremental numeric until it is unique string strImageFile = Server.MapPath("~" + imgCrop.ImageUrl.Replace("/images/" + hfDir.Value + "/Temp/" + hfCurrentIDDir.Value + "/","/"));//holds the location of the file string strImageFileLoc = System.IO.Path.GetFileName(strImageFile);//holds the name of the file string strDraftDir = "Draft/";//holds the location of where the draft images are going int intFileAppend = 0;//holds which a number to make the file unqiure //checks if is is the live if so then draft dirtory is not need if(hfCurrentIDDir.Value == hfCurrentID.Value) strDraftDir = ""; //goes around checking if there is already a file with the same name while (System.IO.File.Exists(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc))) { //adds to the file append in order to make this file unqiue in the dirtory intFileAppend++; strImageFileLoc = System.IO.Path.GetFileNameWithoutExtension(strImageFile) + intFileAppend.ToString() + System.IO.Path.GetExtension(strImageFile).ToLower(); }//end of while loop //actully crops the image sdimgCroppedImage.Save(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc), sdimgCroppedImage.RawFormat); //uses the image for the using(SD.Image imgCurrent = SD.Image.FromFile(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc))) { SD.Image imgLarge = General.resizeImage(imgCurrent, (intMaxWidth * 2), (intMaxHeight * 2));//holds the image as a larger image for imgCurrent for panning effect SD.Image imgThumb = General.resizeImage(imgCurrent, intMaxThumbnailWidth, intMaxThumbnailHeight);//holds the image as a thumbnail for later use SD.Image imgThumbUpload = General.resizeImage(imgCurrent, intMaxThumbnailUploadWidth, intMaxThumbnailUploadHeight);//holds the image as a thumbnail that is a little bit smaller as parts of the site need to use this SD.Image imgIcon = General.resizeImage(imgCurrent, intMaxIconWidth, intMaxIconHeight);//holds the image as a icon for imgCurrent that will go into the navigation of the image slider //saves the image to server again this time in a more larger image imgLarge.Save(Server.MapPath("~/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc).Replace(".","_LG."), sdimgCroppedImage.RawFormat); //saves the image to server again this time in a more small image imgThumb.Save(Server.MapPath("~/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc).Replace(".","_thumbnail."), sdimgCroppedImage.RawFormat); //saves the image to server again this time in a more small image imgThumbUpload.Save(Server.MapPath("~/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc).Replace(".","_upload_thumbnail."), sdimgCroppedImage.RawFormat); //saves the image to server again this time in a more smaller image imgIcon.Save(Server.MapPath("~/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strImageFileLoc).Replace(".","_icon."), sdimgCroppedImage.RawFormat); //checks if this is FH Image or a User Image if(boolUseUser == true) { DataTable dtImage = DAL.getRow("", "Where = " + hfCurrentID.Value);//holds all of the images for this Obituary in order to get the total number of images for this ese //updates the Obituary with the newest image DAL.addUpdateObituaryImage(0, Convert.ToInt32(hfCurrentID.Value), (dtImage.Rows.Count + 1), strImageFileLoc.Replace("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/","")); }//end of if else { DataTable dtImage = DAL.getRow("", "Where = " + hfCurrentID.Value);//holds all of the images for this FH in order to get the total number of images for this ese //updates the FuneralHome with the newest image DAL.addUpdateFuneralHomeImage(0, Convert.ToInt32(hfCurrentID.Value), (dtImage.Rows.Count + 1), strImageFileLoc.Replace("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/",""), true); }//end of else //updates all images again with the current transition mode that was selected ddlTransitionModes_SelectedIndexChanged(sender, e); }//end of using }//end of using //closes the stream ms.Close(); }//end of using }//end of if //resets the Croping/Image section loadImages(); }//end of try catch (Exception ex) { lblUploadCropError.Text = ex.Message;// + " " + ex.StackTrace; lblUploadCropError.Visible = true; }//end of catch }//end of cmdCrop_Click()
}//end of cmdUpload_Click() protected void cmdResize_Click(object sender, EventArgs e) { try { //turns off the error message lblUploadResizeError.Visible = false; //checks if there cropping image as the user may refresh the page this will active it if(File.Exists(Server.MapPath("~" + imgResize.ImageUrl))) { //uses the image using(SD.Image imgCurrent = SD.Image.FromFile(Server.MapPath("~" + imgResize.ImageUrl))) { string strWidthResize = WResize.Value;//holds the width value that will be the new size string strHeightResize = HResize.Value;//holds the height value that will be the new size int intAllowedImageHeight = intDefaultHeight;//holds the height of the crop area alloed int intAllowedImageWidth = intDefaultWidth;//holds the width of the crop area alloed //checks if there is a . in strWidthResize if(strWidthResize.IndexOf(".") > 0) //removes all number from the the . on as this couses an error and for some reason //the convert does not work just rounds up to the nears number strWidthResize = strWidthResize.Substring(0,strWidthResize.IndexOf(".")); //checks if there is a . in strHeightResize if(strHeightResize.IndexOf(".") > 0) //removes all number from the the . on as this couses an error and for some reason //the convert does not work just rounds up to the nears number strHeightResize = strHeightResize.Substring(0, strHeightResize.IndexOf(".")); //checks if strWidthResize is smaill of then the default width if so then sent the croping //to that width as to allowed a for bars on the sides //if the image is smaller then the default width if(Convert.ToInt32(strWidthResize) < intDefaultWidth) intAllowedImageWidth = Convert.ToInt32(strWidthResize); //checks if strHeightResize is smaill of then the default height //if so then sent the croping to that height as to allowed //a for bars on the sides if the image is smaller then the default height if(Convert.ToInt32(strHeightResize) < intDefaultHeight) intAllowedImageHeight = Convert.ToInt32(strHeightResize); SD.Image imgThumb = General.resizeImage(imgCurrent, Convert.ToInt32(strWidthResize), Convert.ToInt32(strHeightResize));//holds the image as a new size that the user likes //saves the image to server again this time in a more small image imgThumb.Save(Server.MapPath("~" + imgResize.ImageUrl.Replace("_thumb2.","_thumb."))); //changes the breadcrumb from resizing to cropping panBreadcrumbResize.CssClass = panBreadcrumbResize.CssClass.Replace("divSelectedBreadcrumb", ""); panBreadcrumbCrop.CssClass += " divSelectedBreadcrumb"; //turns off the resize and gives the user croping panResize.Visible = false; panCrop.Visible = true; imgCrop.ImageUrl = imgResize.ImageUrl.Replace("_thumb2.","_thumb."); //sets jquery for croping with all as there will be litCrop.Text = "$('#" + imgCrop.ClientID + "').Jcrop({\n" + "onSelect: storeCoords,\n" + "minSize: [ " + intAllowedImageWidth + ", " + intAllowedImageHeight + "],\n" + "maxSize: [ " + intAllowedImageWidth + ", " + intAllowedImageHeight + "],\n" + "setSelect: [ " + intAllowedImageWidth + ", " + intAllowedImageHeight + ", 0, 0 ]\n" + "});"; //sets jquery for image of the croping with all as there will be litCropImageClick.Text = "$('#" + imgCrop.ClientID + "').click(function() {\n" + litCrop.Text + "\n" + "});"; //sets the size of the image into W, H, X, Y area as a starting point for the cropping W.Value = imgThumb.Width.ToString(); H.Value = imgThumb.Height.ToString(); X.Value = "0"; Y.Value = "0"; //sets the text for the Croping lblCurrentStep.Text = "Crop your photo"; litHowToUse.Text = "<br/><label>Click and position the highlighted box to the desired area of your photo and then click the CROP button to finalize your selection. If your image is less than the display height or width, the crop box will not be visible. Click CROP to continue.</label>"; }//end of using }//end of if }//end of try catch (Exception ex) { lblUploadResizeError.Text = ex.Message;// + " " + ex.StackTrace; lblUploadResizeError.Visible = true; }//end of catch }//end of cmdResize_Click()
}//end of Page_PreRender() protected void cmdUpload_Click(object sender, EventArgs e) { try { //Change if they what to changes the Main Image of the Category if (!string.IsNullOrEmpty(fulImageUpload.PostedFile.FileName)) { string strImageLocDir = "/images/" + hfDir.Value + "/Temp/" + hfCurrentIDDir.Value + "/";//holds the loction //checks if strImageLocDir temp dirtory has this item in it if not then create one if(!Directory.Exists(Server.MapPath("~/" + strImageLocDir))) //creates dirtory for where the image will be store temporay Directory.CreateDirectory(Server.MapPath("~/" + strImageLocDir)); //checks if strImageLocDir draft dirtory has this item in it if not then create one if(!Directory.Exists(Server.MapPath("~/" + strImageLocDir.Replace("/Temp/","/") + "Draft/"))) //creates dirtory for where the image will be store Directory.CreateDirectory(Server.MapPath("~/" + strImageLocDir.Replace("/Temp/","/") + "Draft/")); //checks if strImageLocDir dirtory has this item in it if not then create one if(!Directory.Exists(Server.MapPath("~/" + strImageLocDir.Replace("/Temp/","/")))) //creates dirtory for where the image will be store Directory.CreateDirectory(Server.MapPath("~/" + strImageLocDir.Replace("/Temp/","/"))); //uplaods the Image to the site string strImageName = General.uploadImage(strImageLocDir,fulImageUpload.PostedFile); //checks if there was an error with the upliad if(strImageName.IndexOf("ERROR! ") >= 0) { //tells the user that there was an error lblUploadError.Text = "Image Upload Error: " + strImageName; lblUploadError.Visible = true; }//end of if else { //uses the image using(SD.Image imgCurrent = SD.Image.FromFile(Server.MapPath("~" + strImageName))) { SD.Image imgThumb = General.resizeImage(imgCurrent, intMaxWidth, intMaxHeight);//holds the image as a thumbnail //saves the image to server again this time in a more small image imgThumb.Save(Server.MapPath("~" + strImageName.Replace(" ","").Replace(".","_thumb2."))); //changes the breadcrumb from upload to resizing panBreadcrumbUpload.CssClass = panBreadcrumbUpload.CssClass.Replace("divSelectedBreadcrumb", ""); panBreadcrumbResize.CssClass += " divSelectedBreadcrumb"; //turns off the upload and gives the user resizing panUpload.Visible = false; panResize.Visible = true; imgResize.ImageUrl = strImageName.Replace(" ","").Replace(".","_thumb2."); imgResize.Width = imgThumb.Width; imgResize.Height = imgThumb.Height; //sets the width and height orginal outline for the user to tell where is the starting point panUploadResizeOrginalArea.Attributes.Add("style", "width: " + imgThumb.Width + "px;height: " + imgThumb.Height + "px;"); //sets the size of the image into WResize, HResize area as a starting point for the resizing WResize.Value = imgThumb.Width.ToString(); HResize.Value = imgThumb.Height.ToString(); lblCurrentStep.Text = "Resize your photo"; litHowToUse.Text = "<br/><label>Resize your photo by clicking and dragging the lower right corner of the picture, making the image larger or smaller. The red 'represents' the height and width the final image will be displayed at. Click the resize button when finished. During the next step, this 'crop box' can be moved freely around the image to select the area you wish to display.</label>"; }//end of using }//end of else }//end of if }//end of try catch (Exception ex) { lblUploadError.Text = "Upload Error: " + ex.Message;// + " " + ex.StackTrace lblUploadError.Visible = true; }//end of catch }//end of cmdUpload_Click()
}//end of useUser #endregion protected void Page_PreRender(object sender, EventArgs e) { if (!IsPostBack) { DataTable dtImage = DAL.getRow("", "Where = " + hfCurrentID.Value + " Order by ");//holds all of the images in order int intRowID = 0;//holds the unqiure row id int intDatabaseImageOrder = 0;//holds the Image Order coming from the database int intDatabaseImageTransitionMode = 0;//holds the Image Transition Mode string strDatabaseImageFileName = "";//holds the Image File Name coming from the database string strDraftDir = "Draft/";//holds the location of where the draft images are going //checks if is is the live if so then draft dirtory is not need if(hfCurrentIDDir.Value == hfCurrentID.Value) strDraftDir = ""; //checks if this is from FH or User if(boolUseUser == true) //gets the details of the image dtImage = DAL.getRow("","Where = " + hfCurrentID.Value + " Order by "); //checks if there is a image to display if(dtImage.Rows.Count > 0) { //goes around adding the images foreach (DataRow drImage in dtImage.Rows) { //checks if this is from FH or User if(boolUseUser == true) { //sets the file name and id strDatabaseImageFileName = drImage[""].ToString(); intDatabaseImageOrder = Convert.ToInt32(drImage[""].ToString()); intDatabaseImageTransitionMode = Convert.ToInt32(drImage[""].ToString()); }//end of if else { //sets the file name and id strDatabaseImageFileName = drImage[""].ToString(); intDatabaseImageOrder = Convert.ToInt32(drImage[""].ToString()); intDatabaseImageTransitionMode = Convert.ToInt32(drImage[""].ToString()); }//end of else //checks that this is first item as this is the only needs to happen onnce //also checks if there is at least two images to transition to //as the image would transiton to nothing if it was one image if(intRowID == 0 && dtImage.Rows.Count > 1) { //sets the scripting for the image sliding effect litScript.Text = "<script type='text/javascript'>\n" + "$(document).ready(function () {\n" + "$('#" + panSliderImage.ClientID + "').bannerscollection_zoominout({\n" + "skin: 'opportune',\n" + "responsive: true,\n" + "duration: 8,\n" + "durationIEfix: 8,\n" + "autoPlay: 8,\n"; //checks if this is Transition Mode as it uses a different transition effect to //move to another slide if(intDatabaseImageTransitionMode == 2) litScript.Text += "fadeSlides: false,\n"; litScript.Text += "width: 543,\n" + "height: 285,\n" + "circleRadius: 8,\n" + "circleLineWidth: 4,\n" + "circleColor: '#ffffff',\n" + "circleAlpha: 50,\n" + "behindCircleColor: '#000000',\n" + "behindCircleAlpha: 20,\n" + "showCircleTimer: false,\n" + "showNavArrows: false,\n" + "thumbsWrapperMarginTop: 30\n" + "});\n" + "});\n" + "</script>"; }//end of if else if(intRowID == 0) { //sets the scripting for the one image display litScript.Text = "<script type='text/javascript'>\n" + "$(document).ready(function () {\n" + "$('.myloader').css('display','none');\n" + "$('.bannerscollection_zoominout_list').css('display','block');\n" + "});\n" + "</script>"; }//end of else //checks if the file in file system if so then display it if (File.Exists(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strDatabaseImageFileName))) { //uses the image using(SD.Image imgCurrent = SD.Image.FromFile(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strDatabaseImageFileName))) { litSliderImage.Text += "<li id='liSlider" + intRowID + "' "; //checks if there is at least two images to transition to //as the image would transiton to nothing if it was one image if(dtImage.Rows.Count > 1) { //checks if there is thumbnail if (File.Exists(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strDatabaseImageFileName.Replace(".","_icon.")))) //uses the icon for the thumbnail nav button litSliderImage.Text += " data-bottom-thumb='/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strDatabaseImageFileName.Replace(".","_icon.") + "'"; else //uses the thumbnail for the thumbnail nav button litSliderImage.Text += " data-bottom-thumb='/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strDatabaseImageFileName.Replace(".","_thumbnail.") + "'"; //checks which transition mode the image are in panning, fading, sweeping if(intDatabaseImageTransitionMode == 0) { System.Random ranNumber = new System.Random((int)System.DateTime.Now.Ticks);//holds the object that will random gen numbers string[] strStartingVerticalPosition = new string[2] {"bottom", "top"};//holds the vertical position where the image will start string[] strStartingHorizontalPosition = new string[2] {"left", "right"};//holds the horizontal position where the image will start //sets the transition mode to Panning and randomly choose a vertical and horizontal position litSliderImage.Text += " data-horizontalPosition='" + strStartingHorizontalPosition[ranNumber.Next(0, 2)] + "' data-verticalPosition='" + strStartingVerticalPosition[ranNumber.Next(0, 2)] + "' data-initialZoom='0.72' data-finalZoom='1'"; }//end of if else if(intDatabaseImageTransitionMode == 1) //sets the transition mode to Fading litSliderImage.Text += " data-initialZoom='1' data-finalZoom='1' data-transition='fade'"; else //sets the transition mode to Sweeping litSliderImage.Text += " data-initialZoom='1' data-finalZoom='1'"; }//end of if else //sets the this itme for displaying in one image mode litSliderImage.Text += " class='liImageSliderOneImage'"; litSliderImage.Text += ">" + "<img src=\"/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir; //checks if this is a panning TransitionMode and there is a larger version of the //image as panning needs a bigger verion to move around in if(intDatabaseImageTransitionMode == 0 && File.Exists(Server.MapPath("/images/" + hfDir.Value + "/" + hfCurrentIDDir.Value + "/" + strDraftDir + strDatabaseImageFileName.Replace(".","_LG.")))) litSliderImage.Text += strDatabaseImageFileName.Replace(".","_LG.") + "\" width='" + (imgCurrent.Width + 210) + "' height='" + (imgCurrent.Height + 210) + "'"; else //displays the normal version litSliderImage.Text += strDatabaseImageFileName + "\" width='" + imgCurrent.Width + "' height='" + imgCurrent.Height + "'"; litSliderImage.Text += " alt='Image " + intDatabaseImageOrder + "' id='imgSlider" + intRowID + "' />" + "</li>"; }//end of using }//end of if intRowID++; }//end of foreach }//end of if else //removes the slider from view panSliderImage.Visible = false; }//end of if }//end of Page_PreRender()
}//end of stripHtml() //uploads the Image to the server public static string uploadImage(string strSavePath,HttpPostedFile myFile, int intMaxThumbnailWidth = 0, int intMaxThumbnailHeight = 0) { // Check file size (mustn't be 0) int intFileLen = myFile.ContentLength; string strImageFileExtension = System.IO.Path.GetExtension(myFile.FileName);//holds the file extension if (intFileLen == 0) return "ERROR! File Length is zero for " + myFile.FileName; // Check file extension that it is the internet images .jpg/.gif/.png if (strImageFileExtension.ToLower() != ".jpg" && strImageFileExtension.ToLower() != ".png" && strImageFileExtension.ToLower() != ".gif") return "ERROR! The Image file must have an extension of either JPG, PNG or GIF: for " + myFile.FileName; //checks if the file size is above 8MB if(intFileLen > 8000000) return "ERROR! The Image file most be below 8MB"; // Read file into a data stream byte[] bytData = new Byte[intFileLen]; myFile.InputStream.Read(bytData,0,intFileLen); // Make sure a duplicate file doesn't exist. If it does, keep on appending an // incremental numeric until it is unique string strImageFileName = System.IO.Path.GetFileName(myFile.FileName); int file_append = 0; //checks if the the file name is loarger then 200 char without the extension if((strImageFileName.Replace(strImageFileExtension,"")).Length > 200) //shorts the file name to fit into the database strImageFileName = (strImageFileName.Replace(strImageFileExtension,"")).Substring(0,200) + strImageFileExtension; while (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strSavePath + strImageFileName))) { file_append++; strImageFileName = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + strImageFileExtension.ToLower(); }//end of while loop // Save the stream to disk System.IO.FileStream newFile = new System.IO.FileStream(HttpContext.Current.Server.MapPath(strSavePath + strImageFileName.Replace("'","")), System.IO.FileMode.Create); newFile.Write(bytData, 0, bytData.Length); newFile.Close(); //checks if this image needs to be a thumbnail as there will be times that a Thumbnail is needed if(intMaxThumbnailWidth > 0 && intMaxThumbnailHeight > 0) { //ues a memory stream of the image using (MemoryStream ms = new MemoryStream(bytData, 0, bytData.Length)) { //writes to memory stream using the bytes from the image ms.Write(bytData, 0, bytData.Length); //uses a image from the memory stream to recreate the image as a file using(SD.Image sdimgCroppedImage = SD.Image.FromStream(ms, true)) { //uses the image from the uploading to make different version of them //to use in different areas of the site using(SD.Image imgCurrent = SD.Image.FromFile(HttpContext.Current.Server.MapPath(strSavePath + strImageFileName.Replace("'","")))) { SD.Image imgThumb = resizeImage(imgCurrent, intMaxThumbnailWidth, intMaxThumbnailHeight);//holds the image as a thumbnail for later use //saves the image to server again this time in a thumbnail of what was just upload imgThumb.Save(HttpContext.Current.Server.MapPath(strSavePath + strImageFileName).Replace(".","_upload_thumbnail."), sdimgCroppedImage.RawFormat); }//end of using }//end of using }//end of using }//end of if return strSavePath + strImageFileName.Replace("'",""); }//end of uploadImage()