private void worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundHelperRequest argmunets = (BackgroundHelperRequest)e.Argument; string imgLocation; //AlgorythmParameters parameters = null; //Call Algorithm Bitmap newImage = new AnaglyphAlgorithmInvoker(argmunets.selectedAlgorythm).Apply(argmunets.image); //Save bmp to root of app SaveBitmapImage(newImage, argmunets.selectedAlgorythm, out imgLocation); var path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); string fullpath = path + @"\" + imgLocation; BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(fullpath); bitmap.EndInit(); bitmap.Freeze(); e.Result = new BackgroundHelperResponse { image = bitmap, location = fullpath, }; newImage.Dispose(); }
public ActionResult Upload(FormCollection formCollection) { if (Request != null) { HttpPostedFileBase leftPhoto = Request.Files["LeftPhoto"]; HttpPostedFileBase rightPhoto = Request.Files["RightPhoto"]; if (PhotosIsValid(leftPhoto, rightPhoto)) { string fileName = leftPhoto.FileName; string fileContentType = leftPhoto.ContentType; byte[] fileBytes = new byte[leftPhoto.ContentLength]; leftPhoto.InputStream.Read(fileBytes, 0, Convert.ToInt32(leftPhoto.ContentLength)); var pathLeft = Path.Combine(Server.MapPath("~/Content/uploads"), fileName); leftPhoto.SaveAs(pathLeft); fileName = rightPhoto.FileName; fileBytes = new byte[rightPhoto.ContentLength]; rightPhoto.InputStream.Read(fileBytes, 0, Convert.ToInt32(rightPhoto.ContentLength)); var pathRight = Path.Combine(Server.MapPath("~/Content/uploads"), fileName); rightPhoto.SaveAs(pathRight); string selectedAlgorithm = Request.Form["SelectedAlgorithm"]; Bitmap newImage = new AnaglyphAlgorithmInvoker(selectedAlgorithm).Apply(new Bitmap(pathLeft), new Bitmap(pathRight)); Random rnd = new Random(); string outfilename = "output-" + selectedAlgorithm.Replace(" ", "_").ToLower() + "-" + rnd.Next(0, 2315412) + ".jpeg"; string outputFileName = Path.Combine(Server.MapPath("~/Content/outputs"), outfilename); string htmlName = "/Content/outputs/" + outfilename; using (MemoryStream memory = new MemoryStream()) { using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite)) { newImage.Save(memory, ImageFormat.Jpeg); byte[] bytes = memory.ToArray(); fs.Write(bytes, 0, bytes.Length); fs.Close(); } } ViewBag.sa = selectedAlgorithm; ViewBag.outputImage = htmlName; } } return(View()); }