/// <summary> /// Saves file to the server. /// </summary> /// <param name="sDataString">Posted 64 Bit data string</param> /// <param name="sFileName">File Name</param> /// <param name="sUrl">Prefix</param> /// <param name="bOverwrite">OverWrite</param> /// <param name="sPrefix">Prefix</param> /// <param name="sSuffix">Suffix</param> /// <param name="iWidth">Image Width Size</param> /// <param name="iHeight">Image Height Size</param> /// <param name="bThumb">Is Thumbnail</param> /// <returns>bool</returns> public bool SaveBase64File(string sDataString, string sFileName, string sUrl, bool bOverwrite, string sPrefix, string sSuffix, int iWidth = 0, int iHeight = 0, bool bThumb = false) { // copied from https://gist.github.com/vbfox/484643 var base64Data = Regex.Match(sDataString, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value; var binData = Convert.FromBase64String(base64Data); bool sStatus = false; string sServerFileUrl = String.Empty; using (var stream = new MemoryStream(binData)) { var vImage = Image.FromStream(stream); int iNewWidth = vImage.Width; int iNewHeight = vImage.Height; //Start creating new bitmap with the new dimension. var vImageBithmap = new Bitmap(iWidth, iHeight); //Start creating graphic effect for smoothing the file. var vImageGraph = Graphics.FromImage(vImageBithmap); vImageGraph.CompositingQuality = CompositingQuality.HighQuality; vImageGraph.SmoothingMode = SmoothingMode.HighQuality; //Start creating Rectangle to contain the image. var vImageRec = new Rectangle(0, 0, iWidth, iHeight); vImageGraph.DrawImage(vImage, vImageRec); //Save the image with the new Size. string sFileUrl = String.Empty; string sNFileName = String.Empty; if (bOverwrite) { sNFileName = sPrefix + Path.GetFileNameWithoutExtension(sFileName) + sSuffix + Path.GetExtension(sFileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sNFileName); if (File.Exists(sFileUrl)) { File.Delete(sFileUrl); } vImageBithmap.Save(sFileUrl, vImage.RawFormat); } else { sNFileName = sPrefix + Guid.NewGuid().ToString("N") + sSuffix + Path.GetExtension(sFileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sNFileName); vImageBithmap.Save(sFileUrl, vImage.RawFormat); } //Add a new url so the user can get the server url of the file. sServerFileUrl = sUrl + "/" + sNFileName; if (bThumb) { ThumbUrls.Add(sServerFileUrl); } else { FileUrls.Add(sServerFileUrl); } //Dispose all used objects. vImageGraph.Dispose(); vImageBithmap.Dispose(); vImage.Dispose(); sStatus = true; } return(sStatus); }
/// <summary> /// Saves file to the server. /// </summary> /// <param name="oFile">Posted FileBase</param> /// <param name="sUrl">Relative Save Path</param> /// <param name="iWidth">Image Width Size</param> /// <param name="iHeight">Image Height Size</param> /// <param name="bOverwrite">Overwrite</param> /// <param name="bThumb">Make it as a thumbnail</param> /// <returns>bool</returns> public bool SaveFile(HttpPostedFileBase oFile, string sUrl, int iWidth, int iHeight, bool bOverwrite, bool bThumb, string sPrefix = "", string sSuffix = "") { try { bool sStatus = false; string sServerFileUrl = String.Empty; //Check if the filebase has content. if (oFile != null) { //Create the image from filebase to Image Drawing. var vImage = Image.FromStream(oFile.InputStream); //Start creating new bitmap with the new dimension. var vImageBithmap = new Bitmap(iWidth, iHeight); //Start creating graphic effect for smoothing the file. var vImageGraph = Graphics.FromImage(vImageBithmap); vImageGraph.CompositingQuality = CompositingQuality.HighQuality; vImageGraph.SmoothingMode = SmoothingMode.HighQuality; //Start creating Rectangle to contain the image. var vImageRec = new Rectangle(0, 0, iWidth, iHeight); vImageGraph.DrawImage(vImage, vImageRec); //Save the image with the new Size. string sFileUrl = String.Empty; string sFileName = String.Empty; if (bOverwrite) { sFileName = sPrefix + Path.GetFileNameWithoutExtension(oFile.FileName) + sSuffix + Path.GetExtension(oFile.FileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sFileName); if (File.Exists(sFileUrl)) { File.Delete(sFileUrl); } vImageBithmap.Save(sFileUrl, vImage.RawFormat); } else { sFileName = sPrefix + Guid.NewGuid().ToString("N") + sSuffix + Path.GetExtension(oFile.FileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sFileName); vImageBithmap.Save(sFileUrl, vImage.RawFormat); } //Add a new url so the user can get the server url of the file. sServerFileUrl = sUrl + "/" + sFileName; if (bThumb) { ThumbUrls.Add(sServerFileUrl); } else { FileUrls.Add(sServerFileUrl); } //Dispose all used objects. vImageGraph.Dispose(); vImageBithmap.Dispose(); vImage.Dispose(); sStatus = true; } return(sStatus); } catch (Exception ex) { Errors.Add(new Error() { Message = ex.Message, Exception = ex }); return(false); } }