How to Optimize And Upload Image in c#

How to Optimize And Upload Image in c#

public static bool OptimizeAndUploadImage(HttpPostedFileBase file, string imageName)
        {
            bool a = false;
            if (file != null && file.ContentLength != 0)
            {
                var file_filename = Path.GetFileName(file.FileName);
                var filewithoutext = Path.GetFileNameWithoutExtension(file.FileName);
                string SourceTempPath = HttpContext.Current.Server.MapPath("~/" + IdEncrypt + "Upload");
                if (!System.IO.Directory.Exists(SourceTempPath))
                {
                    System.IO.Directory.CreateDirectory(SourceTempPath);
                }
                var file_withid = caseIdEncrypt + filewithoutext;
                string ServerMapPath = Path.Combine(HttpContext.Current.Server.MapPath("~/" + IdEncrypt + "Upload/") + file.FileName);
                file.SaveAs(ServerMapPath);
                Bitmap bitmap = new Bitmap(file.InputStream);
                int iwidth = bitmap.Width;
                int iheight = bitmap.Height;
                var fileLength = new FileInfo(ServerMapPath).Length;
                bitmap.Dispose();
                Bitmap objOptImage = new System.Drawing.Bitmap(iwidth, iheight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
                using (System.Drawing.Image objImg = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/" + IdEncrypt + "Upload/" + file.FileName)))
                {
                    // RE-DRAW THE IMAGE USING THE NEWLY OBTAINED PIXEL FORMAT.
                    using (System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(objOptImage))
                    {
                        var _1 = oGraphic;
                        oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                        oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                        System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, iwidth, iheight);

                        _1.DrawImage(objImg, oRectangle);
                    }
                    using (Stream st = new MemoryStream())
                    {
                        objImg.Save(st, System.Drawing.Imaging.ImageFormat.Jpeg);
                        //Save your image
                    }
                    objImg.Dispose();
                }

                objOptImage.Dispose();
            }
            foreach (string file1 in Directory.GetFiles(System.Web.Hosting.HostingEnvironment.MapPath("~/" + IdEncrypt + "Upload")))
            {
                System.IO.File.Delete(file1);
            }
            if (System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/" + IdEncrypt + "Upload")))
            {
                System.IO.Directory.Delete(System.Web.Hosting.HostingEnvironment.MapPath("~/" + IdEncrypt + "Upload"), true);
            }
            return a;
        }

2020-02-05